Skip to content

Add --custom-function parameter to check #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static function parseArguments(array $arguments)
{
$arguments = new ArrayIterator(array_slice($arguments, 1));
$setting = new self;
$customFunctions = array();

foreach ($arguments as $argument) {
if ($argument[0] !== '-') {
Expand Down Expand Up @@ -124,6 +125,10 @@ public static function parseArguments(array $arguments)
$setting->functionsToCheck[] = self::LARAVEL_DUMP;
break;

case '--custom-function':
$customFunctions = array_map('trim', explode(',', $arguments->getNext()));
break;

case '--doctrine':
$setting->functionsToCheck[] = self::DOCTRINE_DUMP;
$setting->functionsToCheck[] = self::DOCTRINE_DUMP_2;
Expand All @@ -134,7 +139,11 @@ public static function parseArguments(array $arguments)
}
}
}

// Merge if any custom function is given
$setting->functionsToCheck = array_merge($setting->functionsToCheck, $customFunctions);
$setting->functionsToCheck = array_unique($setting->functionsToCheck);

return $setting;
}

Expand Down
79 changes: 79 additions & 0 deletions tests/JakubOnderka/PhpVarDumpCheck/CustomFunctionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

use JakubOnderka\PhpVarDumpCheck;

class CustomFunctionTest extends PHPUnit_Framework_TestCase
{

public function testCheck_noDebugFunction()
{
$settings = new PhpVarDumpCheck\Settings();

$settings->functionsToCheck = array_merge($settings->functionsToCheck, array(
'functionName1'
));

$uut = new PhpVarDumpCheck\Checker($settings);
$content = <<<PHP
<?php
nonDebugFunction1(\$var);
\$i++;
nonDebugFunction2(\$i);
PHP;
$result = $uut->check($content);
$this->assertCount(0, $result);
}

public function testCheck_singleFunction()
{
$settings = new PhpVarDumpCheck\Settings();

$settings->functionsToCheck = array_merge($settings->functionsToCheck, array(
'functionName1'
));

$uut = new PhpVarDumpCheck\Checker($settings);
$content = <<<PHP
<?php
functionName1(\$var);
PHP;
$result = $uut->check($content);
$this->assertCount(1, $result);
}


public function testCheck_multipleFunction()
{
$settings = new PhpVarDumpCheck\Settings();

$settings->functionsToCheck = array_merge($settings->functionsToCheck, array(
'functionName1',
'functionName2'
));

$uut = new PhpVarDumpCheck\Checker($settings);

$content1 = <<<PHP
<?php
functionName1(\$var);
PHP;
$content2 = <<<PHP
<?php
functionName2(\$var);
PHP;
$content3 = <<<PHP
<?php
functionName1(\$var);
sleep(4);
functionName2(\$var);
PHP;
$result = $uut->check($content1);
$this->assertCount(1, $result);

$result = $uut->check($content2);
$this->assertCount(1, $result);

$result = $uut->check($content3);
$this->assertCount(2, $result);
}
}
1 change: 1 addition & 0 deletions var-dump-check.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function showOptions() {
--symfony Enable support for Symfony2 (dump, VarDumper::dump, VarDumper::setHandler, Vardumper::dd())
--doctrine Enable support for Doctrine (Doctrine::dump, \Doctrine\Common\Util\Debug::dump)
--laravel Enable support for Laravel (dd, dump)
--custom-function Comma separated custom function name(s) to check like "pre_echo".
--extensions Check only files with selected extensions separated by comma
(default: php, php3, php4, php5, phtml)
--exclude Exclude directory. If you want exclude multiple directory, use
Expand Down