Merge branch 'fix-phpstan-2023-11-21' into 'master'

Fix type-hinting of DocGenObjectNormalizer and related

See merge request Chill-Projet/chill-bundles!619
This commit is contained in:
Julien Fastré 2023-11-21 12:48:30 +00:00
commit ccf8cc4d6e
2 changed files with 10 additions and 6 deletions

View File

@ -11,14 +11,14 @@ declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Serializer\Helper; namespace Chill\DocGeneratorBundle\Serializer\Helper;
use Symfony\Component\Serializer\Mapping\ClassMetadata; use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class NormalizeNullValueHelper class NormalizeNullValueHelper
{ {
public function __construct(private readonly NormalizerInterface $normalizer, private readonly ?string $discriminatorType = null, private readonly ?string $discriminatorValue = null) {} public function __construct(private readonly NormalizerInterface $normalizer, private readonly ?string $discriminatorType = null, private readonly ?string $discriminatorValue = null) {}
public function normalize(array $attributes, string $format = 'docgen', ?array $context = [], ClassMetadata $classMetadata = null) public function normalize(array $attributes, string $format = 'docgen', ?array $context = [], ClassMetadataInterface $classMetadata = null)
{ {
$data = []; $data = [];
$data['isNull'] = true; $data['isNull'] = true;
@ -44,7 +44,7 @@ class NormalizeNullValueHelper
return $data; return $data;
} }
private function getContextForAttribute(string $key, array $initialContext, ?ClassMetadata $classMetadata): array private function getContextForAttribute(string $key, array $initialContext, ?ClassMetadataInterface $classMetadata): array
{ {
if (null === $classMetadata) { if (null === $classMetadata) {
return $initialContext; return $initialContext;

View File

@ -19,6 +19,7 @@ use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Serializer\Exception\ExceptionInterface; use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Exception\LogicException; use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Mapping\AttributeMetadata; use Symfony\Component\Serializer\Mapping\AttributeMetadata;
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
use Symfony\Component\Serializer\Mapping\ClassMetadata; use Symfony\Component\Serializer\Mapping\ClassMetadata;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
@ -52,12 +53,15 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
} }
$metadata = $this->classMetadataFactory->getMetadataFor($classMetadataKey); $metadata = $this->classMetadataFactory->getMetadataFor($classMetadataKey);
if (!$metadata instanceof ClassMetadata) {
throw new \LogicException('ClassMetadata should be the only one implementation for ClassMetadataInterface. See https://github.com/symfony/symfony/pull/17114');
}
$expectedGroups = \array_key_exists(AbstractNormalizer::GROUPS, $context) ? $expectedGroups = \array_key_exists(AbstractNormalizer::GROUPS, $context) ?
\is_array($context[AbstractNormalizer::GROUPS]) ? $context[AbstractNormalizer::GROUPS] : [$context[AbstractNormalizer::GROUPS]] \is_array($context[AbstractNormalizer::GROUPS]) ? $context[AbstractNormalizer::GROUPS] : [$context[AbstractNormalizer::GROUPS]]
: []; : [];
$attributes = \array_filter( $attributes = \array_filter(
$metadata->getAttributesMetadata(), $metadata->getAttributesMetadata(),
static function (AttributeMetadata $a) use ($expectedGroups) { static function (AttributeMetadataInterface $a) use ($expectedGroups) {
foreach ($a->getGroups() as $g) { foreach ($a->getGroups() as $g) {
if (\in_array($g, $expectedGroups, true)) { if (\in_array($g, $expectedGroups, true)) {
return true; return true;
@ -119,7 +123,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
return $type->getName(); return $type->getName();
} }
if ($type instanceof \ReflectionIntersectionType) { if ($type instanceof \ReflectionIntersectionType) {
foreach (array_map(fn (\ReflectionNamedType $t) => $t->getName(), $type->getTypes()) as $classString) { foreach (array_map(fn (\ReflectionType $t) => $t->getName(), $type->getTypes()) as $classString) {
if (ReadableCollection::class === $classString) { if (ReadableCollection::class === $classString) {
return ReadableCollection::class; return ReadableCollection::class;
} }
@ -211,7 +215,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
} }
/** /**
* @param array|AttributeMetadata[] $attributes * @param array<AttributeMetadata> $attributes
* *
* @return array * @return array
* *