Chris@13: isRequireNode($origNode)) { Chris@13: return; Chris@13: } Chris@13: Chris@13: $node = clone $origNode; Chris@13: Chris@13: /* Chris@13: * rewrite Chris@13: * Chris@13: * $foo = require $bar Chris@13: * Chris@13: * to Chris@13: * Chris@13: * $foo = require \Psy\CodeCleaner\RequirePass::resolve($bar) Chris@13: */ Chris@13: $node->expr = new StaticCall( Chris@13: new FullyQualifiedName('Psy\CodeCleaner\RequirePass'), Chris@13: 'resolve', Chris@13: [new Arg($origNode->expr), new Arg(new LNumber($origNode->getLine()))], Chris@13: $origNode->getAttributes() Chris@13: ); Chris@13: Chris@13: return $node; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Runtime validation that $file can be resolved as an include path. Chris@13: * Chris@13: * If $file can be resolved, return $file. Otherwise throw a fatal error exception. Chris@13: * Chris@13: * @throws FatalErrorException when unable to resolve include path for $file Chris@13: * @throws ErrorException if $file is empty and E_WARNING is included in error_reporting level Chris@13: * Chris@13: * @param string $file Chris@13: * @param int $lineNumber Line number of the original require expression Chris@13: * Chris@13: * @return string Exactly the same as $file Chris@13: */ Chris@13: public static function resolve($file, $lineNumber = null) Chris@13: { Chris@13: $file = (string) $file; Chris@13: Chris@13: if ($file === '') { Chris@13: // @todo Shell::handleError would be better here, because we could Chris@13: // fake the file and line number, but we can't call it statically. Chris@13: // So we're duplicating some of the logics here. Chris@17: if (E_WARNING & \error_reporting()) { Chris@13: ErrorException::throwException(E_WARNING, 'Filename cannot be empty', null, $lineNumber); Chris@13: } Chris@17: // @todo trigger an error as fallback? this is pretty ugly… Chris@17: // trigger_error('Filename cannot be empty', E_USER_WARNING); Chris@13: } Chris@13: Chris@17: if ($file === '' || !\stream_resolve_include_path($file)) { Chris@17: $msg = \sprintf("Failed opening required '%s'", $file); Chris@13: throw new FatalErrorException($msg, 0, E_ERROR, null, $lineNumber); Chris@13: } Chris@13: Chris@13: return $file; Chris@13: } Chris@13: Chris@13: private function isRequireNode(Node $node) Chris@13: { Chris@17: return $node instanceof Include_ && \in_array($node->type, self::$requireTypes); Chris@13: } Chris@13: }