for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Ray\Aop;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\NodeVisitorAbstract;
use Ray\Aop\Exception\MultipleClassInOneFileException;
final class CodeVisitor extends NodeVisitorAbstract
{
/** @var ?Namespace_ */
public $namespace;
/** @var Declare_[] */
public $declare = [];
/** @var Use_[] */
public $use = [];
/** @var Class_|null */
public $class;
/** @var ClassMethod[] */
public $classMethod = [];
/**
* @return null
*/
public function enterNode(Node $node)
if ($node instanceof Declare_) {
$this->declare[] = $node;
return null;
}
if ($node instanceof Use_) {
$this->use[] = $node;
if ($node instanceof Namespace_) {
$this->namespace = $node;
return $this->enterNodeClass($node);
$this->enterNodeClass($node)
Ray\Aop\CodeVisitor::enterNodeClass()
This check looks for function or method calls that always return null and whose return value is used.
class A { function getObject() { return null; } } $a = new A(); if ($a->getObject()) {
The method getObject() can return nothing but null, so it makes no sense to use the return value.
getObject()
The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.
private function validateClass(Class_ $class): void
$isClassAlreadyDeclared = $this->class instanceof Class_;
if ($isClassAlreadyDeclared) {
$name = $class->name instanceof Node\Identifier ? $class->name->name : '';
throw new MultipleClassInOneFileException($name);
private function enterNodeClass(Node $node)
if ($node instanceof Class_) {
$this->validateClass($node);
$this->class = $node;
if ($node instanceof ClassMethod) {
$this->classMethod[] = $node;
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.