Completed
Push — develop ( 763ea8...df26dc )
by Paul
01:58
created

AttributeNodeParser::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 17
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\Model\PropertyInterface\NodeInterface;
9
use PhpUnitGen\Model\PropertyInterface\VisibilityInterface;
10
11
/**
12
 * Class AttributeNodeParser.
13
 *
14
 * @author     Paul Thébaud <[email protected]>.
15
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
16
 * @license    https://opensource.org/licenses/MIT The MIT license.
17
 * @link       https://github.com/paul-thebaud/phpunit-generator
18
 * @since      Class available since Release 2.0.0.
19
 */
20
class AttributeNodeParser extends AbstractNodeParser
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function parse(Node $node, NodeInterface $parent): NodeInterface
26
    {
27
        /**
28
         * Overriding variable types.
29
         * @var Node\Stmt\Property  $node   The property node to parse.
30
         * @var TraitModelInterface $parent The node which contains this namespace.
31
         */
32
        foreach ($node->props as $property) {
0 ignored issues
show
Bug introduced by
Accessing props on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
33
            $attribute = new AttributeModel();
34
            $attribute->setName($property->name);
35
            $attribute->setIsStatic($node->isStatic());
36
            $attribute->setVisibility($this->retrieveVisibility($node));
37
38
            $parent->addAttribute($attribute);
39
        }
40
41
        return $parent;
42
    }
43
44
    /**
45
     * Retrieve the visibility of this property.
46
     *
47
     * @param Node\Stmt\Property $property The property.
48
     *
49
     * @return int The visibility as an integer.
50
     */
51
    private function retrieveVisibility(Node\Stmt\Property $property): int
52
    {
53
        if ($property->isPrivate()) {
54
            return VisibilityInterface::PRIVATE;
55
        }
56
        if ($property->isProtected()) {
57
            return VisibilityInterface::PROTECTED;
58
        }
59
        return VisibilityInterface::PUBLIC;
60
    }
61
}
62