|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of PHP-CFG, a Control flow graph implementation for PHP |
| 5 | + * |
| 6 | + * @copyright 2015 Anthony Ferrara. All rights reserved |
| 7 | + * @license MIT See LICENSE at the root of the project for more info |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHPCfg\ParserHandler\Batch; |
| 11 | + |
| 12 | +use PHPCfg\Op; |
| 13 | +use PHPCfg\Operand; |
| 14 | +use PHPCfg\ParserHandler; |
| 15 | +use PHPCfg\ParserHandler\Batch; |
| 16 | +use PHPCfg\ParserHandler\Expr; |
| 17 | +use PHPCfg\ParserHandler\Stmt; |
| 18 | +use PhpParser\Node; |
| 19 | + |
| 20 | +class AssignAndForeach extends ParserHandler implements Expr, Stmt, Batch |
| 21 | +{ |
| 22 | + public function getExprSupport(): array |
| 23 | + { |
| 24 | + return ['Expr_Assign']; |
| 25 | + } |
| 26 | + |
| 27 | + public function getStmtSupport(): array |
| 28 | + { |
| 29 | + return ['Stmt_Foreach']; |
| 30 | + } |
| 31 | + |
| 32 | + public function handleStmt(Node\Stmt $node): void |
| 33 | + { |
| 34 | + // Foreach Implementation |
| 35 | + $attrs = $this->mapAttributes($node); |
| 36 | + $iterable = $this->parser->readVariable($this->parser->parseExprNode($node->expr)); |
| 37 | + $this->addOp(new Op\Iterator\Reset($iterable, $attrs)); |
| 38 | + |
| 39 | + $loopInit = $this->createBlockWithCatchTarget(); |
| 40 | + $loopBody = $this->createBlockWithCatchTarget(); |
| 41 | + $loopEnd = $this->createBlockWithCatchTarget(); |
| 42 | + |
| 43 | + $this->addOp(new Op\Stmt\Jump($loopInit, $attrs)); |
| 44 | + |
| 45 | + $this->block($loopInit); |
| 46 | + $result = $this->addExpr(new Op\Iterator\Valid($iterable, $attrs)); |
| 47 | + $this->addOp(new Op\Stmt\JumpIf($result, $loopBody, $loopEnd, $attrs)); |
| 48 | + |
| 49 | + $this->block($loopBody); |
| 50 | + |
| 51 | + if ($node->keyVar) { |
| 52 | + $this->addOp($keyOp = new Op\Iterator\Key($iterable, $attrs)); |
| 53 | + $this->addOp(new Op\Expr\Assign($this->parser->writeVariable($this->parser->parseExprNode($node->keyVar)), $keyOp->result, $attrs)); |
| 54 | + } |
| 55 | + |
| 56 | + $this->addOp($valueOp = new Op\Iterator\Value($iterable, $node->byRef, $attrs)); |
| 57 | + |
| 58 | + if ($node->valueVar instanceof Node\Expr\List_ || $node->valueVar instanceof Node\Expr\Array_) { |
| 59 | + $this->parseListAssignment($node->valueVar, $valueOp->result); |
| 60 | + } elseif ($node->byRef) { |
| 61 | + $this->addOp(new Op\Expr\AssignRef($this->parser->writeVariable($this->parser->parseExprNode($node->valueVar)), $valueOp->result, $attrs)); |
| 62 | + } else { |
| 63 | + $this->addOp(new Op\Expr\Assign($this->parser->writeVariable($this->parser->parseExprNode($node->valueVar)), $valueOp->result, $attrs)); |
| 64 | + } |
| 65 | + |
| 66 | + $this->block($this->parser->parseNodes($node->stmts, $this->block())); |
| 67 | + $this->addOp(new Op\Stmt\Jump($loopInit, $attrs)); |
| 68 | + |
| 69 | + $this->block($loopEnd); |
| 70 | + } |
| 71 | + |
| 72 | + public function handleExpr(Node\Expr $expr): Operand |
| 73 | + { |
| 74 | + $e = $this->parser->readVariable($this->parser->parseExprNode($expr->expr)); |
| 75 | + if ($expr->var instanceof Node\Expr\List_ || $expr->var instanceof Node\Expr\Array_) { |
| 76 | + $this->parseListAssignment($expr->var, $e); |
| 77 | + |
| 78 | + return $e; |
| 79 | + } |
| 80 | + $v = $this->parser->writeVariable($this->parser->parseExprNode($expr->var)); |
| 81 | + |
| 82 | + return $this->addExpr(new Op\Expr\Assign($v, $e, $this->mapAttributes($expr))); |
| 83 | + } |
| 84 | + |
| 85 | + protected function parseListAssignment(Node\Expr\List_|Node\Expr\Array_ $expr, Operand $rhs): void |
| 86 | + { |
| 87 | + foreach ($expr->items as $i => $item) { |
| 88 | + if (null === $item) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + if ($item->key === null) { |
| 93 | + $key = new Operand\Literal($i); |
| 94 | + } else { |
| 95 | + $key = $this->parser->readVariable($this->parser->parseExprNode($item->key)); |
| 96 | + } |
| 97 | + |
| 98 | + $var = $item->value; |
| 99 | + $result = $this->addExpr(new Op\Expr\ArrayDimFetch($rhs, $key, $this->mapAttributes($expr))); |
| 100 | + if ($var instanceof Node\Expr\List_ || $var instanceof Node\Expr\Array_) { |
| 101 | + $this->parseListAssignment($var, $result); |
| 102 | + |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + $this->addOp(new Op\Expr\Assign( |
| 107 | + $this->parser->writeVariable($this->parser->parseExprNode($var)), |
| 108 | + $result, |
| 109 | + $this->mapAttributes($expr), |
| 110 | + )); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments