annotate vendor/nikic/php-parser/lib/PhpParser/Builder/Class_.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 5fb285c0d0e3
children
rev   line source
Chris@13 1 <?php declare(strict_types=1);
Chris@0 2
Chris@0 3 namespace PhpParser\Builder;
Chris@0 4
Chris@0 5 use PhpParser;
Chris@13 6 use PhpParser\BuilderHelpers;
Chris@0 7 use PhpParser\Node\Name;
Chris@0 8 use PhpParser\Node\Stmt;
Chris@0 9
Chris@0 10 class Class_ extends Declaration
Chris@0 11 {
Chris@0 12 protected $name;
Chris@0 13
Chris@0 14 protected $extends = null;
Chris@13 15 protected $implements = [];
Chris@0 16 protected $flags = 0;
Chris@0 17
Chris@13 18 protected $uses = [];
Chris@13 19 protected $constants = [];
Chris@13 20 protected $properties = [];
Chris@13 21 protected $methods = [];
Chris@0 22
Chris@0 23 /**
Chris@0 24 * Creates a class builder.
Chris@0 25 *
Chris@0 26 * @param string $name Name of the class
Chris@0 27 */
Chris@13 28 public function __construct(string $name) {
Chris@0 29 $this->name = $name;
Chris@0 30 }
Chris@0 31
Chris@0 32 /**
Chris@0 33 * Extends a class.
Chris@0 34 *
Chris@0 35 * @param Name|string $class Name of class to extend
Chris@0 36 *
Chris@0 37 * @return $this The builder instance (for fluid interface)
Chris@0 38 */
Chris@0 39 public function extend($class) {
Chris@13 40 $this->extends = BuilderHelpers::normalizeName($class);
Chris@0 41
Chris@0 42 return $this;
Chris@0 43 }
Chris@0 44
Chris@0 45 /**
Chris@0 46 * Implements one or more interfaces.
Chris@0 47 *
Chris@0 48 * @param Name|string ...$interfaces Names of interfaces to implement
Chris@0 49 *
Chris@0 50 * @return $this The builder instance (for fluid interface)
Chris@0 51 */
Chris@13 52 public function implement(...$interfaces) {
Chris@13 53 foreach ($interfaces as $interface) {
Chris@13 54 $this->implements[] = BuilderHelpers::normalizeName($interface);
Chris@0 55 }
Chris@0 56
Chris@0 57 return $this;
Chris@0 58 }
Chris@0 59
Chris@0 60 /**
Chris@0 61 * Makes the class abstract.
Chris@0 62 *
Chris@0 63 * @return $this The builder instance (for fluid interface)
Chris@0 64 */
Chris@0 65 public function makeAbstract() {
Chris@13 66 $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
Chris@0 67
Chris@0 68 return $this;
Chris@0 69 }
Chris@0 70
Chris@0 71 /**
Chris@0 72 * Makes the class final.
Chris@0 73 *
Chris@0 74 * @return $this The builder instance (for fluid interface)
Chris@0 75 */
Chris@0 76 public function makeFinal() {
Chris@13 77 $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
Chris@0 78
Chris@0 79 return $this;
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * Adds a statement.
Chris@0 84 *
Chris@0 85 * @param Stmt|PhpParser\Builder $stmt The statement to add
Chris@0 86 *
Chris@0 87 * @return $this The builder instance (for fluid interface)
Chris@0 88 */
Chris@0 89 public function addStmt($stmt) {
Chris@13 90 $stmt = BuilderHelpers::normalizeNode($stmt);
Chris@0 91
Chris@13 92 $targets = [
Chris@13 93 Stmt\TraitUse::class => &$this->uses,
Chris@13 94 Stmt\ClassConst::class => &$this->constants,
Chris@13 95 Stmt\Property::class => &$this->properties,
Chris@13 96 Stmt\ClassMethod::class => &$this->methods,
Chris@13 97 ];
Chris@0 98
Chris@13 99 $class = \get_class($stmt);
Chris@13 100 if (!isset($targets[$class])) {
Chris@13 101 throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
Chris@0 102 }
Chris@0 103
Chris@13 104 $targets[$class][] = $stmt;
Chris@0 105
Chris@0 106 return $this;
Chris@0 107 }
Chris@0 108
Chris@0 109 /**
Chris@0 110 * Returns the built class node.
Chris@0 111 *
Chris@0 112 * @return Stmt\Class_ The built class node
Chris@0 113 */
Chris@13 114 public function getNode() : PhpParser\Node {
Chris@13 115 return new Stmt\Class_($this->name, [
Chris@0 116 'flags' => $this->flags,
Chris@0 117 'extends' => $this->extends,
Chris@0 118 'implements' => $this->implements,
Chris@0 119 'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
Chris@13 120 ], $this->attributes);
Chris@0 121 }
Chris@13 122 }