annotate vendor/psy/psysh/src/Psy/CodeCleaner/ValidConstantPass.php @ 7:848c88cfe644

More layout
author Chris Cannam
date Fri, 05 Jan 2018 13:59:44 +0000
parents 4c8ae668cc8c
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-2017 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 Psy\Exception\FatalErrorException;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Validate that namespaced constant references will succeed.
Chris@0 22 *
Chris@0 23 * This pass throws a FatalErrorException rather than letting PHP run
Chris@0 24 * headfirst into a real fatal error and die.
Chris@0 25 *
Chris@0 26 * @todo Detect constants defined in the current code snippet?
Chris@0 27 * ... Might not be worth it, since it would need to both be defining and
Chris@0 28 * referencing a namespaced constant, which doesn't seem like that big of
Chris@0 29 * a target for failure
Chris@0 30 */
Chris@0 31 class ValidConstantPass extends NamespaceAwarePass
Chris@0 32 {
Chris@0 33 /**
Chris@0 34 * Validate that namespaced constant references will succeed.
Chris@0 35 *
Chris@0 36 * Note that this does not (yet) detect constants defined in the current code
Chris@0 37 * snippet. It won't happen very often, so we'll punt for now.
Chris@0 38 *
Chris@0 39 * @throws FatalErrorException if a constant reference is not defined
Chris@0 40 *
Chris@0 41 * @param Node $node
Chris@0 42 */
Chris@0 43 public function leaveNode(Node $node)
Chris@0 44 {
Chris@0 45 if ($node instanceof ConstFetch && count($node->name->parts) > 1) {
Chris@0 46 $name = $this->getFullyQualifiedName($node->name);
Chris@0 47 if (!defined($name)) {
Chris@0 48 $msg = sprintf('Undefined constant %s', $name);
Chris@0 49 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
Chris@0 50 }
Chris@0 51 } elseif ($node instanceof ClassConstFetch) {
Chris@0 52 $this->validateClassConstFetchExpression($node);
Chris@0 53 }
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Validate a class constant fetch expression.
Chris@0 58 *
Chris@0 59 * @throws FatalErrorException if a class constant is not defined
Chris@0 60 *
Chris@0 61 * @param ClassConstFetch $stmt
Chris@0 62 */
Chris@0 63 protected function validateClassConstFetchExpression(ClassConstFetch $stmt)
Chris@0 64 {
Chris@0 65 // give the `class` pseudo-constant a pass
Chris@0 66 if ($stmt->name === 'class') {
Chris@0 67 return;
Chris@0 68 }
Chris@0 69
Chris@0 70 // if class name is an expression, give it a pass for now
Chris@0 71 if (!$stmt->class instanceof Expr) {
Chris@0 72 $className = $this->getFullyQualifiedName($stmt->class);
Chris@0 73
Chris@0 74 // if the class doesn't exist, don't throw an exception… it might be
Chris@0 75 // defined in the same line it's used or something stupid like that.
Chris@0 76 if (class_exists($className) || interface_exists($className)) {
Chris@0 77 $refl = new \ReflectionClass($className);
Chris@0 78 if (!$refl->hasConstant($stmt->name)) {
Chris@0 79 $constType = class_exists($className) ? 'Class' : 'Interface';
Chris@0 80 $msg = sprintf('%s constant \'%s::%s\' not found', $constType, $className, $stmt->name);
Chris@0 81 throw new FatalErrorException($msg, 0, E_ERROR, null, $stmt->getLine());
Chris@0 82 }
Chris@0 83 }
Chris@0 84 }
Chris@0 85 }
Chris@0 86 }