-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNameNodeHelper.php
More file actions
51 lines (47 loc) · 1.26 KB
/
NameNodeHelper.php
File metadata and controls
51 lines (47 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);
namespace Crmplease\Coder\Helper;
use Crmplease\Coder\Rector\RectorException;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType;
use function explode;
use function get_class;
use function implode;
/**
* @author Mougrim <rinat@mougrim.ru>
*/
class NameNodeHelper
{
public function getNameByNodeName(Name $node) : string
{
return $node->toString();
}
/**
* @param Identifier|Name|NullableType|UnionType $node
*
* @return string
* @throws RectorException
*/
public function getNameByTypeNode($node): string
{
if ($node instanceof Identifier) {
return $node->name;
}
if ($node instanceof Name) {
return $node->toString();
}
if ($node instanceof NullableType) {
return "?{$this->getNameByTypeNode($node->type)}";
}
if ($node instanceof UnionType) {
$names = [];
foreach ($node->types as $type) {
$names[] = $this->getNameByTypeNode($type);
}
return implode('|', $names);
}
throw new RectorException("Unknown type node class '" . get_class($node) . "'");
}
}