Skip to content

Commit

Permalink
Merge pull request #1530 from scyzoryck/rector-implementation
Browse files Browse the repository at this point in the history
build(ci): Add Rector into static analysis.
  • Loading branch information
scyzoryck committed Jan 3, 2024
2 parents 4ff99e4 + fb5a502 commit 39096dd
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 45 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/static-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig) {
$rectorConfig->paths([
__DIR__ . '/src',
]);
$rectorConfig->sets([LevelSetList::UP_TO_PHP_74]);
};
4 changes: 1 addition & 3 deletions src/Accessor/DefaultAccessorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Annotation/ReadOnly.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
16 changes: 5 additions & 11 deletions src/Metadata/Driver/AnnotationOrAttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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),
);
}
Expand All @@ -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),
);
}
Expand All @@ -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),
);
}
Expand Down
12 changes: 3 additions & 9 deletions src/Metadata/Driver/AttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}
Expand All @@ -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),
);
}
Expand All @@ -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),
);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Metadata/Driver/AttributeDriver/AttributeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Expand Down
16 changes: 4 additions & 12 deletions src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,13 @@ private function getDocBlocTypeHint($reflector): ?string
// Generic array syntax: array<Product> | array<\Foo\Bar\Product> | array<int,Product>
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<int, ' . implode(',', $resolvedTypes) . '>';
}
Expand Down Expand Up @@ -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)));
}

/**
Expand Down Expand Up @@ -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),
));
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/Ordering/AlphabeticalPropertyOrderingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/XmlSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 39096dd

Please sign in to comment.