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

TraitNodeParser::invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 11
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\ModelInterface\PhpFileModelInterface;
7
use PhpUnitGen\Model\TraitModel;
8
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\AttributeNodeParserInterface;
9
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\MethodNodeParserInterface;
10
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\TraitNodeParserInterface;
11
12
/**
13
 * Class TraitNodeParser.
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 TraitNodeParser extends AbstractNodeParser implements TraitNodeParserInterface
22
{
23
    /**
24
     * TraitNodeParser constructor.
25
     *
26
     * @param MethodNodeParserInterface    $methodNodeParser    The method node parser to use.
27
     * @param AttributeNodeParserInterface $attributeNodeParser The attribute node parser to use.
28
     */
29
    public function __construct(
30
        MethodNodeParserInterface $methodNodeParser,
31
        AttributeNodeParserInterface $attributeNodeParser
32
    ) {
33
        $this->nodeParsers[Node\Stmt\ClassMethod::class] = $methodNodeParser;
34
        $this->nodeParsers[Node\Stmt\Property::class]    = $attributeNodeParser;
35
    }
36
37
    /**
38
     * Parse a node to update the parent node model.
39
     *
40
     * @param Node\Stmt\Trait_      $node   The node to parse.
41
     * @param PhpFileModelInterface $parent The parent node.
42
     *
43
     * @return PhpFileModelInterface The updated parent.
44
     */
45
    public function invoke(Node\Stmt\Trait_ $node, PhpFileModelInterface $parent): PhpFileModelInterface
46
    {
47
        $trait = new TraitModel();
48
        $trait->setParentNode($parent);
49
        $trait->setName($node->name);
0 ignored issues
show
Bug introduced by
It seems like $node->name can also be of type null; however, parameter $name of PhpUnitGen\Model\InterfaceModel::setName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        $trait->setName(/** @scrutinizer ignore-type */ $node->name);
Loading history...
50
51
        $trait = $this->parseSubNodes($node->stmts, $trait);
52
53
        $parent->addTrait($trait);
54
55
        return $parent;
56
    }
57
}