|
2 | 2 |
|
3 | 3 | namespace App\Parsers;
|
4 | 4 |
|
| 5 | +use App\Contexts\AbstractContext; |
| 6 | +use App\Contexts\MethodDefinition; |
5 | 7 | use Microsoft\PhpParser\Node\QualifiedName;
|
6 | 8 | use Microsoft\PhpParser\Node\Statement\FunctionDeclaration;
|
7 | 9 | use Microsoft\PhpParser\Token;
|
8 | 10 |
|
9 | 11 | class FunctionDeclarationParser extends AbstractParser
|
10 | 12 | {
|
11 |
| - use InitsNewContext; |
| 13 | + /** |
| 14 | + * @var MethodDefinition |
| 15 | + */ |
| 16 | + protected AbstractContext $context; |
12 | 17 |
|
13 | 18 | public function parse(FunctionDeclaration $node)
|
14 | 19 | {
|
15 |
| - $this->context->methodDefinition = array_map( |
16 |
| - fn (Token $part) => $part->getText($node->getRoot()->getFullText()), |
17 |
| - $node->getNameParts(), |
18 |
| - ); |
19 |
| - |
20 |
| - if ($node->parameters) { |
21 |
| - foreach ($node->parameters->getElements() as $element) { |
22 |
| - $param = [ |
23 |
| - 'types' => [], |
24 |
| - 'name' => $element->getName(), |
25 |
| - ]; |
26 |
| - |
27 |
| - if ($element->typeDeclarationList) { |
28 |
| - foreach ($element->typeDeclarationList->getValues() as $type) { |
29 |
| - if ($type instanceof Token) { |
30 |
| - $param['types'][] = $type->getText($node->getRoot()->getFullText()); |
31 |
| - } elseif ($type instanceof QualifiedName) { |
32 |
| - $param['types'][] = (string) $type->getResolvedName(); |
33 |
| - } else { |
34 |
| - $this->debug('unknown type', $type::class); |
35 |
| - } |
36 |
| - } |
37 |
| - } |
38 |
| - |
39 |
| - $this->context->methodDefinitionParams[] = $param; |
40 |
| - } |
41 |
| - } |
| 20 | + $this->context->methodName = collect($node->getNameParts())->map( |
| 21 | + fn(Token $part) => $part->getText($node->getRoot()->getFullText()) |
| 22 | + )->join('\\'); |
42 | 23 |
|
43 | 24 | return $this->context;
|
44 | 25 | }
|
| 26 | + |
| 27 | + public function initNewContext(): ?AbstractContext |
| 28 | + { |
| 29 | + return new MethodDefinition; |
| 30 | + } |
45 | 31 | }
|
0 commit comments