Spaces:
Runtime error
Runtime error
File size: 3,369 Bytes
7a66774 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | <?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Mapping\Loader;
use Symfony\Component\Serializer\Attribute\Ignore;
/**
* Provides methods to detect accessor name collisions during serialization.
*
* @internal
*/
trait AccessorCollisionResolverTrait
{
private function getAttributeNameFromAccessor(\ReflectionClass $class, \ReflectionMethod $method, bool $andMutator): ?string
{
$methodName = $method->name;
$i = match ($methodName[0]) {
's' => $andMutator && str_starts_with($methodName, 'set') ? 3 : null,
'g' => str_starts_with($methodName, 'get') ? 3 : null,
'h' => str_starts_with($methodName, 'has') ? 3 : null,
'c' => str_starts_with($methodName, 'can') ? 3 : null,
'i' => str_starts_with($methodName, 'is') ? 2 : null,
default => null,
};
// ctype_lower check to find out if method looks like accessor but actually is not, e.g. hash, cancel
if (null === $i || ctype_lower($methodName[$i] ?? 'a') || $method->isStatic()) {
return null;
}
if ('s' === $methodName[0] ? !$method->getNumberOfParameters() : ($method->getNumberOfRequiredParameters() || \in_array((string) $method->getReturnType(), ['void', 'never'], true))) {
return null;
}
$attributeName = substr($methodName, $i);
if (!$class->hasProperty($attributeName)) {
$attributeName = lcfirst($attributeName);
}
return $attributeName;
}
private function hasPropertyForAccessor(\ReflectionClass $class, string $propName): bool
{
do {
if ($class->hasProperty($propName)) {
return true;
}
} while ($class = $class->getParentClass());
return false;
}
private function hasAttributeNameCollision(\ReflectionClass $class, string $attributeName, string $methodName): bool
{
if ($this->hasPropertyForAccessor($class, $attributeName)) {
return true;
}
if ($class->hasMethod($attributeName)) {
$candidate = $class->getMethod($attributeName);
if ($candidate->getName() !== $methodName && $this->isReadableAccessorMethod($candidate)) {
return true;
}
}
$ucAttributeName = ucfirst($attributeName);
foreach (['get', 'is', 'has', 'can'] as $prefix) {
$candidateName = $prefix.$ucAttributeName;
if ($candidateName === $methodName || !$class->hasMethod($candidateName)) {
continue;
}
if ($this->isReadableAccessorMethod($class->getMethod($candidateName))) {
return true;
}
}
return false;
}
private function isReadableAccessorMethod(\ReflectionMethod $method): bool
{
return $method->isPublic()
&& !$method->isStatic()
&& !$method->getAttributes(Ignore::class)
&& !$method->getNumberOfRequiredParameters()
&& !\in_array((string) $method->getReturnType(), ['void', 'never'], true);
}
}
|