From 1f95f3b7359ce38c011cffa036a4a785d55d21f2 Mon Sep 17 00:00:00 2001 From: Jaapio Date: Fri, 11 Nov 2022 14:24:45 +0100 Subject: [PATCH] Codestyle fixes and static analysis --- src/DocBlock/StandardTagFactory.php | 2 +- src/DocBlock/Tags/Factory/MethodFactory.php | 11 +++++++++-- src/DocBlock/Tags/Method.php | 8 +++++++- src/DocBlock/Tags/Param.php | 9 +++++++-- src/DocBlock/Tags/Property.php | 10 +++++++++- src/DocBlock/Tags/PropertyRead.php | 10 +++++++++- src/DocBlock/Tags/PropertyWrite.php | 10 +++++++++- src/DocBlock/Tags/Return_.php | 10 +++++++++- src/DocBlock/Tags/Var_.php | 10 +++++++++- src/DocBlockFactory.php | 3 +-- .../unit/DocBlock/Tags/Factory/MethodFactoryTest.php | 12 ++++++------ 11 files changed, 76 insertions(+), 19 deletions(-) diff --git a/src/DocBlock/StandardTagFactory.php b/src/DocBlock/StandardTagFactory.php index fa724aa..e5c071c 100644 --- a/src/DocBlock/StandardTagFactory.php +++ b/src/DocBlock/StandardTagFactory.php @@ -177,7 +177,7 @@ final class StandardTagFactory implements TagFactory } if (is_object($handler)) { - Assert::implementsInterface($handler, Factory::class); + Assert::isInstanceOf($handler, Factory::class); $this->tagHandlerMappings[$tagName] = $handler; return; diff --git a/src/DocBlock/Tags/Factory/MethodFactory.php b/src/DocBlock/Tags/Factory/MethodFactory.php index c884743..920be84 100644 --- a/src/DocBlock/Tags/Factory/MethodFactory.php +++ b/src/DocBlock/Tags/Factory/MethodFactory.php @@ -51,7 +51,10 @@ final class MethodFactory implements PHPStanFactory function (MethodTagValueParameterNode $param) use ($context) { return new MethodParameter( trim($param->parameterName, '$'), - $this->typeResolver->createType($param->type, $context) ?? new Mixed_(), + $param->type === null ? new Mixed_() : $this->typeResolver->createType( + $param->type, + $context + ), $param->isReference, $param->isVariadic, (string) $param->defaultValue @@ -69,6 +72,10 @@ final class MethodFactory implements PHPStanFactory private function createReturnType(MethodTagValueNode $tagValue, Context $context): Type { - return $this->typeResolver->createType($tagValue->returnType, $context) ?? new Void_(); + if ($tagValue->returnType === null) { + return new Void_(); + } + + return $this->typeResolver->createType($tagValue->returnType, $context); } } diff --git a/src/DocBlock/Tags/Method.php b/src/DocBlock/Tags/Method.php index bbb2062..02aa2b4 100644 --- a/src/DocBlock/Tags/Method.php +++ b/src/DocBlock/Tags/Method.php @@ -63,6 +63,7 @@ final class Method extends BaseTag implements Factory\StaticMethod /** * @param array> $arguments + * @param MethodParameter[] $parameters * @phpstan-param array $arguments */ public function __construct( @@ -90,6 +91,10 @@ final class Method extends BaseTag implements Factory\StaticMethod $this->parameters = $parameters ?? $this->fromLegacyArguments($arguments); } + /** + * @deprecated Create using static factory is deprecated, + * this method should not be called directly by library consumers + */ public static function create( string $body, ?TypeResolver $typeResolver = null, @@ -261,7 +266,7 @@ final class Method extends BaseTag implements Factory\StaticMethod { $arguments = []; foreach ($this->parameters as $parameter) { - $arguments[] = ($parameter->getType() ?? new Mixed_()) . ' ' . + $arguments[] = $parameter->getType() . ' ' . ($parameter->isReference() ? '&' : '') . ($parameter->isVariadic() ? '...' : '') . '$' . $parameter->getName(); @@ -334,6 +339,7 @@ final class Method extends BaseTag implements Factory\StaticMethod /** * @param array{name: string, type: Type} $arguments + * @phpstan-param array $arguments * * @return MethodParameter[] */ diff --git a/src/DocBlock/Tags/Param.php b/src/DocBlock/Tags/Param.php index c4c0880..cb14abe 100644 --- a/src/DocBlock/Tags/Param.php +++ b/src/DocBlock/Tags/Param.php @@ -61,7 +61,8 @@ final class Param extends TagWithType implements Factory\StaticMethod } /** - * @deprecated Create using static factory is deprecated, this method should not be called directly by library consumers + * @deprecated Create using static factory is deprecated, + * this method should not be called directly by library consumers */ public static function create( string $body, @@ -69,7 +70,11 @@ final class Param extends TagWithType implements Factory\StaticMethod ?DescriptionFactory $descriptionFactory = null, ?TypeContext $context = null ): self { - trigger_error('Create using static factory is deprecated, this method should not be called directly by library consumers', E_USER_DEPRECATED); + trigger_error( + 'Create using static factory is deprecated, this method should not be called directly + by library consumers', + E_USER_DEPRECATED + ); Assert::stringNotEmpty($body); Assert::notNull($typeResolver); Assert::notNull($descriptionFactory); diff --git a/src/DocBlock/Tags/Property.php b/src/DocBlock/Tags/Property.php index 429e7a6..b3d9df4 100644 --- a/src/DocBlock/Tags/Property.php +++ b/src/DocBlock/Tags/Property.php @@ -49,13 +49,21 @@ final class Property extends TagWithType implements Factory\StaticMethod $this->description = $description; } + /** + * @deprecated Create using static factory is deprecated, + * this method should not be called directly by library consumers + */ public static function create( string $body, ?TypeResolver $typeResolver = null, ?DescriptionFactory $descriptionFactory = null, ?TypeContext $context = null ): self { - trigger_error('Create using static factory is deprecated, this method should not be called directly by library consumers', E_USER_DEPRECATED); + trigger_error( + 'Create using static factory is deprecated, this method should not be called directly + by library consumers', + E_USER_DEPRECATED + ); Assert::stringNotEmpty($body); Assert::notNull($typeResolver); Assert::notNull($descriptionFactory); diff --git a/src/DocBlock/Tags/PropertyRead.php b/src/DocBlock/Tags/PropertyRead.php index 150d451..ccffde6 100644 --- a/src/DocBlock/Tags/PropertyRead.php +++ b/src/DocBlock/Tags/PropertyRead.php @@ -49,13 +49,21 @@ final class PropertyRead extends TagWithType implements Factory\StaticMethod $this->description = $description; } + /** + * @deprecated Create using static factory is deprecated, + * this method should not be called directly by library consumers + */ public static function create( string $body, ?TypeResolver $typeResolver = null, ?DescriptionFactory $descriptionFactory = null, ?TypeContext $context = null ): self { - trigger_error('Create using static factory is deprecated, this method should not be called directly by library consumers', E_USER_DEPRECATED); + trigger_error( + 'Create using static factory is deprecated, this method should not be called directly + by library consumers', + E_USER_DEPRECATED + ); Assert::stringNotEmpty($body); Assert::notNull($typeResolver); Assert::notNull($descriptionFactory); diff --git a/src/DocBlock/Tags/PropertyWrite.php b/src/DocBlock/Tags/PropertyWrite.php index debbb45..35dac20 100644 --- a/src/DocBlock/Tags/PropertyWrite.php +++ b/src/DocBlock/Tags/PropertyWrite.php @@ -49,13 +49,21 @@ final class PropertyWrite extends TagWithType implements Factory\StaticMethod $this->description = $description; } + /** + * @deprecated Create using static factory is deprecated, + * this method should not be called directly by library consumers + */ public static function create( string $body, ?TypeResolver $typeResolver = null, ?DescriptionFactory $descriptionFactory = null, ?TypeContext $context = null ): self { - trigger_error('Create using static factory is deprecated, this method should not be called directly by library consumers', E_USER_DEPRECATED); + trigger_error( + 'Create using static factory is deprecated, this method should not be called directly + by library consumers', + E_USER_DEPRECATED + ); Assert::stringNotEmpty($body); Assert::notNull($typeResolver); Assert::notNull($descriptionFactory); diff --git a/src/DocBlock/Tags/Return_.php b/src/DocBlock/Tags/Return_.php index af3a24a..2a1efb7 100644 --- a/src/DocBlock/Tags/Return_.php +++ b/src/DocBlock/Tags/Return_.php @@ -36,13 +36,21 @@ final class Return_ extends TagWithType implements Factory\StaticMethod $this->description = $description; } + /** + * @deprecated Create using static factory is deprecated, + * this method should not be called directly by library consumers + */ public static function create( string $body, ?TypeResolver $typeResolver = null, ?DescriptionFactory $descriptionFactory = null, ?TypeContext $context = null ): self { - trigger_error('Create using static factory is deprecated, this method should not be called directly by library consumers', E_USER_DEPRECATED); + trigger_error( + 'Create using static factory is deprecated, this method should not be called directly + by library consumers', + E_USER_DEPRECATED + ); Assert::notNull($typeResolver); Assert::notNull($descriptionFactory); diff --git a/src/DocBlock/Tags/Var_.php b/src/DocBlock/Tags/Var_.php index c523612..d8e75b9 100644 --- a/src/DocBlock/Tags/Var_.php +++ b/src/DocBlock/Tags/Var_.php @@ -49,13 +49,21 @@ final class Var_ extends TagWithType implements Factory\StaticMethod $this->description = $description; } + /** + * @deprecated Create using static factory is deprecated, + * this method should not be called directly by library consumers + */ public static function create( string $body, ?TypeResolver $typeResolver = null, ?DescriptionFactory $descriptionFactory = null, ?TypeContext $context = null ): self { - trigger_error('Create using static factory is deprecated, this method should not be called directly by library consumers', E_USER_DEPRECATED); + trigger_error( + 'Create using static factory is deprecated, this method should not be called directly + by library consumers', + E_USER_DEPRECATED + ); Assert::stringNotEmpty($body); Assert::notNull($typeResolver); Assert::notNull($descriptionFactory); diff --git a/src/DocBlockFactory.php b/src/DocBlockFactory.php index 102d220..46eb87f 100644 --- a/src/DocBlockFactory.php +++ b/src/DocBlockFactory.php @@ -26,7 +26,6 @@ use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyFactory; use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory; use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory; use phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory; -use phpDocumentor\Reflection\DocBlock\Tags\Factory\TypeFactory; use phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory; use Webmozart\Assert\Assert; @@ -64,7 +63,7 @@ final class DocBlockFactory implements DocBlockFactoryInterface * * @param array|Factory> $additionalTags */ - public static function createInstance(array $additionalTags = []): self + public static function createInstance(array $additionalTags = []): DocBlockFactoryInterface { $fqsenResolver = new FqsenResolver(); $tagFactory = new StandardTagFactory($fqsenResolver); diff --git a/tests/unit/DocBlock/Tags/Factory/MethodFactoryTest.php b/tests/unit/DocBlock/Tags/Factory/MethodFactoryTest.php index a8ab6ea..2daad8b 100644 --- a/tests/unit/DocBlock/Tags/Factory/MethodFactoryTest.php +++ b/tests/unit/DocBlock/Tags/Factory/MethodFactoryTest.php @@ -76,7 +76,7 @@ final class MethodFactoryTest extends TagFactoryTestCase new Method( 'myMethod', [], - new Mixed_(), + new Void_(), false, new Description(''), false, @@ -88,7 +88,7 @@ final class MethodFactoryTest extends TagFactoryTestCase new Method( 'myMethod', [], - new Mixed_(), + new Void_(), false, new Description(''), false, @@ -100,7 +100,7 @@ final class MethodFactoryTest extends TagFactoryTestCase new Method( 'myMethod', [], - new Mixed_(), + new Void_(), false, new Description(''), false, @@ -112,7 +112,7 @@ final class MethodFactoryTest extends TagFactoryTestCase new Method( 'myMethod', [], - new Mixed_(), + new Void_(), false, new Description(''), false, @@ -124,7 +124,7 @@ final class MethodFactoryTest extends TagFactoryTestCase new Method( 'myMethod', [], - new Mixed_(), + new Void_(), false, new Description(''), false, @@ -136,7 +136,7 @@ final class MethodFactoryTest extends TagFactoryTestCase new Method( 'myMethod', [], - new Mixed_(), + new Void_(), false, new Description(''), false,