annotate vendor/psy/psysh/src/CodeCleaner/ValidConstantPass.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@13 1 <?php
Chris@13 2
Chris@13 3 /*
Chris@13 4 * This file is part of Psy Shell.
Chris@13 5 *
Chris@13 6 * (c) 2012-2018 Justin Hileman
Chris@13 7 *
Chris@13 8 * For the full copyright and license information, please view the LICENSE
Chris@13 9 * file that was distributed with this source code.
Chris@13 10 */
Chris@13 11
Chris@13 12 namespace Psy\CodeCleaner;
Chris@13 13
Chris@13 14 use PhpParser\Node;
Chris@13 15 use PhpParser\Node\Expr;
Chris@13 16 use PhpParser\Node\Expr\ClassConstFetch;
Chris@13 17 use PhpParser\Node\Expr\ConstFetch;
Chris@13 18 use PhpParser\Node\Identifier;
Chris@13 19 use Psy\Exception\FatalErrorException;
Chris@13 20
Chris@13 21 /**
Chris@13 22 * Validate that namespaced constant references will succeed.
Chris@13 23 *
Chris@13 24 * This pass throws a FatalErrorException rather than letting PHP run
Chris@13 25 * headfirst into a real fatal error and die.
Chris@13 26 *
Chris@13 27 * @todo Detect constants defined in the current code snippet?
Chris@13 28 * ... Might not be worth it, since it would need to both be defining and
Chris@13 29 * referencing a namespaced constant, which doesn't seem like that big of
Chris@13 30 * a target for failure
Chris@13 31 */
Chris@13 32 class ValidConstantPass extends NamespaceAwarePass
Chris@13 33 {
Chris@13 34 /**
Chris@13 35 * Validate that namespaced constant references will succeed.
Chris@13 36 *
Chris@13 37 * Note that this does not (yet) detect constants defined in the current code
Chris@13 38 * snippet. It won't happen very often, so we'll punt for now.
Chris@13 39 *
Chris@13 40 * @throws FatalErrorException if a constant reference is not defined
Chris@13 41 *
Chris@13 42 * @param Node $node
Chris@13 43 */
Chris@13 44 public function leaveNode(Node $node)
Chris@13 45 {
Chris@17 46 if ($node instanceof ConstFetch && \count($node->name->parts) > 1) {
Chris@13 47 $name = $this->getFullyQualifiedName($node->name);
Chris@17 48 if (!\defined($name)) {
Chris@17 49 $msg = \sprintf('Undefined constant %s', $name);
Chris@13 50 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
Chris@13 51 }
Chris@13 52 } elseif ($node instanceof ClassConstFetch) {
Chris@13 53 $this->validateClassConstFetchExpression($node);
Chris@13 54 }
Chris@13 55 }
Chris@13 56
Chris@13 57 /**
Chris@13 58 * Validate a class constant fetch expression.
Chris@13 59 *
Chris@13 60 * @throws FatalErrorException if a class constant is not defined
Chris@13 61 *
Chris@13 62 * @param ClassConstFetch $stmt
Chris@13 63 */
Chris@13 64 protected function validateClassConstFetchExpression(ClassConstFetch $stmt)
Chris@13 65 {
Chris@13 66 // For PHP Parser 4.x
Chris@13 67 $constName = $stmt->name instanceof Identifier ? $stmt->name->toString() : $stmt->name;
Chris@13 68
Chris@13 69 // give the `class` pseudo-constant a pass
Chris@13 70 if ($constName === 'class') {
Chris@13 71 return;
Chris@13 72 }
Chris@13 73
Chris@13 74 // if class name is an expression, give it a pass for now
Chris@13 75 if (!$stmt->class instanceof Expr) {
Chris@13 76 $className = $this->getFullyQualifiedName($stmt->class);
Chris@13 77
Chris@13 78 // if the class doesn't exist, don't throw an exception… it might be
Chris@13 79 // defined in the same line it's used or something stupid like that.
Chris@17 80 if (\class_exists($className) || \interface_exists($className)) {
Chris@13 81 $refl = new \ReflectionClass($className);
Chris@13 82 if (!$refl->hasConstant($constName)) {
Chris@17 83 $constType = \class_exists($className) ? 'Class' : 'Interface';
Chris@17 84 $msg = \sprintf('%s constant \'%s::%s\' not found', $constType, $className, $constName);
Chris@13 85 throw new FatalErrorException($msg, 0, E_ERROR, null, $stmt->getLine());
Chris@13 86 }
Chris@13 87 }
Chris@13 88 }
Chris@13 89 }
Chris@13 90 }