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;
16 use PhpParser\Node\Expr\ClassConstFetch;
17 use PhpParser\Node\Expr\ConstFetch;
18 use Psy\Exception\FatalErrorException;
21 * Validate that namespaced constant references will succeed.
23 * This pass throws a FatalErrorException rather than letting PHP run
24 * headfirst into a real fatal error and die.
26 * @todo Detect constants defined in the current code snippet?
27 * ... Might not be worth it, since it would need to both be defining and
28 * referencing a namespaced constant, which doesn't seem like that big of
29 * a target for failure
31 class ValidConstantPass extends NamespaceAwarePass
34 * Validate that namespaced constant references will succeed.
36 * Note that this does not (yet) detect constants defined in the current code
37 * snippet. It won't happen very often, so we'll punt for now.
39 * @throws FatalErrorException if a constant reference is not defined
43 public function leaveNode(Node $node)
45 if ($node instanceof ConstFetch && count($node->name->parts) > 1) {
46 $name = $this->getFullyQualifiedName($node->name);
47 if (!defined($name)) {
48 $msg = sprintf('Undefined constant %s', $name);
49 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
51 } elseif ($node instanceof ClassConstFetch) {
52 $this->validateClassConstFetchExpression($node);
57 * Validate a class constant fetch expression.
59 * @throws FatalErrorException if a class constant is not defined
61 * @param ClassConstFetch $stmt
63 protected function validateClassConstFetchExpression(ClassConstFetch $stmt)
65 // give the `class` pseudo-constant a pass
66 if ($stmt->name === 'class') {
70 // if class name is an expression, give it a pass for now
71 if (!$stmt->class instanceof Expr) {
72 $className = $this->getFullyQualifiedName($stmt->class);
74 // if the class doesn't exist, don't throw an exception… it might be
75 // defined in the same line it's used or something stupid like that.
76 if (class_exists($className) || interface_exists($className)) {
77 $refl = new \ReflectionClass($className);
78 if (!$refl->hasConstant($stmt->name)) {
79 $constType = class_exists($className) ? 'Class' : 'Interface';
80 $msg = sprintf('%s constant \'%s::%s\' not found', $constType, $className, $stmt->name);
81 throw new FatalErrorException($msg, 0, E_ERROR, null, $stmt->getLine());