| 1 |
<?php |
| 2 |
// phpcs:ignoreFile -- Bundled third-party (Mozart) dependency; exempt from plugin coding standards. |
| 3 |
|
| 4 |
namespace WPDeveloper\BetterDocs\Dependencies\PhpParser\Builder; |
| 5 |
|
| 6 |
use WPDeveloper\BetterDocs\Dependencies\PhpParser\BuilderAbstract; |
| 7 |
use WPDeveloper\BetterDocs\Dependencies\PhpParser\Node; |
| 8 |
use WPDeveloper\BetterDocs\Dependencies\PhpParser\Node\Stmt; |
| 9 |
|
| 10 |
/** |
| 11 |
* @method $this as(string $alias) Sets alias for used name. |
| 12 |
*/ |
| 13 |
class Use_ extends BuilderAbstract { |
| 14 |
protected $name; |
| 15 |
protected $type; |
| 16 |
protected $alias = null; |
| 17 |
|
| 18 |
/** |
| 19 |
* Creates a name use (alias) builder. |
| 20 |
* |
| 21 |
* @param Node\Name|string $name Name of the entity (namespace, class, function, constant) to alias |
| 22 |
* @param int $type One of the Stmt\Use_::TYPE_* constants |
| 23 |
*/ |
| 24 |
public function __construct($name, $type) { |
| 25 |
$this->name = $this->normalizeName($name); |
| 26 |
$this->type = $type; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Sets alias for used name. |
| 31 |
* |
| 32 |
* @param string $alias Alias to use (last component of full name by default) |
| 33 |
* |
| 34 |
* @return $this The builder instance (for fluid interface) |
| 35 |
*/ |
| 36 |
protected function as_($alias) { |
| 37 |
$this->alias = $alias; |
| 38 |
return $this; |
| 39 |
} |
| 40 |
public function __call($name, $args) { |
| 41 |
if (method_exists($this, $name . '_')) { |
| 42 |
return call_user_func_array(array($this, $name . '_'), $args); |
| 43 |
} |
| 44 |
|
| 45 |
throw new \LogicException(sprintf('Method "%s" does not exist', $name)); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Returns the built node. |
| 50 |
* |
| 51 |
* @return Node The built node |
| 52 |
*/ |
| 53 |
public function getNode() { |
| 54 |
$alias = null !== $this->alias ? $this->alias : $this->name->getLast(); |
| 55 |
return new Stmt\Use_(array( |
| 56 |
new Stmt\UseUse($this->name, $alias) |
| 57 |
), $this->type); |
| 58 |
} |
| 59 |
} |
| 60 |
|