for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types = 1);
namespace hanneskod\readmetester;
/**
* Wrapps an executable block of code
*/
class CodeBlock
{
* @var string The contained code
private $code;
public function __construct(string $code)
$this->code = $code;
}
* Prepend this code block with the contents of $codeBlock
public function prepend(CodeBlock $codeBlock)
$this->code = sprintf(
"%s\n%s%s\n%s",
'ob_start();',
$codeBlock->getCode(),
'ob_end_clean();',
$this->code
);
* Grab contained code
public function getCode(): string
return $this->code;
* Execute code block
*
* @return Result The result of the executed code
public function execute(): Result
$returnValue = '';
$returnValue
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
$myVar = 'Value'; $higher = false; if (rand(1, 6) > 3) { $higher = true; } else { $higher = false; }
Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.
$myVar
$higher
$exception = null;
ob_start();
try {
$returnValue = eval($this->code);
} catch (\Exception $e) {
catch (\Exception $e) { $exception = $e; }
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.
return
die
exit
function fx() { try { doSomething(); return true; } catch (\Exception $e) { return false; } return false; }
In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.
return false
$exception = $e;
return new Result($returnValue, ob_get_clean(), $exception);
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.