annotate vendor/psy/psysh/src/CodeCleaner/RequirePass.php @ 19:fa3358dc1485 tip

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