annotate vendor/psy/psysh/src/CodeCleaner/PassableByReferencePass.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\Expr;
Chris@13 16 use PhpParser\Node\Expr\ClassConstFetch;
Chris@13 17 use PhpParser\Node\Expr\FuncCall;
Chris@13 18 use PhpParser\Node\Expr\MethodCall;
Chris@13 19 use PhpParser\Node\Expr\PropertyFetch;
Chris@13 20 use PhpParser\Node\Expr\StaticCall;
Chris@13 21 use PhpParser\Node\Expr\Variable;
Chris@13 22 use Psy\Exception\FatalErrorException;
Chris@13 23
Chris@13 24 /**
Chris@13 25 * Validate that only variables (and variable-like things) are passed by reference.
Chris@13 26 */
Chris@13 27 class PassableByReferencePass extends CodeCleanerPass
Chris@13 28 {
Chris@13 29 const EXCEPTION_MESSAGE = 'Only variables can be passed by reference';
Chris@13 30
Chris@13 31 /**
Chris@13 32 * @throws FatalErrorException if non-variables are passed by reference
Chris@13 33 *
Chris@13 34 * @param Node $node
Chris@13 35 */
Chris@13 36 public function enterNode(Node $node)
Chris@13 37 {
Chris@13 38 // @todo support MethodCall and StaticCall as well.
Chris@13 39 if ($node instanceof FuncCall) {
Chris@13 40 // if function name is an expression or a variable, give it a pass for now.
Chris@13 41 if ($node->name instanceof Expr || $node->name instanceof Variable) {
Chris@13 42 return;
Chris@13 43 }
Chris@13 44
Chris@13 45 $name = (string) $node->name;
Chris@13 46
Chris@13 47 if ($name === 'array_multisort') {
Chris@13 48 return $this->validateArrayMultisort($node);
Chris@13 49 }
Chris@13 50
Chris@13 51 try {
Chris@13 52 $refl = new \ReflectionFunction($name);
Chris@13 53 } catch (\ReflectionException $e) {
Chris@13 54 // Well, we gave it a shot!
Chris@13 55 return;
Chris@13 56 }
Chris@13 57
Chris@13 58 foreach ($refl->getParameters() as $key => $param) {
Chris@17 59 if (\array_key_exists($key, $node->args)) {
Chris@13 60 $arg = $node->args[$key];
Chris@13 61 if ($param->isPassedByReference() && !$this->isPassableByReference($arg)) {
Chris@13 62 throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine());
Chris@13 63 }
Chris@13 64 }
Chris@13 65 }
Chris@13 66 }
Chris@13 67 }
Chris@13 68
Chris@13 69 private function isPassableByReference(Node $arg)
Chris@13 70 {
Chris@13 71 // FuncCall, MethodCall and StaticCall are all PHP _warnings_ not fatal errors, so we'll let
Chris@13 72 // PHP handle those ones :)
Chris@13 73 return $arg->value instanceof ClassConstFetch ||
Chris@13 74 $arg->value instanceof PropertyFetch ||
Chris@13 75 $arg->value instanceof Variable ||
Chris@13 76 $arg->value instanceof FuncCall ||
Chris@13 77 $arg->value instanceof MethodCall ||
Chris@13 78 $arg->value instanceof StaticCall;
Chris@13 79 }
Chris@13 80
Chris@13 81 /**
Chris@13 82 * Because array_multisort has a problematic signature...
Chris@13 83 *
Chris@13 84 * The argument order is all sorts of wonky, and whether something is passed
Chris@13 85 * by reference or not depends on the values of the two arguments before it.
Chris@13 86 * We'll do a good faith attempt at validating this, but err on the side of
Chris@13 87 * permissive.
Chris@13 88 *
Chris@13 89 * This is why you don't design languages where core code and extensions can
Chris@13 90 * implement APIs that wouldn't be possible in userland code.
Chris@13 91 *
Chris@13 92 * @throws FatalErrorException for clearly invalid arguments
Chris@13 93 *
Chris@13 94 * @param Node $node
Chris@13 95 */
Chris@13 96 private function validateArrayMultisort(Node $node)
Chris@13 97 {
Chris@13 98 $nonPassable = 2; // start with 2 because the first one has to be passable by reference
Chris@13 99 foreach ($node->args as $arg) {
Chris@13 100 if ($this->isPassableByReference($arg)) {
Chris@13 101 $nonPassable = 0;
Chris@13 102 } elseif (++$nonPassable > 2) {
Chris@13 103 // There can be *at most* two non-passable-by-reference args in a row. This is about
Chris@13 104 // as close as we can get to validating the arguments for this function :-/
Chris@13 105 throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine());
Chris@13 106 }
Chris@13 107 }
Chris@13 108 }
Chris@13 109 }