annotate vendor/psy/psysh/src/CodeCleaner/ValidConstantPass.php @ 5:12f9dff5fda9 tip

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