|
| 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\Stmt; |
| 11 | + |
| 12 | +use PHPCfg\Func; |
| 13 | +use PHPCfg\Op; |
| 14 | +use PHPCfg\ParserHandler; |
| 15 | +use PhpParser\Modifiers; |
| 16 | +use PhpParser\Node\Stmt; |
| 17 | +use RuntimeException; |
| 18 | + |
| 19 | +class ClassMethod extends ParserHandler |
| 20 | +{ |
| 21 | + public function handleStmt(Stmt $node): void |
| 22 | + { |
| 23 | + if (! $this->parser->currentClass instanceof Op\Type\Literal) { |
| 24 | + throw new RuntimeException('Unknown current class'); |
| 25 | + } |
| 26 | + |
| 27 | + $this->parser->script->functions[] = $func = new Func( |
| 28 | + $node->name->toString(), |
| 29 | + $node->flags | ($node->byRef ? Func::FLAG_RETURNS_REF : 0), |
| 30 | + $this->parser->parseTypeNode($node->returnType), |
| 31 | + $this->parser->currentClass, |
| 32 | + ); |
| 33 | + |
| 34 | + if ($node->stmts !== null) { |
| 35 | + $this->parser->parseFunc($func, $node->params, $node->stmts, null); |
| 36 | + } else { |
| 37 | + $func->params = $this->parser->parseParameterList($func, $node->params); |
| 38 | + $func->cfg = null; |
| 39 | + } |
| 40 | + |
| 41 | + $visibility = $node->flags & Modifiers::VISIBILITY_MASK; |
| 42 | + $static = $node->flags & Modifiers::STATIC; |
| 43 | + $final = $node->flags & Modifiers::FINAL; |
| 44 | + $abstract = $node->flags & Modifiers::ABSTRACT; |
| 45 | + |
| 46 | + $this->addOp($class_method = new Op\Stmt\ClassMethod( |
| 47 | + $func, |
| 48 | + $visibility, |
| 49 | + (bool) $static, |
| 50 | + (bool) $final, |
| 51 | + (bool) $abstract, |
| 52 | + $this->parser->parseAttributeGroups($node->attrGroups), |
| 53 | + $this->mapAttributes($node), |
| 54 | + )); |
| 55 | + $func->callableOp = $class_method; |
| 56 | + } |
| 57 | +} |
0 commit comments