Simple 'PHP Unit Test' for detecting pre function variable dump for laravel and PHP.
A simple unit testing a class method for detecting pre function variable dumper in both php and php framework's laravel.
//---code snippet--//
protected static function searchForExecutedVariableDumper($className, $methodName, $dump = null) {
$class = new \ReflectionClass($className);
$content = $class->getMethod($methodName);
$file = $content->getFileName();
$start_line = $content->getStartLine();
$end_line = $content->getEndLine() - 1;
$length = $end_line - $start_line;
$source = file($file);
$body = array_slice($source, $start_line, $length);
$search = [];
$search2 = [];
$subject = '';
$dumper = '';
if($dump) {
switch ($dump) {
case 'dd' :
case 'dd()' :
$dumper = 'dd';
break;
case 'dump' :
case 'dump()' :
$dumper = 'dump';
break;
case 'var_dump' :
case 'var_dump()' :
$dumper = 'var_dump';
break;
}
}
foreach ($body as $segment) {
$subset_prefix = '';
$subset_suffix = '';
$dumper_prefix = preg_match("/var_dump|dd|dump/", $segment, $matches);
/*
* Dumper base on the var dumper present.
* */
if($dumper_prefix && is_null($dump)) {
$dumper = $matches[0];
switch ($dumper) {
case 'dd' :
$dumper = 'dd';
break;
case 'dump' :
$dumper = 'dump';
break;
case 'var_dump' :
$dumper = 'var_dump';
break;
}
}
/*
* If the $dump var not equal to dumper present.
* */
if($dumper_prefix) {
$dumper = $matches[0];
switch ($dumper) {
case 'dd' :
$dumper = 'dd';
break;
case 'dump' :
$dumper = 'dump';
break;
case 'var_dump' :
$dumper = 'var_dump';
break;
}
}
if($dumper == 'dd') {
$subset_prefix = substr(trim($segment), 0 , 3);
$subset_suffix = substr(trim($segment), -2 , 1);
}
if($dumper == 'var_dump') {
$subset_prefix = substr(trim($segment), 0 , 9);
$subset_suffix = substr(trim($segment), -2 , 1);
}
if($dumper == 'dump') {
$subset_prefix = substr(trim($segment), 0 , 5);
$subset_suffix = substr(trim($segment), -2 , 1);
}
$search[] = $subset_prefix;
$search2[] = $subset_suffix;
if( (in_array('dd(', $search) && in_array(')', $search2)) || (in_array('var_dump(', $search) && in_array(')', $search2)) || (in_array('dump(', $search) && in_array(')', $search2)) ) {
$str_concat = $subset_prefix . $subset_suffix;
switch ($str_concat) {
case 'dd()' :
$subject = $str_concat;
break;
case 'var_dump()' :
$subject = $str_concat;
break;
case 'dump()' :
$subject = $str_concat;
break;
}
break;
}
}
if($dumper == 'dd') {
self::assertNotSame($subject, 'dd()', 'A variable dumper (\'dd()\') found being executed!');
}
if($dumper == 'var_dump') {
self::assertNotSame($subject, 'var_dump()', 'A variable dumper (\'var_dump()\') found being executed!');
}
if($dumper == 'dump') {
self::assertNotSame($subject, 'dump()', 'A variable dumper (\'dump()\') found being executed!');
}
}
Nice one GO :)
A good write up on how to use PHP to make SuperiorCoin Transactions with Json would be cool.
@mps01k thanks for the comment and info i will give it a shot.
@originalworks
The @OriginalWorks bot has determined this post by @arnelpancho to be original material and upvoted it!
To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!
Congratulations @arnelpancho! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
You published your First Post
Award for the number of upvotes
Award for the number of upvotes received
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP