4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\CodeCleaner;
15 use PhpParser\Node\Expr\ConstFetch;
16 use PhpParser\Node\Expr\FuncCall;
17 use PhpParser\Node\Name;
18 use PhpParser\Node\Stmt\Class_;
19 use PhpParser\Node\Stmt\Trait_;
20 use Psy\Exception\ErrorException;
23 * The called class pass throws warnings for get_class() and get_called_class()
24 * outside a class context.
26 class CalledClassPass extends CodeCleanerPass
33 public function beforeTraverse(array $nodes)
35 $this->inClass = false;
39 * @throws ErrorException if get_class or get_called_class is called without an object from outside a class
43 public function enterNode(Node $node)
45 if ($node instanceof Class_ || $node instanceof Trait_) {
46 $this->inClass = true;
47 } elseif ($node instanceof FuncCall && !$this->inClass) {
48 // We'll give any args at all (besides null) a pass.
49 // Technically we should be checking whether the args are objects, but this will do for now.
51 // @todo switch this to actually validate args when we get context-aware code cleaner passes.
52 if (!empty($node->args) && !$this->isNull($node->args[0])) {
56 // We'll ignore name expressions as well (things like `$foo()`)
57 if (!($node->name instanceof Name)) {
61 $name = strtolower($node->name);
62 if (in_array($name, array('get_class', 'get_called_class'))) {
63 $msg = sprintf('%s() called without object from outside a class', $name);
64 throw new ErrorException($msg, 0, E_USER_WARNING, null, $node->getLine());
72 public function leaveNode(Node $node)
74 if ($node instanceof Class_) {
75 $this->inClass = false;
79 private function isNull(Node $node)
81 return $node->value instanceof ConstFetch && strtolower($node->value->name) === 'null';