1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Parser\NodeParser; |
4
|
|
|
|
5
|
|
|
use PhpParser\Node; |
6
|
|
|
use PhpUnitGen\Exception\Exception; |
7
|
|
|
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface; |
8
|
|
|
use PhpUnitGen\Model\PropertyInterface\NodeInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class GroupUseNodeParser. |
12
|
|
|
* |
13
|
|
|
* @author Paul Thébaud <[email protected]>. |
14
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
15
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
16
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
17
|
|
|
* @since Class available since Release 2.0.0. |
18
|
|
|
*/ |
19
|
|
|
class GroupUseNodeParser extends AbstractNodeParser |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Parse a node to update the parent node model. |
23
|
|
|
* |
24
|
|
|
* @param mixed $node The node to parse. |
25
|
|
|
* @param NodeInterface $parent The parent node. |
26
|
|
|
*/ |
27
|
|
|
public function invoke($node, NodeInterface $parent): void |
28
|
|
|
{ |
29
|
|
|
if (! $node instanceof Node\Stmt\GroupUse || ! $parent instanceof PhpFileModelInterface) { |
30
|
|
|
throw new Exception('GroupUseNodeParser is made to parse a use group node'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ($this->validateType($node->type)) { |
34
|
|
|
$prefix = $node->prefix->toString(); |
35
|
|
|
foreach ($node->uses as $use) { |
36
|
|
|
if ($this->validateType($node->type)) { |
37
|
|
|
$parent->addUse($use->alias, $prefix . '\\' . $use->name->toString()); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Validate a use type. |
45
|
|
|
* |
46
|
|
|
* @param int $type The type to validate. |
47
|
|
|
* |
48
|
|
|
* @return bool True if the type is valid. |
49
|
|
|
*/ |
50
|
|
|
private function validateType(int $type): bool |
51
|
|
|
{ |
52
|
|
|
return $type === Node\Stmt\Use_::TYPE_NORMAL || $type === Node\Stmt\Use_::TYPE_UNKNOWN; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|