|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpParser\Builder; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\BuilderAbstract; |
|
6
|
|
|
use PhpParser\Node; |
|
7
|
|
|
use PhpParser\Node\Stmt; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @method $this as(string $alias) Sets alias for used name. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Use_ extends BuilderAbstract { |
|
13
|
|
|
protected $name; |
|
14
|
|
|
protected $type; |
|
15
|
|
|
protected $alias = null; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Creates a name use (alias) builder. |
|
19
|
|
|
* |
|
20
|
|
|
* @param Node\Name|string $name Name of the entity (namespace, class, function, constant) to alias |
|
21
|
|
|
* @param int $type One of the Stmt\Use_::TYPE_* constants |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct($name, $type) { |
|
24
|
|
|
$this->name = $this->normalizeName($name); |
|
25
|
|
|
$this->type = $type; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Sets alias for used name. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $alias Alias to use (last component of full name by default) |
|
32
|
|
|
* |
|
33
|
|
|
* @return $this The builder instance (for fluid interface) |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function as_($alias) { |
|
36
|
|
|
$this->alias = $alias; |
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
View Code Duplication |
public function __call($name, $args) { |
|
|
|
|
|
|
40
|
|
|
if (method_exists($this, $name . '_')) { |
|
41
|
|
|
return call_user_func_array(array($this, $name . '_'), $args); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
throw new \LogicException(sprintf('Method "%s" does not exist', $name)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Returns the built node. |
|
49
|
|
|
* |
|
50
|
|
|
* @return Node The built node |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getNode() { |
|
53
|
|
|
$alias = null !== $this->alias ? $this->alias : $this->name->getLast(); |
|
54
|
|
|
return new Stmt\Use_(array( |
|
55
|
|
|
new Stmt\UseUse($this->name, $alias) |
|
56
|
|
|
), $this->type); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.