comparison vendor/psy/psysh/src/CodeCleaner/ListPass.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
10 */ 10 */
11 11
12 namespace Psy\CodeCleaner; 12 namespace Psy\CodeCleaner;
13 13
14 use PhpParser\Node; 14 use PhpParser\Node;
15 use PhpParser\Node\Expr;
15 use PhpParser\Node\Expr\Array_; 16 use PhpParser\Node\Expr\Array_;
17 use PhpParser\Node\Expr\ArrayDimFetch;
16 use PhpParser\Node\Expr\ArrayItem; 18 use PhpParser\Node\Expr\ArrayItem;
17 use PhpParser\Node\Expr\Assign; 19 use PhpParser\Node\Expr\Assign;
20 use PhpParser\Node\Expr\FuncCall;
18 use PhpParser\Node\Expr\List_; 21 use PhpParser\Node\Expr\List_;
22 use PhpParser\Node\Expr\MethodCall;
23 use PhpParser\Node\Expr\PropertyFetch;
19 use PhpParser\Node\Expr\Variable; 24 use PhpParser\Node\Expr\Variable;
20 use Psy\Exception\ParseErrorException; 25 use Psy\Exception\ParseErrorException;
21 26
22 /** 27 /**
23 * Validate that the list assignment. 28 * Validate that the list assignment.
26 { 31 {
27 private $atLeastPhp71; 32 private $atLeastPhp71;
28 33
29 public function __construct() 34 public function __construct()
30 { 35 {
31 $this->atLeastPhp71 = version_compare(PHP_VERSION, '7.1', '>='); 36 $this->atLeastPhp71 = \version_compare(PHP_VERSION, '7.1', '>=');
32 } 37 }
33 38
34 /** 39 /**
35 * Validate use of list assignment. 40 * Validate use of list assignment.
36 * 41 *
72 if (!$this->atLeastPhp71 && $item instanceof ArrayItem && $item->key !== null) { 77 if (!$this->atLeastPhp71 && $item instanceof ArrayItem && $item->key !== null) {
73 $msg = 'Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting \',\' or \')\''; 78 $msg = 'Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting \',\' or \')\'';
74 throw new ParseErrorException($msg, $item->key->getLine()); 79 throw new ParseErrorException($msg, $item->key->getLine());
75 } 80 }
76 81
77 $value = ($item instanceof ArrayItem) ? $item->value : $item; 82 if (!self::isValidArrayItem($item)) {
78
79 if (!$value instanceof Variable) {
80 $msg = 'Assignments can only happen to writable values'; 83 $msg = 'Assignments can only happen to writable values';
81 throw new ParseErrorException($msg, $item->getLine()); 84 throw new ParseErrorException($msg, $item->getLine());
82 } 85 }
83 } 86 }
84 87
85 if (!$itemFound) { 88 if (!$itemFound) {
86 throw new ParseErrorException('Cannot use empty list'); 89 throw new ParseErrorException('Cannot use empty list');
87 } 90 }
88 } 91 }
92
93 /**
94 * Validate whether a given item in an array is valid for short assignment.
95 *
96 * @param Expr $item
97 *
98 * @return bool
99 */
100 private static function isValidArrayItem(Expr $item)
101 {
102 $value = ($item instanceof ArrayItem) ? $item->value : $item;
103
104 while ($value instanceof ArrayDimFetch || $value instanceof PropertyFetch) {
105 $value = $value->var;
106 }
107
108 // We just kind of give up if it's a method call. We can't tell if it's
109 // valid via static analysis.
110 return $value instanceof Variable || $value instanceof MethodCall || $value instanceof FuncCall;
111 }
89 } 112 }