annotate vendor/psy/psysh/src/Psy/CodeCleaner/PassableByReferencePass.php @ 0:4c8ae668cc8c

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