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