Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / CodeCleaner / ValidConstantPass.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\CodeCleaner;
13
14 use PhpParser\Node;
15 use PhpParser\Node\Expr;
16 use PhpParser\Node\Expr\ClassConstFetch;
17 use PhpParser\Node\Expr\ConstFetch;
18 use Psy\Exception\FatalErrorException;
19
20 /**
21  * Validate that namespaced constant references will succeed.
22  *
23  * This pass throws a FatalErrorException rather than letting PHP run
24  * headfirst into a real fatal error and die.
25  *
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
30  */
31 class ValidConstantPass extends NamespaceAwarePass
32 {
33     /**
34      * Validate that namespaced constant references will succeed.
35      *
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.
38      *
39      * @throws FatalErrorException if a constant reference is not defined
40      *
41      * @param Node $node
42      */
43     public function leaveNode(Node $node)
44     {
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());
50             }
51         } elseif ($node instanceof ClassConstFetch) {
52             $this->validateClassConstFetchExpression($node);
53         }
54     }
55
56     /**
57      * Validate a class constant fetch expression.
58      *
59      * @throws FatalErrorException if a class constant is not defined
60      *
61      * @param ClassConstFetch $stmt
62      */
63     protected function validateClassConstFetchExpression(ClassConstFetch $stmt)
64     {
65         // give the `class` pseudo-constant a pass
66         if ($stmt->name === 'class') {
67             return;
68         }
69
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);
73
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());
82                 }
83             }
84         }
85     }
86 }