| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Mapping\Loader; |
| 13 |
|
| 14 |
use Symfony\Component\Serializer\Attribute\Ignore; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
trait AccessorCollisionResolverTrait |
| 22 |
{ |
| 23 |
private function getAttributeNameFromAccessor(\ReflectionClass $class, \ReflectionMethod $method, bool $andMutator): ?string |
| 24 |
{ |
| 25 |
$methodName = $method->name; |
| 26 |
|
| 27 |
$i = match ($methodName[0]) { |
| 28 |
's' => $andMutator && str_starts_with($methodName, 'set') ? 3 : null, |
| 29 |
'g' => str_starts_with($methodName, 'get') ? 3 : null, |
| 30 |
'h' => str_starts_with($methodName, 'has') ? 3 : null, |
| 31 |
'c' => str_starts_with($methodName, 'can') ? 3 : null, |
| 32 |
'i' => str_starts_with($methodName, 'is') ? 2 : null, |
| 33 |
default => null, |
| 34 |
}; |
| 35 |
|
| 36 |
|
| 37 |
if (null === $i || ctype_lower($methodName[$i] ?? 'a') || (!$andMutator && $method->isStatic())) { |
| 38 |
return null; |
| 39 |
} |
| 40 |
|
| 41 |
if ('s' === $methodName[0] ? !$method->getNumberOfParameters() : ($method->getNumberOfRequiredParameters() || \in_array((string) $method->getReturnType(), ['void', 'never'], true))) { |
| 42 |
return null; |
| 43 |
} |
| 44 |
|
| 45 |
$attributeName = substr($methodName, $i); |
| 46 |
|
| 47 |
if (!$class->hasProperty($attributeName)) { |
| 48 |
$attributeName = lcfirst($attributeName); |
| 49 |
} |
| 50 |
|
| 51 |
return $attributeName; |
| 52 |
} |
| 53 |
|
| 54 |
private function hasPropertyForAccessor(\ReflectionClass $class, string $propName): bool |
| 55 |
{ |
| 56 |
do { |
| 57 |
if ($class->hasProperty($propName)) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
} while ($class = $class->getParentClass()); |
| 61 |
|
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
private function hasAttributeNameCollision(\ReflectionClass $class, string $attributeName, string $methodName): bool |
| 66 |
{ |
| 67 |
if ($this->hasPropertyForAccessor($class, $attributeName)) { |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
if ($class->hasMethod($attributeName)) { |
| 72 |
$candidate = $class->getMethod($attributeName); |
| 73 |
if ($candidate->getName() !== $methodName && $this->isReadableAccessorMethod($candidate)) { |
| 74 |
return true; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
$ucAttributeName = ucfirst($attributeName); |
| 79 |
foreach (['get', 'is', 'has', 'can'] as $prefix) { |
| 80 |
$candidateName = $prefix.$ucAttributeName; |
| 81 |
if ($candidateName === $methodName || !$class->hasMethod($candidateName)) { |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
if ($this->isReadableAccessorMethod($class->getMethod($candidateName))) { |
| 86 |
return true; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
private function isReadableAccessorMethod(\ReflectionMethod $method): bool |
| 94 |
{ |
| 95 |
return $method->isPublic() |
| 96 |
&& !$method->isStatic() |
| 97 |
&& !$method->getAttributes(Ignore::class) |
| 98 |
&& !$method->getNumberOfRequiredParameters() |
| 99 |
&& !\in_array((string) $method->getReturnType(), ['void', 'never'], true); |
| 100 |
} |
| 101 |
} |