diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index b9a08df..8afd75f 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -84,6 +84,7 @@ ->setFinder( PhpCsFixer\Finder::create() ->exclude('vendor') + ->exclude('tests/Stub') ->in(__DIR__) ) ->setUsingCache(false); diff --git a/src/Visitor/RewriteVisitor.php b/src/Visitor/RewriteVisitor.php index e8003ea..8730d8e 100644 --- a/src/Visitor/RewriteVisitor.php +++ b/src/Visitor/RewriteVisitor.php @@ -136,28 +136,28 @@ protected function generateClassPropertyAttributes(Node\Stmt\Property $node): No continue; } /** @var Node\Identifier|Node\Name $type */ - [$type,$comment] = $this->guessClassPropertyType($node,$property); - if($type) { - if( + [$type,$comment] = $this->guessClassPropertyType($node, $property); + if ($type) { + if ( $comment // type is from comment like @var && ($parentClass = $this->reflection->getParentClass()) // is subclass && $parentClass->hasProperty($property->name) // parentClass have same property - && !$parentClass->getProperty($property->name)->hasType() // parentClass property not have type, subclass same on + && ! $parentClass->getProperty($property->name)->hasType() // parentClass property not have type, subclass same on ) { - if($annotation instanceof Inject) { + if ($annotation instanceof Inject) { $args = ['value' => $this->getInjectPropertyType($type)]; } } else { $node->type = $type; } - if($comment) { + if ($comment) { $comments = $this->removeAnnotationFromComments($comments, 'var'); } } $node->attrGroups[] = new Node\AttributeGroup([ new Node\Attribute( $this->guessName($this->getClassName($annotation)), - $this->buildAttributeArgs($annotation,$args ?? []), + $this->buildAttributeArgs($annotation, $args ?? []), ), ]); $comments = $this->removeAnnotationFromComments($comments, $annotation); @@ -167,45 +167,39 @@ protected function generateClassPropertyAttributes(Node\Stmt\Property $node): No return $node; } - protected function getInjectPropertyType(Node\Name $type) :?string + protected function getInjectPropertyType(Node\Name $type): ?string { - if(in_array($type->toString(),$this->baseType(),true)) { + if (in_array($type->toString(), $this->baseType(), true)) { return $type->toString(); } return match (true) { - $type->isRelative(), $type->isQualified(), $type->isUnqualified() => $this->namespace->name->toString().'\\' - .$type->toString(), + $type->isRelative(), $type->isQualified(), $type->isUnqualified() => $this->namespace->name->toString() . '\\' + . $type->toString(), default => $type->toString(), }; } - protected function guessName(string $name) :Node\Name + protected function guessName(string $name): Node\Name { return str_contains($name, '\\') ? new Node\Name\FullyQualified($name) : new Node\Name($name); } - /** - * @param Node\Stmt\Property $node - * @param ReflectionProperty $property - * - * @return array - */ - protected function guessClassPropertyType(Node\Stmt\Property $node,ReflectionProperty $property) :array + protected function guessClassPropertyType(Node\Stmt\Property $node, ReflectionProperty $property): array { $fromComment = false; - if($node->type) { - return [$node->type,$fromComment]; + if ($node->type) { + return [$node->type, $fromComment]; } - if($type = $this->readTypeFromPropertyComment($property)) { + if ($type = $this->readTypeFromPropertyComment($property)) { $fromComment = true; - if(str_ends_with($type,'[]')) { - return [new Node\Name('array'),$fromComment]; + if (str_ends_with($type, '[]')) { + return [new Node\Name('array'), $fromComment]; } - if($type !== 'callable') { - return [$this->guessName($type),$fromComment]; + if ($type !== 'callable') { + return [$this->guessName($type), $fromComment]; } } - return [null,false]; + return [null, false]; } protected function readTypeFromPropertyComment(ReflectionProperty $property): ?string @@ -223,9 +217,9 @@ protected function readTypeFromPropertyComment(ReflectionProperty $property): ?s return $type; } - protected function buildAttributeArgs(AbstractAnnotation $annotation,array $args = []): array + protected function buildAttributeArgs(AbstractAnnotation $annotation, array $args = []): array { - return $this->factory->args(array_merge($args,$this->getNotDefaultPropertyFromAnnotation($annotation))); + return $this->factory->args(array_merge($args, $this->getNotDefaultPropertyFromAnnotation($annotation))); } protected function getNotDefaultPropertyFromAnnotation(AbstractAnnotation $annotation): array @@ -295,7 +289,7 @@ protected function compatibleFullyQualifiedClass(string $class): array return [$class, '\\' . $class]; } - protected function baseType() :array + protected function baseType(): array { return [ 'bool', diff --git a/tests/Cases/CodeGeneratorTest.php b/tests/Cases/CodeGeneratorTest.php index 731879c..e457769 100644 --- a/tests/Cases/CodeGeneratorTest.php +++ b/tests/Cases/CodeGeneratorTest.php @@ -30,18 +30,18 @@ public function testRewriteClass(): void $test = $this->makeTestClass(); $sample = new ReflectionClass(Attribute::class); $this->assertEquals($test->getParentClass(), $sample->getParentClass()); - $this->assertEquals($test->getDocComment(),$sample->getDocComment()); + $this->assertEquals($test->getDocComment(), $sample->getDocComment()); foreach ($test->getProperties() as $testProperty) { $this->assertTrue($sample->hasProperty($testProperty->name)); - $this->assertEquals($testProperty->hasType(),$testProperty->hasType()); - if($testProperty->hasType()) { + $this->assertEquals($testProperty->hasType(), $testProperty->hasType()); + if ($testProperty->hasType()) { $this->assertEquals($testProperty->getType(), $testProperty->getType()); } $sampleProperty = $sample->getProperty($testProperty->name); $testAttributes = $testProperty->getAttributes(); $sampleAttributes = $sampleProperty->getAttributes(); - for ($index = 0, $indexMax = count($testAttributes);$index < $indexMax;$index ++) { - $this->assertAnnotationAttribute($testAttributes[$index],$sampleAttributes[$index]); + for ($index = 0, $indexMax = count($testAttributes); $index < $indexMax; ++$index) { + $this->assertAnnotationAttribute($testAttributes[$index], $sampleAttributes[$index]); } } foreach ($test->getMethods() as $testMethod) { @@ -49,26 +49,26 @@ public function testRewriteClass(): void $sampleMethod = $sample->getMethod($test->name); $testAttributes = $testMethod->getAttributes(); $sampleAttributes = $sampleMethod->getAttributes(); - for ($index = 0, $indexMax = count($testAttributes);$index < $indexMax;$index ++) { - $this->assertAnnotationAttribute($testAttributes[$index],$sampleAttributes[$index]); + for ($index = 0, $indexMax = count($testAttributes); $index < $indexMax; ++$index) { + $this->assertAnnotationAttribute($testAttributes[$index], $sampleAttributes[$index]); } - $this->assertEquals($testMethod->getAttributes(),$sampleMethod->getAttributes()); + $this->assertEquals($testMethod->getAttributes(), $sampleMethod->getAttributes()); } } - protected function makeTestClass() :ReflectionClass + protected function makeTestClass(): ReflectionClass { $generator = new CodeGenerator(new Ast(new AnnotationReader())); $code = $generator->generate(file_get_contents(__DIR__ . '/../Stub/Test.php')); - file_put_contents(__DIR__ . '/../Stub/TestAttribute.php',str_replace('class Test','class TestAttribute',$code)); + file_put_contents(__DIR__ . '/../Stub/TestAttribute.php', str_replace('class Test', 'class TestAttribute', $code)); return new ReflectionClass(TestAttribute::class); } - protected function assertAnnotationAttribute($test,$sample) :void + protected function assertAnnotationAttribute($test, $sample): void { $class = new ReflectionClass($test); foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { - $this->assertEquals($property->getValue($test),$property->getValue($sample)); + $this->assertEquals($property->getValue($test), $property->getValue($sample)); } } } diff --git a/tests/Stub/Attribute.php b/tests/Stub/Attribute.php index 2f43112..5ade496 100644 --- a/tests/Stub/Attribute.php +++ b/tests/Stub/Attribute.php @@ -24,25 +24,25 @@ class Attribute extends ParentClass /** * @var int[] */ - #[Value(key:"int_array")] + #[Value(key: 'int_array')] public $valueNoTypeProperty; - #[Value(key:"int")] + #[Value(key: 'int')] public int $valueHasTypeProperty; - #[Value(key:"int_array")] + #[Value(key: 'int_array')] public array $valueArrayTypeProperty; /** * @var callable */ - #[Value(key:"callable")] + #[Value(key: 'callable')] public $valueCallableProperty; - #[Value(key:"int_or_null")] + #[Value(key: 'int_or_null')] public ?int $valueIntOrNullProperty; - #[Value(key:"foo_or_null")] + #[Value(key: 'foo_or_null')] public null|Foo $valueFooOrNullProperty2; #[Inject(value: Foo::class)] diff --git a/tests/Stub/Foo.php b/tests/Stub/Foo.php index 7daa827..6fd96e1 100644 --- a/tests/Stub/Foo.php +++ b/tests/Stub/Foo.php @@ -1,11 +1,16 @@ handle($request); } -} \ No newline at end of file +} diff --git a/tests/Stub/ParentClass.php b/tests/Stub/ParentClass.php index bf57caf..99cd76c 100644 --- a/tests/Stub/ParentClass.php +++ b/tests/Stub/ParentClass.php @@ -1,14 +1,19 @@