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

Breadcrumb

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

class Class_

Same name in this branch
  1. 11.1.x vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/MagicConst/Class_.php \PhpParser\Node\Scalar\MagicConst\Class_
  2. 11.1.x vendor/nikic/php-parser/lib/PhpParser/Node/Stmt/Class_.php \PhpParser\Node\Stmt\Class_
  3. 11.1.x vendor/mck89/peast/lib/Peast/Syntax/Node/Class_.php \Peast\Syntax\Node\Class_

Hierarchy

  • class \PhpParser\Builder\Declaration implements \PhpParser\Builder
    • class \PhpParser\Builder\Class_ extends \PhpParser\Builder\Declaration

Expanded class hierarchy of Class_

File

vendor/nikic/php-parser/lib/PhpParser/Builder/Class_.php, line 12

Namespace

PhpParser\Builder
View source
class Class_ extends Declaration {
    protected string $name;
    protected ?Name $extends = null;
    
    /** @var list<Name> */
    protected array $implements = [];
    protected int $flags = 0;
    
    /** @var list<Stmt\TraitUse> */
    protected array $uses = [];
    
    /** @var list<Stmt\ClassConst> */
    protected array $constants = [];
    
    /** @var list<Stmt\Property> */
    protected array $properties = [];
    
    /** @var list<Stmt\ClassMethod> */
    protected array $methods = [];
    
    /** @var list<Node\AttributeGroup> */
    protected array $attributeGroups = [];
    
    /**
     * Creates a class builder.
     *
     * @param string $name Name of the class
     */
    public function __construct(string $name) {
        $this->name = $name;
    }
    
    /**
     * Extends a class.
     *
     * @param Name|string $class Name of class to extend
     *
     * @return $this The builder instance (for fluid interface)
     */
    public function extend($class) {
        $this->extends = BuilderHelpers::normalizeName($class);
        return $this;
    }
    
    /**
     * Implements one or more interfaces.
     *
     * @param Name|string ...$interfaces Names of interfaces to implement
     *
     * @return $this The builder instance (for fluid interface)
     */
    public function implement(...$interfaces) {
        foreach ($interfaces as $interface) {
            $this->implements[] = BuilderHelpers::normalizeName($interface);
        }
        return $this;
    }
    
    /**
     * Makes the class abstract.
     *
     * @return $this The builder instance (for fluid interface)
     */
    public function makeAbstract() {
        $this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::ABSTRACT);
        return $this;
    }
    
    /**
     * Makes the class final.
     *
     * @return $this The builder instance (for fluid interface)
     */
    public function makeFinal() {
        $this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::FINAL);
        return $this;
    }
    
    /**
     * Makes the class readonly.
     *
     * @return $this The builder instance (for fluid interface)
     */
    public function makeReadonly() {
        $this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::READONLY);
        return $this;
    }
    
    /**
     * Adds a statement.
     *
     * @param Stmt|PhpParser\Builder $stmt The statement to add
     *
     * @return $this The builder instance (for fluid interface)
     */
    public function addStmt($stmt) {
        $stmt = BuilderHelpers::normalizeNode($stmt);
        if ($stmt instanceof Stmt\Property) {
            $this->properties[] = $stmt;
        }
        elseif ($stmt instanceof Stmt\ClassMethod) {
            $this->methods[] = $stmt;
        }
        elseif ($stmt instanceof Stmt\TraitUse) {
            $this->uses[] = $stmt;
        }
        elseif ($stmt instanceof Stmt\ClassConst) {
            $this->constants[] = $stmt;
        }
        else {
            throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
        }
        return $this;
    }
    
    /**
     * Adds an attribute group.
     *
     * @param Node\Attribute|Node\AttributeGroup $attribute
     *
     * @return $this The builder instance (for fluid interface)
     */
    public function addAttribute($attribute) {
        $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
        return $this;
    }
    
    /**
     * Returns the built class node.
     *
     * @return Stmt\Class_ The built class node
     */
    public function getNode() : PhpParser\Node {
        return new Stmt\Class_($this->name, [
            'flags' => $this->flags,
            'extends' => $this->extends,
            'implements' => $this->implements,
            'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
            'attrGroups' => $this->attributeGroups,
        ], $this->attributes);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
Class_::$attributeGroups protected property @var list&lt;Node\AttributeGroup&gt;
Class_::$constants protected property @var list&lt;Stmt\ClassConst&gt;
Class_::$extends protected property
Class_::$flags protected property
Class_::$implements protected property @var list&lt;Name&gt;
Class_::$methods protected property @var list&lt;Stmt\ClassMethod&gt;
Class_::$name protected property
Class_::$properties protected property @var list&lt;Stmt\Property&gt;
Class_::$uses protected property @var list&lt;Stmt\TraitUse&gt;
Class_::addAttribute public function Adds an attribute group.
Class_::addStmt public function Adds a statement. Overrides Declaration::addStmt
Class_::extend public function Extends a class.
Class_::getNode public function Returns the built class node. Overrides Builder::getNode
Class_::implement public function Implements one or more interfaces.
Class_::makeAbstract public function Makes the class abstract.
Class_::makeFinal public function Makes the class final.
Class_::makeReadonly public function Makes the class readonly.
Class_::__construct public function Creates a class builder.
Declaration::$attributes protected property @var array&lt;string, mixed&gt;
Declaration::addStmts public function Adds multiple statements.
Declaration::setDocComment public function Sets doc comment for the declaration.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal