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

ClassNodeParser::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
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\ClassModel;
7
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface;
8
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\AttributeNodeParserInterface;
9
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\ClassNodeParserInterface;
10
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\MethodNodeParserInterface;
11
12
/**
13
 * Class ClassNodeParser.
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 ClassNodeParser extends AbstractNodeParser implements ClassNodeParserInterface
22
{
23
    /**
24
     * ClassNodeParser 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\Class_      $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\Class_ $node, PhpFileModelInterface $parent): PhpFileModelInterface
46
    {
47
        $class = new ClassModel();
48
        $class->setParentNode($parent);
49
        $class->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
        $class->setName(/** @scrutinizer ignore-type */ $node->name);
Loading history...
50
        $class->setIsAbstract($node->isAbstract());
51
        $class->setIsFinal($node->isFinal());
52
53
        $class = $this->parseSubNodes($node->stmts, $class);
54
55
        $parent->addTrait($class);
56
57
        return $parent;
58
    }
59
}