| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Attribute\Reflection; |
| 13 |
|
| 14 |
/** |
| 15 |
* @internal |
| 16 |
*/ |
| 17 |
class ReflectionMember |
| 18 |
{ |
| 19 |
public function __construct( |
| 20 |
private readonly \ReflectionParameter|\ReflectionProperty $member, |
| 21 |
) { |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @template T of object |
| 26 |
* |
| 27 |
* @param class-string<T> $class |
| 28 |
* |
| 29 |
* @return T|null |
| 30 |
*/ |
| 31 |
public function getAttribute(string $class): ?object |
| 32 |
{ |
| 33 |
return ($this->member->getAttributes($class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null)?->newInstance(); |
| 34 |
} |
| 35 |
|
| 36 |
public function getSourceName(): string |
| 37 |
{ |
| 38 |
if ($this->member instanceof \ReflectionProperty) { |
| 39 |
return $this->member->getDeclaringClass()->name; |
| 40 |
} |
| 41 |
|
| 42 |
$function = $this->member->getDeclaringFunction(); |
| 43 |
|
| 44 |
if ($function instanceof \ReflectionMethod) { |
| 45 |
return $function->class.'::'.$function->name.'()'; |
| 46 |
} |
| 47 |
|
| 48 |
return $function->name.'()'; |
| 49 |
} |
| 50 |
|
| 51 |
public function getSourceThis(): ?object |
| 52 |
{ |
| 53 |
if ($this->member instanceof \ReflectionParameter) { |
| 54 |
return $this->member->getDeclaringFunction()->getClosureThis(); |
| 55 |
} |
| 56 |
|
| 57 |
return null; |
| 58 |
} |
| 59 |
|
| 60 |
public function getType(): ?\ReflectionType |
| 61 |
{ |
| 62 |
return $this->member->getType(); |
| 63 |
} |
| 64 |
|
| 65 |
public function getName(): string |
| 66 |
{ |
| 67 |
return $this->member->getName(); |
| 68 |
} |
| 69 |
|
| 70 |
public function hasDefaultValue(): bool |
| 71 |
{ |
| 72 |
if ($this->member instanceof \ReflectionParameter) { |
| 73 |
return $this->member->isDefaultValueAvailable(); |
| 74 |
} |
| 75 |
|
| 76 |
return $this->member->hasDefaultValue(); |
| 77 |
} |
| 78 |
|
| 79 |
public function getDefaultValue(): mixed |
| 80 |
{ |
| 81 |
$defaultValue = $this->member->getDefaultValue(); |
| 82 |
|
| 83 |
if ($defaultValue instanceof \BackedEnum) { |
| 84 |
return $defaultValue->value; |
| 85 |
} |
| 86 |
|
| 87 |
return $defaultValue; |
| 88 |
} |
| 89 |
|
| 90 |
public function isNullable(): bool |
| 91 |
{ |
| 92 |
return (bool) $this->member->getType()?->allowsNull(); |
| 93 |
} |
| 94 |
|
| 95 |
public function getMemberName(): string |
| 96 |
{ |
| 97 |
return $this->member instanceof \ReflectionParameter ? 'parameter' : 'property'; |
| 98 |
} |
| 99 |
|
| 100 |
public function isParameter(): bool |
| 101 |
{ |
| 102 |
return $this->member instanceof \ReflectionParameter; |
| 103 |
} |
| 104 |
|
| 105 |
public function isProperty(): bool |
| 106 |
{ |
| 107 |
return $this->member instanceof \ReflectionProperty; |
| 108 |
} |
| 109 |
} |