1<?php declare(strict_types=1); 2 3namespace PhpParser\Builder; 4 5use PhpParser; 6use PhpParser\BuilderHelpers; 7use PhpParser\Node\Identifier; 8use PhpParser\Node\Name; 9use PhpParser\Node\NullableType; 10use PhpParser\Node\Stmt; 11 12class Property implements PhpParser\Builder 13{ 14 protected $name; 15 16 protected $flags = 0; 17 protected $default = null; 18 protected $attributes = []; 19 20 /** @var null|Identifier|Name|NullableType */ 21 protected $type; 22 23 /** 24 * Creates a property builder. 25 * 26 * @param string $name Name of the property 27 */ 28 public function __construct(string $name) { 29 $this->name = $name; 30 } 31 32 /** 33 * Makes the property public. 34 * 35 * @return $this The builder instance (for fluid interface) 36 */ 37 public function makePublic() { 38 $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC); 39 40 return $this; 41 } 42 43 /** 44 * Makes the property protected. 45 * 46 * @return $this The builder instance (for fluid interface) 47 */ 48 public function makeProtected() { 49 $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED); 50 51 return $this; 52 } 53 54 /** 55 * Makes the property private. 56 * 57 * @return $this The builder instance (for fluid interface) 58 */ 59 public function makePrivate() { 60 $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE); 61 62 return $this; 63 } 64 65 /** 66 * Makes the property static. 67 * 68 * @return $this The builder instance (for fluid interface) 69 */ 70 public function makeStatic() { 71 $this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_STATIC); 72 73 return $this; 74 } 75 76 /** 77 * Sets default value for the property. 78 * 79 * @param mixed $value Default value to use 80 * 81 * @return $this The builder instance (for fluid interface) 82 */ 83 public function setDefault($value) { 84 $this->default = BuilderHelpers::normalizeValue($value); 85 86 return $this; 87 } 88 89 /** 90 * Sets doc comment for the property. 91 * 92 * @param PhpParser\Comment\Doc|string $docComment Doc comment to set 93 * 94 * @return $this The builder instance (for fluid interface) 95 */ 96 public function setDocComment($docComment) { 97 $this->attributes = [ 98 'comments' => [BuilderHelpers::normalizeDocComment($docComment)] 99 ]; 100 101 return $this; 102 } 103 104 /** 105 * Sets the property type for PHP 7.4+. 106 * 107 * @param string|Name|NullableType|Identifier $type 108 * 109 * @return $this 110 */ 111 public function setType($type) { 112 $this->type = BuilderHelpers::normalizeType($type); 113 114 return $this; 115 } 116 117 /** 118 * Returns the built class node. 119 * 120 * @return Stmt\Property The built property node 121 */ 122 public function getNode() : PhpParser\Node { 123 return new Stmt\Property( 124 $this->flags !== 0 ? $this->flags : Stmt\Class_::MODIFIER_PUBLIC, 125 [ 126 new Stmt\PropertyProperty($this->name, $this->default) 127 ], 128 $this->attributes, 129 $this->type 130 ); 131 } 132} 133