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