Mercurial > hg > isophonics-drupal-site
annotate vendor/psy/psysh/src/CodeCleaner/FunctionContextPass.php @ 19:fa3358dc1485 tip
Add ndrum files
author | Chris Cannam |
---|---|
date | Wed, 28 Aug 2019 13:14:47 +0100 |
parents | 5fb285c0d0e3 |
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\Yield_; |
Chris@13 | 16 use PhpParser\Node\FunctionLike; |
Chris@13 | 17 use Psy\Exception\FatalErrorException; |
Chris@13 | 18 |
Chris@13 | 19 class FunctionContextPass extends CodeCleanerPass |
Chris@13 | 20 { |
Chris@13 | 21 /** @var int */ |
Chris@13 | 22 private $functionDepth; |
Chris@13 | 23 |
Chris@13 | 24 /** |
Chris@13 | 25 * @param array $nodes |
Chris@13 | 26 */ |
Chris@13 | 27 public function beforeTraverse(array $nodes) |
Chris@13 | 28 { |
Chris@13 | 29 $this->functionDepth = 0; |
Chris@13 | 30 } |
Chris@13 | 31 |
Chris@13 | 32 public function enterNode(Node $node) |
Chris@13 | 33 { |
Chris@13 | 34 if ($node instanceof FunctionLike) { |
Chris@13 | 35 $this->functionDepth++; |
Chris@13 | 36 |
Chris@13 | 37 return; |
Chris@13 | 38 } |
Chris@13 | 39 |
Chris@13 | 40 // node is inside function context |
Chris@13 | 41 if ($this->functionDepth !== 0) { |
Chris@13 | 42 return; |
Chris@13 | 43 } |
Chris@13 | 44 |
Chris@13 | 45 // It causes fatal error. |
Chris@13 | 46 if ($node instanceof Yield_) { |
Chris@13 | 47 $msg = 'The "yield" expression can only be used inside a function'; |
Chris@13 | 48 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine()); |
Chris@13 | 49 } |
Chris@13 | 50 } |
Chris@13 | 51 |
Chris@13 | 52 /** |
Chris@13 | 53 * @param \PhpParser\Node $node |
Chris@13 | 54 */ |
Chris@13 | 55 public function leaveNode(Node $node) |
Chris@13 | 56 { |
Chris@13 | 57 if ($node instanceof FunctionLike) { |
Chris@13 | 58 $this->functionDepth--; |
Chris@13 | 59 } |
Chris@13 | 60 } |
Chris@13 | 61 } |