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