1
|
|
|
<?php |
2
|
|
|
namespace gossi\codegen\parser\visitor\parts; |
3
|
|
|
|
4
|
|
|
use gossi\codegen\model\AbstractPhpStruct; |
5
|
|
|
use PhpParser\Node\Stmt\ClassLike; |
6
|
|
|
use PhpParser\Node\Stmt\Namespace_; |
7
|
|
|
use PhpParser\Node\Stmt\TraitUse; |
8
|
|
|
use PhpParser\Node\Stmt\UseUse; |
9
|
|
|
|
10
|
|
|
trait StructParserPart { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @return AbstractPhpStruct |
14
|
|
|
*/ |
15
|
|
|
abstract protected function getStruct(); |
16
|
|
|
|
17
|
15 |
|
public function visitStruct(ClassLike $node) { |
18
|
15 |
|
$struct = $this->getStruct(); |
19
|
15 |
|
$struct->setName($node->name->name); |
20
|
|
|
|
21
|
15 |
|
if (($doc = $node->getDocComment()) !== null) { |
22
|
4 |
|
$struct->setDocblock($doc->getReformattedText()); |
23
|
4 |
|
$docblock = $struct->getDocblock(); |
24
|
4 |
|
$struct->setDescription($docblock->getShortDescription()); |
25
|
4 |
|
$struct->setLongDescription($docblock->getLongDescription()); |
26
|
|
|
} |
27
|
15 |
|
} |
28
|
|
|
|
29
|
2 |
|
public function visitTraitUse(TraitUse $node) { |
30
|
2 |
|
$struct = $this->getStruct(); |
31
|
|
|
|
32
|
2 |
|
foreach ($node->traits as $trait) { |
33
|
2 |
|
$struct->addTrait(implode('\\', $trait->parts)); |
34
|
|
|
} |
35
|
2 |
|
} |
36
|
|
|
|
37
|
9 |
|
public function visitNamespace(Namespace_ $node) { |
38
|
9 |
|
if ($node->name !== null) { |
39
|
9 |
|
$this->getStruct()->setNamespace(implode('\\', $node->name->parts)); |
40
|
|
|
} |
41
|
9 |
|
} |
42
|
|
|
|
43
|
1 |
|
public function visitUseStatement(UseUse $node) { |
44
|
1 |
|
$name = implode('\\', $node->name->parts); |
45
|
1 |
|
$alias = !empty($node->alias) ? $node->alias : null; |
46
|
|
|
|
47
|
1 |
|
$this->getStruct()->addUseStatement($name, $alias); |
|
|
|
|
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.