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

InterfaceNodeParser::invoke()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 2
dl 0
loc 19
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\Configuration\ConfigurationInterface\ConfigInterface;
7
use PhpUnitGen\Exception\AnnotationParseException;
8
use PhpUnitGen\Exception\Exception;
9
use PhpUnitGen\Model\InterfaceModel;
10
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface;
11
use PhpUnitGen\Model\PropertyInterface\NodeInterface;
12
use PhpUnitGen\Parser\NodeParserUtil\ClassLikeNameHelper;
13
14
/**
15
 * Class InterfaceNodeParser.
16
 *
17
 * @author     Paul Thébaud <[email protected]>.
18
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
19
 * @license    https://opensource.org/licenses/MIT The MIT license.
20
 * @link       https://github.com/paul-thebaud/phpunit-generator
21
 * @since      Class available since Release 2.0.0.
22
 */
23
class InterfaceNodeParser extends AbstractNodeParser
24
{
25
    /**
26
     * @var ConfigInterface $config The configuration to use.
27
     */
28
    private $config;
29
30
    /**
31
     * @var DocumentationNodeParser $documentationNodeParser The documentation node parser to use.
32
     */
33
    private $documentationNodeParser;
34
35
    /**
36
     * InterfaceNodeParser constructor.
37
     *
38
     * @param ConfigInterface         $config                  The configuration to use.
39
     * @param MethodNodeParser        $methodNodeParser        The method node parser to use.
40
     * @param DocumentationNodeParser $documentationNodeParser The documentation node parser to use.
41
     */
42
    public function __construct(
43
        ConfigInterface $config,
44
        MethodNodeParser $methodNodeParser,
45
        DocumentationNodeParser $documentationNodeParser
46
    ) {
47
        $this->config                  = $config;
48
        $this->documentationNodeParser = $documentationNodeParser;
49
50
        $this->nodeParsers[Node\Stmt\ClassMethod::class] = $methodNodeParser;
51
    }
52
53
    /**
54
     * Parse a node to update the parent node model.
55
     *
56
     * @param mixed         $node   The node to parse.
57
     * @param NodeInterface $parent The parent node.
58
     *
59
     * @throws AnnotationParseException If an annotation can not be parsed.
60
     */
61
    public function invoke($node, NodeInterface $parent): void
62
    {
63
        if ($this->config->hasInterfaceParsing()) {
64
            if (! $node instanceof Node\Stmt\Interface_ || ! $parent instanceof PhpFileModelInterface) {
65
                throw new Exception('InterfaceNodeParser is made to parse an interface node');
66
            }
67
68
            $interface = new InterfaceModel();
69
            $interface->setParentNode($parent);
70
            $interface->setName(ClassLikeNameHelper::getName($node));
71
            $parent->addConcreteUse($parent->getFullNameFor($interface->getName()), $interface->getName());
72
73
            if (($documentation = $node->getDocComment()) !== null) {
74
                $this->documentationNodeParser->invoke($documentation, $interface);
75
            }
76
77
            $this->parseSubNodes($node->stmts, $interface);
78
79
            $parent->addInterface($interface);
80
        }
81
    }
82
}
83