annotate vendor/nikic/php-parser/lib/PhpParser/Builder/Class_.php @ 0:4c8ae668cc8c

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