InterfaceNodeParser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 57
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A invoke() 0 19 5
1
<?php
2
3
/**
4
 * This file is part of PhpUnitGen.
5
 *
6
 * (c) 2017-2018 Paul Thébaud <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhpUnitGen\Parser\NodeParser;
13
14
use PhpParser\Node;
15
use PhpUnitGen\Configuration\ConfigurationInterface\ConfigInterface;
16
use PhpUnitGen\Exception\AnnotationParseException;
17
use PhpUnitGen\Exception\Exception;
18
use PhpUnitGen\Model\InterfaceModel;
19
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface;
20
use PhpUnitGen\Model\PropertyInterface\NodeInterface;
21
use PhpUnitGen\Parser\NodeParserUtil\ClassLikeNameHelper;
22
23
/**
24
 * Class InterfaceNodeParser.
25
 *
26
 * @author     Paul Thébaud <[email protected]>.
27
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
28
 * @license    https://opensource.org/licenses/MIT The MIT license.
29
 * @link       https://github.com/paul-thebaud/phpunit-generator
30
 * @since      Class available since Release 2.0.0.
31
 */
32
class InterfaceNodeParser extends AbstractNodeParser
33
{
34
    /**
35
     * @var ConfigInterface $config The configuration to use.
36
     */
37
    private $config;
38
39
    /**
40
     * @var DocumentationNodeParser $documentationNodeParser The documentation node parser to use.
41
     */
42
    private $documentationNodeParser;
43
44
    /**
45
     * InterfaceNodeParser constructor.
46
     *
47
     * @param ConfigInterface         $config                  The configuration to use.
48
     * @param MethodNodeParser        $methodNodeParser        The method node parser to use.
49
     * @param DocumentationNodeParser $documentationNodeParser The documentation node parser to use.
50
     */
51
    public function __construct(
52
        ConfigInterface $config,
53
        MethodNodeParser $methodNodeParser,
54
        DocumentationNodeParser $documentationNodeParser
55
    ) {
56
        $this->config                  = $config;
57
        $this->documentationNodeParser = $documentationNodeParser;
58
59
        $this->nodeParsers[Node\Stmt\ClassMethod::class] = $methodNodeParser;
60
    }
61
62
    /**
63
     * Parse a node to update the parent node model.
64
     *
65
     * @param mixed         $node   The node to parse.
66
     * @param NodeInterface $parent The parent node.
67
     *
68
     * @throws AnnotationParseException If an annotation can not be parsed.
69
     */
70
    public function invoke($node, NodeInterface $parent): void
71
    {
72
        if ($this->config->hasInterfaceParsing()) {
73
            if (! $node instanceof Node\Stmt\Interface_ || ! $parent instanceof PhpFileModelInterface) {
74
                throw new Exception('InterfaceNodeParser is made to parse an interface node');
75
            }
76
77
            $interface = new InterfaceModel();
78
            $interface->setParentNode($parent);
79
            $interface->setName(ClassLikeNameHelper::getName($node));
80
            $parent->addConcreteUse($parent->getFullNameFor($interface->getName()), $interface->getName());
81
82
            if (($documentation = $node->getDocComment()) !== null) {
83
                $this->documentationNodeParser->invoke($documentation, $interface);
84
            }
85
86
            $this->parseSubNodes($node->stmts, $interface);
87
88
            $parent->addInterface($interface);
89
        }
90
    }
91
}
92