From e8e2dd8e2b8c7251dc90b4a1218887fbfaf49203 Mon Sep 17 00:00:00 2001 From: Marcin Czarnecki Date: Tue, 2 Jan 2024 16:15:24 +0100 Subject: [PATCH 1/2] build(ci): Add Rector into static analysis. --- .github/workflows/static-analysis.yaml | 4 ++++ composer.json | 1 + rector.php | 13 +++++++++++++ 3 files changed, 18 insertions(+) create mode 100644 rector.php diff --git a/.github/workflows/static-analysis.yaml b/.github/workflows/static-analysis.yaml index cbda4cc36..c1d5d635c 100644 --- a/.github/workflows/static-analysis.yaml +++ b/.github/workflows/static-analysis.yaml @@ -38,3 +38,7 @@ jobs: - name: "Run a static analysis with phpstan/phpstan" run: "vendor/bin/phpstan analyse --error-format=checkstyle | cs2pr" + + - name: "Run a static analysis with rector/rector" + run: "vendor/bin/rector --dry-run" + if: ${{ matrix.php-version == 8.2 }} diff --git a/composer.json b/composer.json index 857e77617..4d754518e 100644 --- a/composer.json +++ b/composer.json @@ -41,6 +41,7 @@ "phpstan/phpstan": "^1.0.2", "phpunit/phpunit": "^9.0 || ^10.0", "psr/container": "^1.0 || ^2.0", + "rector/rector": "^0.18.13", "symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0", "symfony/expression-language": "^5.4 || ^6.0 || ^7.0", "symfony/filesystem": "^5.4 || ^6.0 || ^7.0", diff --git a/rector.php b/rector.php new file mode 100644 index 000000000..6cc8584b1 --- /dev/null +++ b/rector.php @@ -0,0 +1,13 @@ +paths([ + __DIR__ . '/src', + ]); + $rectorConfig->sets([LevelSetList::UP_TO_PHP_74]); +}; From fb5a502d92516ad69e8c14982da338a31c548bed Mon Sep 17 00:00:00 2001 From: Marcin Czarnecki Date: Tue, 2 Jan 2024 16:24:50 +0100 Subject: [PATCH 2/2] build(code style): Update src to 7.4 --- src/Accessor/DefaultAccessorStrategy.php | 4 +--- src/Annotation/ReadOnly.php | 4 +++- .../Driver/AnnotationOrAttributeDriver.php | 16 +++++----------- src/Metadata/Driver/AttributeDriver.php | 12 +++--------- .../Driver/AttributeDriver/AttributeReader.php | 4 +--- .../DocBlockDriver/DocBlockTypeResolver.php | 16 ++++------------ .../AlphabeticalPropertyOrderingStrategy.php | 4 +--- src/XmlSerializationVisitor.php | 4 +--- 8 files changed, 19 insertions(+), 45 deletions(-) diff --git a/src/Accessor/DefaultAccessorStrategy.php b/src/Accessor/DefaultAccessorStrategy.php index a3c25a326..ed8d647ce 100644 --- a/src/Accessor/DefaultAccessorStrategy.php +++ b/src/Accessor/DefaultAccessorStrategy.php @@ -86,9 +86,7 @@ public function getValue(object $object, PropertyMetadata $metadata, Serializati $accessor = $this->readAccessors[$metadata->class] ?? null; if (null === $accessor) { - $accessor = \Closure::bind(static function ($o, $name) { - return $o->$name; - }, null, $metadata->class); + $accessor = \Closure::bind(static fn ($o, $name) => $o->$name, null, $metadata->class); $this->readAccessors[$metadata->class] = $accessor; } diff --git a/src/Annotation/ReadOnly.php b/src/Annotation/ReadOnly.php index 23bff2630..b5344e10e 100644 --- a/src/Annotation/ReadOnly.php +++ b/src/Annotation/ReadOnly.php @@ -2,4 +2,6 @@ declare(strict_types=1); -class_alias('JMS\Serializer\Annotation\DeprecatedReadOnly', 'JMS\Serializer\Annotation\ReadOnly'); +use JMS\Serializer\Annotation\DeprecatedReadOnly; + +class_alias(DeprecatedReadOnly::class, 'JMS\Serializer\Annotation\ReadOnly'); diff --git a/src/Metadata/Driver/AnnotationOrAttributeDriver.php b/src/Metadata/Driver/AnnotationOrAttributeDriver.php index 8e1b52343..92becb724 100644 --- a/src/Metadata/Driver/AnnotationOrAttributeDriver.php +++ b/src/Metadata/Driver/AnnotationOrAttributeDriver.php @@ -290,10 +290,10 @@ public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadat } } - if (!$configured) { + // if (!$configured) { // return null; // uncomment the above line afetr a couple of months - } + // } return $classMetadata; } @@ -307,9 +307,7 @@ protected function getClassAnnotations(\ReflectionClass $class): array if (PHP_VERSION_ID >= 80000) { $annotations = array_map( - static function (\ReflectionAttribute $attribute): object { - return $attribute->newInstance(); - }, + static fn (\ReflectionAttribute $attribute): object => $attribute->newInstance(), $class->getAttributes(SerializerAttribute::class, \ReflectionAttribute::IS_INSTANCEOF), ); } @@ -330,9 +328,7 @@ protected function getMethodAnnotations(\ReflectionMethod $method): array if (PHP_VERSION_ID >= 80000) { $annotations = array_map( - static function (\ReflectionAttribute $attribute): object { - return $attribute->newInstance(); - }, + static fn (\ReflectionAttribute $attribute): object => $attribute->newInstance(), $method->getAttributes(SerializerAttribute::class, \ReflectionAttribute::IS_INSTANCEOF), ); } @@ -353,9 +349,7 @@ protected function getPropertyAnnotations(\ReflectionProperty $property): array if (PHP_VERSION_ID >= 80000) { $annotations = array_map( - static function (\ReflectionAttribute $attribute): object { - return $attribute->newInstance(); - }, + static fn (\ReflectionAttribute $attribute): object => $attribute->newInstance(), $property->getAttributes(SerializerAttribute::class, \ReflectionAttribute::IS_INSTANCEOF), ); } diff --git a/src/Metadata/Driver/AttributeDriver.php b/src/Metadata/Driver/AttributeDriver.php index e966791c2..2c6c1a2c9 100644 --- a/src/Metadata/Driver/AttributeDriver.php +++ b/src/Metadata/Driver/AttributeDriver.php @@ -14,9 +14,7 @@ class AttributeDriver extends AnnotationOrAttributeDriver protected function getClassAnnotations(\ReflectionClass $class): array { return array_map( - static function (\ReflectionAttribute $attribute): object { - return $attribute->newInstance(); - }, + static fn (\ReflectionAttribute $attribute): object => $attribute->newInstance(), $class->getAttributes(SerializerAttribute::class, \ReflectionAttribute::IS_INSTANCEOF), ); } @@ -27,9 +25,7 @@ static function (\ReflectionAttribute $attribute): object { protected function getMethodAnnotations(\ReflectionMethod $method): array { return array_map( - static function (\ReflectionAttribute $attribute): object { - return $attribute->newInstance(); - }, + static fn (\ReflectionAttribute $attribute): object => $attribute->newInstance(), $method->getAttributes(SerializerAttribute::class, \ReflectionAttribute::IS_INSTANCEOF), ); } @@ -40,9 +36,7 @@ static function (\ReflectionAttribute $attribute): object { protected function getPropertyAnnotations(\ReflectionProperty $property): array { return array_map( - static function (\ReflectionAttribute $attribute): object { - return $attribute->newInstance(); - }, + static fn (\ReflectionAttribute $attribute): object => $attribute->newInstance(), $property->getAttributes(SerializerAttribute::class, \ReflectionAttribute::IS_INSTANCEOF), ); } diff --git a/src/Metadata/Driver/AttributeDriver/AttributeReader.php b/src/Metadata/Driver/AttributeDriver/AttributeReader.php index 91e8aa83a..12da20f58 100644 --- a/src/Metadata/Driver/AttributeDriver/AttributeReader.php +++ b/src/Metadata/Driver/AttributeDriver/AttributeReader.php @@ -85,9 +85,7 @@ private function buildAnnotation(array $attributes): ?SerializerAttribute private function buildAnnotations(array $attributes): array { return array_map( - static function (\ReflectionAttribute $attribute): object { - return $attribute->newInstance(); - }, + static fn (\ReflectionAttribute $attribute): object => $attribute->newInstance(), $attributes, ); } diff --git a/src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php b/src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php index 4e43cf6e0..d5fad5b64 100644 --- a/src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php +++ b/src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php @@ -115,17 +115,13 @@ private function getDocBlocTypeHint($reflector): ?string // Generic array syntax: array | array<\Foo\Bar\Product> | array if ($type instanceof GenericTypeNode) { if ($this->isSimpleType($type->type, 'array')) { - $resolvedTypes = array_map(function (TypeNode $node) use ($reflector) { - return $this->resolveTypeFromTypeNode($node, $reflector); - }, $type->genericTypes); + $resolvedTypes = array_map(fn (TypeNode $node) => $this->resolveTypeFromTypeNode($node, $reflector), $type->genericTypes); return 'array<' . implode(',', $resolvedTypes) . '>'; } if ($this->isSimpleType($type->type, 'list')) { - $resolvedTypes = array_map(function (TypeNode $node) use ($reflector) { - return $this->resolveTypeFromTypeNode($node, $reflector); - }, $type->genericTypes); + $resolvedTypes = array_map(fn (TypeNode $node) => $this->resolveTypeFromTypeNode($node, $reflector), $type->genericTypes); return 'array'; } @@ -194,9 +190,7 @@ private function flattenParamTagValueTypes(string $parameterName, array $varTagV */ private function filterNullFromTypes(array $types): array { - return array_values(array_filter(array_map(function (TypeNode $node) { - return $this->isNullType($node) ? null : $node; - }, $types))); + return array_values(array_filter(array_map(fn (TypeNode $node) => $this->isNullType($node) ? null : $node, $types))); } /** @@ -427,9 +421,7 @@ private function getPhpstanType(\ReflectionClass $declaringClass, string $typeHi return sprintf('array<%s>', implode( ',', - array_map(static function (string $type) use ($reflector, $self) { - return $self->resolveType(trim($type), $reflector); - }, $types), + array_map(static fn (string $type) => $self->resolveType(trim($type), $reflector), $types), )); } } diff --git a/src/Ordering/AlphabeticalPropertyOrderingStrategy.php b/src/Ordering/AlphabeticalPropertyOrderingStrategy.php index a60d3cb09..68db769c0 100644 --- a/src/Ordering/AlphabeticalPropertyOrderingStrategy.php +++ b/src/Ordering/AlphabeticalPropertyOrderingStrategy.php @@ -15,9 +15,7 @@ public function order(array $properties): array { uasort( $properties, - static function (PropertyMetadata $a, PropertyMetadata $b): int { - return strcmp($a->name, $b->name); - }, + static fn (PropertyMetadata $a, PropertyMetadata $b): int => strcmp($a->name, $b->name), ); return $properties; diff --git a/src/XmlSerializationVisitor.php b/src/XmlSerializationVisitor.php index ae150aa66..8406d75b4 100644 --- a/src/XmlSerializationVisitor.php +++ b/src/XmlSerializationVisitor.php @@ -318,9 +318,7 @@ public function visitProperty(PropertyMetadata $metadata, $v): void } if ($addEnclosingElement = !$this->isInLineCollection($metadata) && !$metadata->inline) { - $namespace = null !== $metadata->xmlNamespace - ? $metadata->xmlNamespace - : $this->getClassDefaultNamespace($this->objectMetadataStack->top()); + $namespace = $metadata->xmlNamespace ?? $this->getClassDefaultNamespace($this->objectMetadataStack->top()); $element = $this->createElement($metadata->serializedName, $namespace); $this->currentNode->appendChild($element);