Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. TraitUseAdaptation.php

class TraitUseAdaptation

Same name in this branch
  1. 11.1.x vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/TraitUseAdaptation.php \PhpParser\Node\Stmt\TraitUseAdaptation

Hierarchy

  • class \PhpParser\Builder\TraitUseAdaptation implements \PhpParser\Builder

Expanded class hierarchy of TraitUseAdaptation

File

vendor/nikic/php-parser/lib/PhpParser/Builder/TraitUseAdaptation.php, line 11

Namespace

PhpParser\Builder
View source
class TraitUseAdaptation implements Builder {
    private const TYPE_UNDEFINED = 0;
    private const TYPE_ALIAS = 1;
    private const TYPE_PRECEDENCE = 2;
    protected int $type;
    protected ?Node\Name $trait;
    protected Node\Identifier $method;
    protected ?int $modifier = null;
    protected ?Node\Identifier $alias = null;
    
    /** @var Node\Name[] */
    protected array $insteadof = [];
    
    /**
     * Creates a trait use adaptation builder.
     *
     * @param Node\Name|string|null $trait Name of adapted trait
     * @param Node\Identifier|string $method Name of adapted method
     */
    public function __construct($trait, $method) {
        $this->type = self::TYPE_UNDEFINED;
        $this->trait = is_null($trait) ? null : BuilderHelpers::normalizeName($trait);
        $this->method = BuilderHelpers::normalizeIdentifier($method);
    }
    
    /**
     * Sets alias of method.
     *
     * @param Node\Identifier|string $alias Alias for adapted method
     *
     * @return $this The builder instance (for fluid interface)
     */
    public function as($alias) {
        if ($this->type === self::TYPE_UNDEFINED) {
            $this->type = self::TYPE_ALIAS;
        }
        if ($this->type !== self::TYPE_ALIAS) {
            throw new \LogicException('Cannot set alias for not alias adaptation buider');
        }
        $this->alias = BuilderHelpers::normalizeIdentifier($alias);
        return $this;
    }
    
    /**
     * Sets adapted method public.
     *
     * @return $this The builder instance (for fluid interface)
     */
    public function makePublic() {
        $this->setModifier(Modifiers::PUBLIC);
        return $this;
    }
    
    /**
     * Sets adapted method protected.
     *
     * @return $this The builder instance (for fluid interface)
     */
    public function makeProtected() {
        $this->setModifier(Modifiers::PROTECTED);
        return $this;
    }
    
    /**
     * Sets adapted method private.
     *
     * @return $this The builder instance (for fluid interface)
     */
    public function makePrivate() {
        $this->setModifier(Modifiers::PRIVATE);
        return $this;
    }
    
    /**
     * Adds overwritten traits.
     *
     * @param Node\Name|string ...$traits Traits for overwrite
     *
     * @return $this The builder instance (for fluid interface)
     */
    public function insteadof(...$traits) {
        if ($this->type === self::TYPE_UNDEFINED) {
            if (is_null($this->trait)) {
                throw new \LogicException('Precedence adaptation must have trait');
            }
            $this->type = self::TYPE_PRECEDENCE;
        }
        if ($this->type !== self::TYPE_PRECEDENCE) {
            throw new \LogicException('Cannot add overwritten traits for not precedence adaptation buider');
        }
        foreach ($traits as $trait) {
            $this->insteadof[] = BuilderHelpers::normalizeName($trait);
        }
        return $this;
    }
    protected function setModifier(int $modifier) : void {
        if ($this->type === self::TYPE_UNDEFINED) {
            $this->type = self::TYPE_ALIAS;
        }
        if ($this->type !== self::TYPE_ALIAS) {
            throw new \LogicException('Cannot set access modifier for not alias adaptation buider');
        }
        if (is_null($this->modifier)) {
            $this->modifier = $modifier;
        }
        else {
            throw new \LogicException('Multiple access type modifiers are not allowed');
        }
    }
    
    /**
     * Returns the built node.
     *
     * @return Node The built node
     */
    public function getNode() : Node {
        switch ($this->type) {
            case self::TYPE_ALIAS:
                return new Stmt\TraitUseAdaptation\Alias($this->trait, $this->method, $this->modifier, $this->alias);
            case self::TYPE_PRECEDENCE:
                return new Stmt\TraitUseAdaptation\Precedence($this->trait, $this->method, $this->insteadof);
            default:
                throw new \LogicException('Type of adaptation is not defined');
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
TraitUseAdaptation::$alias protected property
TraitUseAdaptation::$insteadof protected property @var Node\Name[]
TraitUseAdaptation::$method protected property
TraitUseAdaptation::$modifier protected property
TraitUseAdaptation::$trait protected property
TraitUseAdaptation::$type protected property
TraitUseAdaptation::as public function Sets alias of method.
TraitUseAdaptation::getNode public function Returns the built node. Overrides Builder::getNode
TraitUseAdaptation::insteadof public function Adds overwritten traits.
TraitUseAdaptation::makePrivate public function Sets adapted method private.
TraitUseAdaptation::makeProtected public function Sets adapted method protected.
TraitUseAdaptation::makePublic public function Sets adapted method public.
TraitUseAdaptation::setModifier protected function
TraitUseAdaptation::TYPE_ALIAS private constant
TraitUseAdaptation::TYPE_PRECEDENCE private constant
TraitUseAdaptation::TYPE_UNDEFINED private constant
TraitUseAdaptation::__construct public function Creates a trait use adaptation builder.
RSS feed
Powered by Drupal