1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of PhpUnitGen. |
5
|
|
|
* |
6
|
|
|
* (c) 2017-2018 Paul Thébaud <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PhpUnitGen\Parser\NodeParser; |
13
|
|
|
|
14
|
|
|
use PhpParser\Node; |
15
|
|
|
use PhpUnitGen\Exception\Exception; |
16
|
|
|
use PhpUnitGen\Model\AttributeModel; |
17
|
|
|
use PhpUnitGen\Model\ModelInterface\TraitModelInterface; |
18
|
|
|
use PhpUnitGen\Model\PropertyInterface\NodeInterface; |
19
|
|
|
use PhpUnitGen\Parser\NodeParserUtil\AttributeVisibilityHelper; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class AttributeNodeParser. |
23
|
|
|
* |
24
|
|
|
* @author Paul Thébaud <[email protected]>. |
25
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
26
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
27
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
28
|
|
|
* @since Class available since Release 2.0.0. |
29
|
|
|
*/ |
30
|
|
|
class AttributeNodeParser extends AbstractNodeParser |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var ValueNodeParser $valueNodeParser The value node parser. |
34
|
|
|
*/ |
35
|
|
|
protected $valueNodeParser; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* AttributeNodeParser constructor. |
39
|
|
|
* |
40
|
|
|
* @param ValueNodeParser $valueNodeParser The value node parser. |
41
|
|
|
*/ |
42
|
|
|
public function __construct(ValueNodeParser $valueNodeParser) |
43
|
|
|
{ |
44
|
|
|
$this->valueNodeParser = $valueNodeParser; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Parse a node to update the parent node model. |
49
|
|
|
* |
50
|
|
|
* @param mixed $node The node to parse. |
51
|
|
|
* @param NodeInterface $parent The parent node. |
52
|
|
|
*/ |
53
|
|
|
public function invoke($node, NodeInterface $parent): void |
54
|
|
|
{ |
55
|
|
|
if (! $node instanceof Node\Stmt\Property || ! $parent instanceof TraitModelInterface) { |
56
|
|
|
throw new Exception('AttributeNodeParser is made to parse a property node'); |
57
|
|
|
} |
58
|
|
|
$isStatic = $node->isStatic(); |
59
|
|
|
$visibility = AttributeVisibilityHelper::getVisibility($node); |
60
|
|
|
|
61
|
|
|
foreach ($node->props as $property) { |
62
|
|
|
$attribute = new AttributeModel(); |
63
|
|
|
$attribute->setParentNode($parent); |
64
|
|
|
$attribute->setName($property->name); |
65
|
|
|
$attribute->setIsStatic($isStatic); |
66
|
|
|
$attribute->setVisibility($visibility); |
67
|
|
|
|
68
|
|
|
if ($property->default !== null) { |
69
|
|
|
$this->valueNodeParser->invoke($property->default, $attribute); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$parent->addAttribute($attribute); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|