Passed
Push — develop ( fb709e...b69568 )
by Paul
03:14
created

AttributeNodeParser::invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 2
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Parser\NodeParser;
4
5
use PhpParser\Node;
6
use PhpUnitGen\Model\AttributeModel;
7
use PhpUnitGen\Model\ModelInterface\TraitModelInterface;
8
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\AttributeNodeParserInterface;
9
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\ValueNodeParserInterface;
10
use PhpUnitGen\Parser\NodeParserUtil\AttributeVisibilityTrait;
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 implements AttributeNodeParserInterface
22
{
23
    use AttributeVisibilityTrait;
24
25
    /**
26
     * @var ValueNodeParserInterface $valueNodeParser The value node parser.
27
     */
28
    protected $valueNodeParser;
29
30
    /**
31
     * AttributeNodeParser constructor.
32
     *
33
     * @param ValueNodeParserInterface $valueNodeParser The value node parser.
34
     */
35
    public function __construct(ValueNodeParserInterface $valueNodeParser)
36
    {
37
        $this->valueNodeParser = $valueNodeParser;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function invoke(Node\Stmt\Property $node, TraitModelInterface $parent): TraitModelInterface
44
    {
45
        $isStatic = $node->isStatic();
46
        $visibility = $this->getPropertyVisibility($node);
47
48
        foreach ($node->props as $property) {
49
            $attribute = new AttributeModel();
50
            $attribute->setParentNode($parent);
51
            $attribute->setName($property->name);
52
            $attribute->setIsStatic($isStatic);
53
            $attribute->setVisibility($visibility);
54
55
            if ($property->default !== null) {
56
                $attribute = $this->valueNodeParser->invoke($property->default, $attribute);
57
            }
58
59
            $parent->addAttribute($attribute);
60
        }
61
62
        return $parent;
63
    }
64
}
65