mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-29 11:03:50 +00:00
apply more cs rules for php-cs
This commit is contained in:
@@ -12,10 +12,7 @@ declare(strict_types=1);
|
||||
namespace Chill\DocGeneratorBundle\Serializer\Encoder;
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
||||
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
use function array_keys;
|
||||
use function is_array;
|
||||
|
||||
class DocGenEncoder implements EncoderInterface
|
||||
{
|
||||
@@ -38,21 +35,21 @@ class DocGenEncoder implements EncoderInterface
|
||||
|
||||
private function canonicalizeKey(string $path, string $key): string
|
||||
{
|
||||
return '' === $path ? $key : $path . '_' . $key;
|
||||
return '' === $path ? $key : $path.'_'.$key;
|
||||
}
|
||||
|
||||
private function isAssociative(array $data)
|
||||
{
|
||||
$keys = array_keys($data);
|
||||
$keys = \array_keys($data);
|
||||
|
||||
return array_keys($keys) !== $keys;
|
||||
return \array_keys($keys) !== $keys;
|
||||
}
|
||||
|
||||
private function recusiveEncoding(array $data, array &$result, $path)
|
||||
{
|
||||
if ($this->isAssociative($data)) {
|
||||
foreach ($data as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
if (\is_array($value)) {
|
||||
$this->recusiveEncoding($value, $result, $this->canonicalizeKey($path, $key));
|
||||
} else {
|
||||
$result[$this->canonicalizeKey($path, $key)] = $value;
|
||||
|
@@ -14,14 +14,11 @@ namespace Chill\DocGeneratorBundle\Serializer\Helper;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
use function array_merge;
|
||||
use function is_array;
|
||||
|
||||
class NormalizeNullValueHelper
|
||||
{
|
||||
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 = [], ClassMetadata $classMetadata = null)
|
||||
{
|
||||
$data = [];
|
||||
$data['isNull'] = true;
|
||||
@@ -36,7 +33,7 @@ class NormalizeNullValueHelper
|
||||
} else {
|
||||
$data[$key] = match ($class) {
|
||||
'array', 'bool', 'double', 'float', 'int', 'resource', 'string', 'null' => '',
|
||||
default => $this->normalizer->normalize(null, $format, array_merge(
|
||||
default => $this->normalizer->normalize(null, $format, \array_merge(
|
||||
$this->getContextForAttribute($key, $context, $classMetadata),
|
||||
['docgen:expects' => $class]
|
||||
)),
|
||||
@@ -57,10 +54,10 @@ class NormalizeNullValueHelper
|
||||
|
||||
if (null !== $attributeMetadata) {
|
||||
/** @var \Symfony\Component\Serializer\Mapping\AttributeMetadata $attributeMetadata */
|
||||
$initialContext = array_merge(
|
||||
$initialContext = \array_merge(
|
||||
$initialContext,
|
||||
$attributeMetadata->getNormalizationContextForGroups(
|
||||
is_array($initialContext['groups']) ? $initialContext['groups'] : [$initialContext['groups']]
|
||||
\is_array($initialContext['groups']) ? $initialContext['groups'] : [$initialContext['groups']]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@@ -11,7 +11,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\DocGeneratorBundle\Serializer\Normalizer;
|
||||
|
||||
use ArrayObject;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\Common\Collections\ReadableCollection;
|
||||
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
|
||||
@@ -26,10 +25,10 @@ class CollectionDocGenNormalizer implements ContextAwareNormalizerInterface, Nor
|
||||
use NormalizerAwareTrait;
|
||||
|
||||
/**
|
||||
* @param Collection $object
|
||||
* @param Collection $object
|
||||
* @param string|null $format
|
||||
*
|
||||
* @return array|ArrayObject|bool|float|int|string|void|null
|
||||
* @return array|\ArrayObject|bool|float|int|string|void|null
|
||||
*/
|
||||
public function normalize($object, $format = null, array $context = [])
|
||||
{
|
||||
|
@@ -14,8 +14,6 @@ namespace Chill\DocGeneratorBundle\Serializer\Normalizer;
|
||||
use Chill\DocGeneratorBundle\Serializer\Helper\NormalizeNullValueHelper;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Doctrine\Common\Collections\ReadableCollection;
|
||||
use ReflectionClass;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccess;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccessor;
|
||||
use Symfony\Component\Serializer\Exception\ExceptionInterface;
|
||||
@@ -28,15 +26,6 @@ use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
use function array_filter;
|
||||
use function array_key_exists;
|
||||
use function array_merge;
|
||||
use function get_class;
|
||||
use function implode;
|
||||
use function in_array;
|
||||
use function is_array;
|
||||
use function is_object;
|
||||
|
||||
class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInterface
|
||||
{
|
||||
use NormalizerAwareTrait;
|
||||
@@ -55,27 +44,22 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
$classMetadataKey = $object ?? $context['docgen:expects'] ?? null;
|
||||
|
||||
if (null === $classMetadataKey) {
|
||||
throw new RuntimeException('Could not determine the metadata for this object. Either provide a non-null object, or a "docgen:expects" key in the context');
|
||||
throw new \RuntimeException('Could not determine the metadata for this object. Either provide a non-null object, or a "docgen:expects" key in the context');
|
||||
}
|
||||
|
||||
if (!$this->classMetadataFactory->hasMetadataFor($classMetadataKey)) {
|
||||
throw new LogicException(sprintf(
|
||||
'This object does not have metadata: %s. Add groups on this entity to allow to serialize with the format %s and groups %s',
|
||||
is_object($object) ? $object::class : '(todo' /*$context['docgen:expects'],*/ ,
|
||||
$format,
|
||||
implode(', ', ($context['groups'] ?? []))
|
||||
));
|
||||
throw new LogicException(sprintf('This object does not have metadata: %s. Add groups on this entity to allow to serialize with the format %s and groups %s', \is_object($object) ? $object::class : '(todo' /* $context['docgen:expects'], */ , $format, \implode(', ', $context['groups'] ?? [])));
|
||||
}
|
||||
|
||||
$metadata = $this->classMetadataFactory->getMetadataFor($classMetadataKey);
|
||||
$expectedGroups = array_key_exists(AbstractNormalizer::GROUPS, $context) ?
|
||||
is_array($context[AbstractNormalizer::GROUPS]) ? $context[AbstractNormalizer::GROUPS] : [$context[AbstractNormalizer::GROUPS]]
|
||||
$expectedGroups = \array_key_exists(AbstractNormalizer::GROUPS, $context) ?
|
||||
\is_array($context[AbstractNormalizer::GROUPS]) ? $context[AbstractNormalizer::GROUPS] : [$context[AbstractNormalizer::GROUPS]]
|
||||
: [];
|
||||
$attributes = array_filter(
|
||||
$attributes = \array_filter(
|
||||
$metadata->getAttributesMetadata(),
|
||||
static function (AttributeMetadata $a) use ($expectedGroups) {
|
||||
foreach ($a->getGroups() as $g) {
|
||||
if (in_array($g, $expectedGroups, true)) {
|
||||
if (\in_array($g, $expectedGroups, true)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -93,10 +77,10 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
|
||||
public function supportsNormalization($data, $format = null): bool
|
||||
{
|
||||
return 'docgen' === $format && (is_object($data) || null === $data);
|
||||
return 'docgen' === $format && (\is_object($data) || null === $data);
|
||||
}
|
||||
|
||||
private function getExpectedType(AttributeMetadata $attribute, ReflectionClass $reflection): string
|
||||
private function getExpectedType(AttributeMetadata $attribute, \ReflectionClass $reflection): string
|
||||
{
|
||||
$type = null;
|
||||
|
||||
@@ -104,48 +88,32 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
// we have to get the expected content
|
||||
if ($reflection->hasProperty($attribute->getName())) {
|
||||
if (!$reflection->getProperty($attribute->getName())->hasType()) {
|
||||
throw new \LogicException(sprintf(
|
||||
'Could not determine how the content is determined for the attribute %s on class %s. Add a type on this property',
|
||||
$attribute->getName(),
|
||||
$reflection->getName()
|
||||
));
|
||||
throw new \LogicException(sprintf('Could not determine how the content is determined for the attribute %s on class %s. Add a type on this property', $attribute->getName(), $reflection->getName()));
|
||||
}
|
||||
|
||||
$type = $reflection->getProperty($attribute->getName())->getType();
|
||||
} elseif ($reflection->hasMethod($method = 'get' . ucfirst($attribute->getName()))) {
|
||||
} elseif ($reflection->hasMethod($method = 'get'.ucfirst($attribute->getName()))) {
|
||||
if (!$reflection->getMethod($method)->hasReturnType()) {
|
||||
throw new \LogicException(sprintf(
|
||||
'Could not determine how the content is determined for the attribute %s on class %s. Add a return type on the method',
|
||||
$attribute->getName(),
|
||||
$reflection->getName()
|
||||
));
|
||||
throw new \LogicException(sprintf('Could not determine how the content is determined for the attribute %s on class %s. Add a return type on the method', $attribute->getName(), $reflection->getName()));
|
||||
}
|
||||
|
||||
$type = $reflection->getMethod($method)->getReturnType();
|
||||
} elseif ($reflection->hasMethod($method = 'is' . ucfirst($attribute->getName()))) {
|
||||
} elseif ($reflection->hasMethod($method = 'is'.ucfirst($attribute->getName()))) {
|
||||
if (!$reflection->getMethod($method)->hasReturnType()) {
|
||||
throw new \LogicException(sprintf(
|
||||
'Could not determine how the content is determined for the attribute %s on class %s. Add a return type on the method',
|
||||
$attribute->getName(),
|
||||
$reflection->getName()
|
||||
));
|
||||
throw new \LogicException(sprintf('Could not determine how the content is determined for the attribute %s on class %s. Add a return type on the method', $attribute->getName(), $reflection->getName()));
|
||||
}
|
||||
|
||||
$type = $reflection->getMethod($method)->getReturnType();
|
||||
} elseif ($reflection->hasMethod($attribute->getName())) {
|
||||
if (!$reflection->getMethod($attribute->getName())->hasReturnType()) {
|
||||
throw new \LogicException(sprintf(
|
||||
'Could not determine how the content is determined for the attribute %s on class %s. Add a return type on the method',
|
||||
$attribute->getName(),
|
||||
$reflection->getName()
|
||||
));
|
||||
throw new \LogicException(sprintf('Could not determine how the content is determined for the attribute %s on class %s. Add a return type on the method', $attribute->getName(), $reflection->getName()));
|
||||
}
|
||||
|
||||
$type = $reflection->getMethod($attribute->getName())->getReturnType();
|
||||
} else {
|
||||
$reflection = $reflection->getParentClass();
|
||||
}
|
||||
} while (null === $type && $reflection instanceof ReflectionClass);
|
||||
} while (null === $type && $reflection instanceof \ReflectionClass);
|
||||
|
||||
if ($type instanceof \ReflectionNamedType) {
|
||||
return $type->getName();
|
||||
@@ -156,22 +124,19 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
return ReadableCollection::class;
|
||||
}
|
||||
|
||||
$class = new ReflectionClass($classString);
|
||||
$class = new \ReflectionClass($classString);
|
||||
|
||||
if ($class->implementsInterface(ReadableCollection::class)) {
|
||||
return ReadableCollection::class;
|
||||
}
|
||||
}
|
||||
|
||||
throw new \LogicException(sprintf("The automatic normalization of intersection types is not supported, unless a %s is contained in the intersected types", ReadableCollection::class));
|
||||
throw new \LogicException(sprintf('The automatic normalization of intersection types is not supported, unless a %s is contained in the intersected types', ReadableCollection::class));
|
||||
} elseif (null === $type ?? null) {
|
||||
throw new \LogicException(sprintf(
|
||||
'Could not determine the type for this attribute: %s. Add a return type to the method or property declaration',
|
||||
$attribute->getName()
|
||||
));
|
||||
throw new \LogicException(sprintf('Could not determine the type for this attribute: %s. Add a return type to the method or property declaration', $attribute->getName()));
|
||||
}
|
||||
|
||||
throw new \LogicException(sprintf("The automatic normalization of %s is not supported", $type::class));
|
||||
throw new \LogicException(sprintf('The automatic normalization of %s is not supported', $type::class));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,13 +176,13 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
return $normalizer->normalize($keys, $format, $context, $metadata);
|
||||
}
|
||||
|
||||
private function normalizeNullOutputValue(mixed $format, array $context, AttributeMetadata $attribute, ReflectionClass $reflection)
|
||||
private function normalizeNullOutputValue(mixed $format, array $context, AttributeMetadata $attribute, \ReflectionClass $reflection)
|
||||
{
|
||||
$type = $this->getExpectedType($attribute, $reflection);
|
||||
|
||||
switch ($type) {
|
||||
case 'array':
|
||||
if (in_array('is-translatable', $attribute->getNormalizationContextForGroups(['docgen:read']), true)) {
|
||||
if (\in_array('is-translatable', $attribute->getNormalizationContextForGroups(['docgen:read']), true)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -237,7 +202,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
return $this->normalizer->normalize(
|
||||
null,
|
||||
$format,
|
||||
array_merge(
|
||||
\array_merge(
|
||||
$context,
|
||||
['docgen:expects' => $type]
|
||||
)
|
||||
@@ -246,13 +211,11 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $object
|
||||
* @param $format
|
||||
* @param array|AttributeMetadata[] $attributes
|
||||
*
|
||||
* @throws ExceptionInterface
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws ExceptionInterface
|
||||
*/
|
||||
private function normalizeObject($object, $format, array $context, array $expectedGroups, ClassMetadata $metadata, array $attributes)
|
||||
{
|
||||
@@ -269,10 +232,10 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
/** @var AttributeMetadata $attribute */
|
||||
$value = $this->propertyAccess->getValue($object, $attribute->getName());
|
||||
$key = $attribute->getSerializedName() ?? $attribute->getName();
|
||||
$objectContext = array_merge(
|
||||
$objectContext = \array_merge(
|
||||
$context,
|
||||
$attribute->getNormalizationContextForGroups(
|
||||
is_array($context['groups']) ? $context['groups'] : [$context['groups']]
|
||||
\is_array($context['groups']) ? $context['groups'] : [$context['groups']]
|
||||
)
|
||||
);
|
||||
$isTranslatable = $objectContext['is-translatable'] ?? false;
|
||||
@@ -284,7 +247,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
// when normalizing collection, we should not preserve keys (to ensure that the result is a list)
|
||||
// this is why we make call to the normalizer again to use the CollectionDocGenNormalizer
|
||||
$data[$key] =
|
||||
$this->normalizer->normalize($value, $format, array_merge(
|
||||
$this->normalizer->normalize($value, $format, \array_merge(
|
||||
$objectContext,
|
||||
$attribute->getNormalizationContextForGroups($expectedGroups)
|
||||
));
|
||||
@@ -293,15 +256,15 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
|
||||
foreach ($value as $k => $v) {
|
||||
$arr[$k] =
|
||||
$this->normalizer->normalize($v, $format, array_merge(
|
||||
$this->normalizer->normalize($v, $format, \array_merge(
|
||||
$objectContext,
|
||||
$attribute->getNormalizationContextForGroups($expectedGroups)
|
||||
));
|
||||
}
|
||||
$data[$key] = $arr;
|
||||
} elseif (is_object($value)) {
|
||||
} elseif (\is_object($value)) {
|
||||
$data[$key] =
|
||||
$this->normalizer->normalize($value, $format, array_merge(
|
||||
$this->normalizer->normalize($value, $format, \array_merge(
|
||||
$objectContext,
|
||||
$attribute->getNormalizationContextForGroups($expectedGroups)
|
||||
));
|
||||
|
Reference in New Issue
Block a user