Completed
Push — develop ( 34db25...1b1cc6 )
by Paul
02:36
created

AttributeNodeParser::invoke()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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