Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Aug 20, 2021
1 parent 9f776c4 commit 6e21caa
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 63 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
->setFinder(
PhpCsFixer\Finder::create()
->exclude('vendor')
->exclude('tests/Stub')
->in(__DIR__)
)
->setUsingCache(false);
54 changes: 24 additions & 30 deletions src/Visitor/RewriteVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -295,7 +289,7 @@ protected function compatibleFullyQualifiedClass(string $class): array
return [$class, '\\' . $class];
}

protected function baseType() :array
protected function baseType(): array
{
return [
'bool',
Expand Down
24 changes: 12 additions & 12 deletions tests/Cases/CodeGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,45 @@ 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) {
$this->assertTrue($sample->hasMethod($testMethod->name));
$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));
}
}
}
12 changes: 6 additions & 6 deletions tests/Stub/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
13 changes: 9 additions & 4 deletions tests/Stub/Foo.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<?php

declare(strict_types=1);


/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Stub;


class Foo
{
}
}
15 changes: 10 additions & 5 deletions tests/Stub/FooMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<?php

declare(strict_types=1);


/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Stub;


class FooMiddleware
{
public function process($request,$handler)
public function process($request, $handler)
{
return $handler->handle($request);
}
}
}
13 changes: 9 additions & 4 deletions tests/Stub/ParentClass.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<?php

declare(strict_types=1);


/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Stub;


class ParentClass
{
public $valueNoTypeProperty;

public $injectNoTypeProperty;
}
}
3 changes: 1 addition & 2 deletions tests/Stub/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**
* @Annotation
* @Aspect()
* @Aspect
*/
class Test extends ParentClass
{
Expand Down Expand Up @@ -79,5 +79,4 @@ class Test extends ParentClass
* @var self
*/
public $injectSelfProperty;

}

0 comments on commit 6e21caa

Please sign in to comment.