0e8056116340257f59c7584dc78ac384bcc29e92
[yaffs-website] / vendor / psy / psysh / src / CodeCleaner / RequirePass.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 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\Arg;
16 use PhpParser\Node\Expr\Include_;
17 use PhpParser\Node\Expr\StaticCall;
18 use PhpParser\Node\Name\FullyQualified as FullyQualifiedName;
19 use PhpParser\Node\Scalar\LNumber;
20 use Psy\Exception\ErrorException;
21 use Psy\Exception\FatalErrorException;
22 use Psy\Shell;
23
24 /**
25  * Add runtime validation for `require` and `require_once` calls.
26  */
27 class RequirePass extends CodeCleanerPass
28 {
29     private static $requireTypes = [Include_::TYPE_REQUIRE, Include_::TYPE_REQUIRE_ONCE];
30
31     /**
32      * {@inheritdoc}
33      */
34     public function enterNode(Node $origNode)
35     {
36         if (!$this->isRequireNode($origNode)) {
37             return;
38         }
39
40         $node = clone $origNode;
41
42         /*
43          * rewrite
44          *
45          *   $foo = require $bar
46          *
47          * to
48          *
49          *   $foo = require \Psy\CodeCleaner\RequirePass::resolve($bar)
50          */
51         $node->expr = new StaticCall(
52             new FullyQualifiedName('Psy\CodeCleaner\RequirePass'),
53             'resolve',
54             [new Arg($origNode->expr), new Arg(new LNumber($origNode->getLine()))],
55             $origNode->getAttributes()
56         );
57
58         return $node;
59     }
60
61     /**
62      * Runtime validation that $file can be resolved as an include path.
63      *
64      * If $file can be resolved, return $file. Otherwise throw a fatal error exception.
65      *
66      * @throws FatalErrorException when unable to resolve include path for $file
67      * @throws ErrorException      if $file is empty and E_WARNING is included in error_reporting level
68      *
69      * @param string $file
70      * @param int    $lineNumber Line number of the original require expression
71      *
72      * @return string Exactly the same as $file
73      */
74     public static function resolve($file, $lineNumber = null)
75     {
76         $file = (string) $file;
77
78         if ($file === '') {
79             // @todo Shell::handleError would be better here, because we could
80             // fake the file and line number, but we can't call it statically.
81             // So we're duplicating some of the logics here.
82             if (E_WARNING & error_reporting()) {
83                 ErrorException::throwException(E_WARNING, 'Filename cannot be empty', null, $lineNumber);
84             } else {
85                 // @todo trigger an error as fallback? this is pretty ugly…
86                 // trigger_error('Filename cannot be empty', E_USER_WARNING);
87             }
88         }
89
90         if ($file === '' || !stream_resolve_include_path($file)) {
91             $msg = sprintf("Failed opening required '%s'", $file);
92             throw new FatalErrorException($msg, 0, E_ERROR, null, $lineNumber);
93         }
94
95         return $file;
96     }
97
98     private function isRequireNode(Node $node)
99     {
100         return $node instanceof Include_ && in_array($node->type, self::$requireTypes);
101     }
102 }