for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Spaark\CompositeUtils\Factory\Reflection;
use Spaark\CompositeUtils\Factory\BaseFactory;
use Spaark\CompositeUtils\Model\Reflection\ReflectionFile;
use Spaark\CompositeUtils\Model\Reflection\NamespaceBlock;
use Spaark\CompositeUtils\Model\Reflection\UseStatement;
use Spaark\CompositeUtils\Service\RawPropertyAccessor;
use \ReflectionClass as PHPNativeReflectionClass;
class ReflectionFileFactory extends ReflectorFactory
{
/**
* @var string
*/
protected $filename;
* @var int
protected $i = 0;
* @var array
protected $tokens;
public function __construct(string $filename)
$this->filename = $filename;
$this->object = new ReflectionFile();
$this->accessor = new RawPropertyAccessor($this->object);
}
public function build()
$this->parseFile();
return $this->object;
public function parseFile()
$tokens = token_get_all(file_get_contents($this->filename));
$matching = null;
$classname = '';
$as = '';
$currentNS = null;
foreach ($tokens as $token)
if ($token === ';')
$readingClassName = false;
$readingClassName
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
switch ($matching)
case T_NAMESPACE:
$ns = new NamespaceBlock($classname);
$currentNS = new RawPropertyAccessor($ns);
$this->accessor->rawAddToValue
(
'namespaces',
$ns
);
$currentNS->setRawValue('file', $this->object);
break;
case T_AS:
case T_USE:
if (!$as)
$as = explode('\\', $classname);
$as = end($as);
$currentNS->rawAddToValue
'useStatements',
new UseStatement($classname, $as)
continue;
if ($matching === T_AS)
if ($token[0] === T_STRING)
$as .= $token[1];
elseif ($matching)
switch ($token[0])
case T_STRING:
case T_NS_SEPARATOR:
$classname .= $token[1];
$matching = T_AS;
else
$matching = $token[0];
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.