|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace phpDocumentor\Reflection\Php\Factory; |
| 6 | + |
| 7 | +use phpDocumentor\Reflection\DocBlock; |
| 8 | +use phpDocumentor\Reflection\Fqsen; |
| 9 | +use phpDocumentor\Reflection\Location; |
| 10 | +use phpDocumentor\Reflection\Php\AsyncVisibility; |
| 11 | +use phpDocumentor\Reflection\Php\Property as PropertyElement; |
| 12 | +use phpDocumentor\Reflection\Php\Visibility; |
| 13 | +use PhpParser\Node\ComplexType; |
| 14 | +use PhpParser\Node\Expr; |
| 15 | +use PhpParser\Node\Identifier; |
| 16 | +use PhpParser\Node\Name; |
| 17 | +use PhpParser\Node\Param; |
| 18 | +use PhpParser\PrettyPrinter\Standard as PrettyPrinter; |
| 19 | + |
| 20 | +use function method_exists; |
| 21 | + |
| 22 | +/** |
| 23 | + * This class is responsible for building a property element from a PhpParser node. |
| 24 | + * |
| 25 | + * @internal |
| 26 | + */ |
| 27 | +final class PropertyBuilder |
| 28 | +{ |
| 29 | + private Fqsen $fqsen; |
| 30 | + private Visibility $visibility; |
| 31 | + private bool $readOnly = false; |
| 32 | + private Identifier|Name|ComplexType|null $type; |
| 33 | + private DocBlock|null $docblock; |
| 34 | + private PrettyPrinter $valueConverter; |
| 35 | + private Expr|null $default; |
| 36 | + private bool $static; |
| 37 | + private Location $startLocation; |
| 38 | + private Location $endLocation; |
| 39 | + |
| 40 | + private function __construct() |
| 41 | + { |
| 42 | + } |
| 43 | + |
| 44 | + public static function create(PrettyPrinter $valueConverter): self |
| 45 | + { |
| 46 | + $instance = new self(); |
| 47 | + $instance->valueConverter = $valueConverter; |
| 48 | + |
| 49 | + return $instance; |
| 50 | + } |
| 51 | + |
| 52 | + public function fqsen(Fqsen $fqsen): self |
| 53 | + { |
| 54 | + $this->fqsen = $fqsen; |
| 55 | + |
| 56 | + return $this; |
| 57 | + } |
| 58 | + |
| 59 | + public function visibility(Param|PropertyIterator $node): self |
| 60 | + { |
| 61 | + $this->visibility = $this->buildVisibility($node); |
| 62 | + |
| 63 | + return $this; |
| 64 | + } |
| 65 | + |
| 66 | + public function type(Identifier|Name|ComplexType|null $type): self |
| 67 | + { |
| 68 | + $this->type = $type; |
| 69 | + |
| 70 | + return $this; |
| 71 | + } |
| 72 | + |
| 73 | + public function readOnly(bool $readOnly): self |
| 74 | + { |
| 75 | + $this->readOnly = $readOnly; |
| 76 | + |
| 77 | + return $this; |
| 78 | + } |
| 79 | + |
| 80 | + public function docblock(DocBlock|null $docblock): self |
| 81 | + { |
| 82 | + $this->docblock = $docblock; |
| 83 | + |
| 84 | + return $this; |
| 85 | + } |
| 86 | + |
| 87 | + public function default(Expr|null $default): self |
| 88 | + { |
| 89 | + $this->default = $default; |
| 90 | + |
| 91 | + return $this; |
| 92 | + } |
| 93 | + |
| 94 | + public function static(bool $static): self |
| 95 | + { |
| 96 | + $this->static = $static; |
| 97 | + |
| 98 | + return $this; |
| 99 | + } |
| 100 | + |
| 101 | + public function startLocation(Location $startLocation): self |
| 102 | + { |
| 103 | + $this->startLocation = $startLocation; |
| 104 | + |
| 105 | + return $this; |
| 106 | + } |
| 107 | + |
| 108 | + public function endLocation(Location $endLocation): self |
| 109 | + { |
| 110 | + $this->endLocation = $endLocation; |
| 111 | + |
| 112 | + return $this; |
| 113 | + } |
| 114 | + |
| 115 | + public function build(): PropertyElement |
| 116 | + { |
| 117 | + return new PropertyElement( |
| 118 | + $this->fqsen, |
| 119 | + $this->visibility, |
| 120 | + $this->docblock, |
| 121 | + $this->default !== null ? $this->valueConverter->prettyPrintExpr($this->default) : null, |
| 122 | + $this->static, |
| 123 | + $this->startLocation, |
| 124 | + $this->endLocation, |
| 125 | + (new Type())->fromPhpParser($this->type), |
| 126 | + $this->readOnly, |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Returns true when current property has async accessors. |
| 132 | + * |
| 133 | + * This method will always return false when your phpparser version is < 5.2 |
| 134 | + */ |
| 135 | + private function isAsync(Param|PropertyIterator $node): bool |
| 136 | + { |
| 137 | + if (method_exists($node, 'isPrivateSet') === false) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + return $node->isPublicSet() || $node->isProtectedSet() || $node->isPrivateSet(); |
| 142 | + } |
| 143 | + |
| 144 | + private function buildVisibility(Param|PropertyIterator $node): Visibility |
| 145 | + { |
| 146 | + if ($this->isAsync($node) === false) { |
| 147 | + return $this->buildReadVisibility($node); |
| 148 | + } |
| 149 | + |
| 150 | + $readVisibility = $this->buildReadVisibility($node); |
| 151 | + $writeVisibility = $this->buildWriteVisibility($node); |
| 152 | + |
| 153 | + return new AsyncVisibility( |
| 154 | + $readVisibility, |
| 155 | + $writeVisibility, |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + private function buildReadVisibility(Param|PropertyIterator $node): Visibility |
| 160 | + { |
| 161 | + if ($node->isPrivate()) { |
| 162 | + return new Visibility(Visibility::PRIVATE_); |
| 163 | + } |
| 164 | + |
| 165 | + if ($node->isProtected()) { |
| 166 | + return new Visibility(Visibility::PROTECTED_); |
| 167 | + } |
| 168 | + |
| 169 | + return new Visibility(Visibility::PUBLIC_); |
| 170 | + } |
| 171 | + |
| 172 | + private function buildWriteVisibility(Param|PropertyIterator $node): Visibility |
| 173 | + { |
| 174 | + if ($node->isPrivateSet()) { |
| 175 | + return new Visibility(Visibility::PRIVATE_); |
| 176 | + } |
| 177 | + |
| 178 | + if ($node->isProtectedSet()) { |
| 179 | + return new Visibility(Visibility::PROTECTED_); |
| 180 | + } |
| 181 | + |
| 182 | + return new Visibility(Visibility::PUBLIC_); |
| 183 | + } |
| 184 | +} |
0 commit comments