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); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$trait = $this->parseSubNodes($node->stmts, $trait); |
52
|
|
|
|
53
|
|
|
$parent->addTrait($trait); |
54
|
|
|
|
55
|
|
|
return $parent; |
56
|
|
|
} |
57
|
|
|
} |