From 497505e532f9f1ba4f540931c8996e171f996058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Unger?= Date: Thu, 12 Dec 2019 14:23:58 +0100 Subject: [PATCH] Bump PHPStan dependency to 0.12 --- .travis.yml | 6 ---- composer.json | 8 ++--- phpstan.neon.dist | 2 +- src/DisallowFloatAssignedToVariableRule.php | 10 ++++-- src/DisallowFloatEverywhereRule.php | 13 +++---- src/DisallowFloatInFunctionSignatureRule.php | 34 ++++++++++--------- src/DisallowFloatInMethodSignatureRule.php | 24 +++++++------ src/DisallowFloatPropertyTypeRule.php | 15 ++++---- ...isallowFloatAssignedToVariableRuleTest.php | 3 ++ tests/src/DisallowFloatEverywhereRuleTest.php | 3 ++ ...allowFloatInFunctionSignatureRuleTest.php} | 5 ++- ...DisallowFloatInMethodSignatureRuleTest.php | 3 ++ .../src/DisallowFloatPropertyTypeRuleTest.php | 3 ++ 13 files changed, 73 insertions(+), 56 deletions(-) rename tests/src/{DisallowFloatinFunctionSignatureRuleTest.php => DisallowFloatInFunctionSignatureRuleTest.php} (94%) diff --git a/.travis.yml b/.travis.yml index 6969aae..ba557cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,9 +35,3 @@ jobs: - stage: Code Style script: vendor/bin/phpcs - - - stage: Dependencies Check - script: vendor/bin/composer-require-checker - - - stage: Backwards Compatibility Check - script: vendor/bin/roave-backward-compatibility-check diff --git a/composer.json b/composer.json index 9222624..ffac4c2 100644 --- a/composer.json +++ b/composer.json @@ -7,16 +7,14 @@ "require": { "php": "^7.2", "nikic/php-parser": "^4.0", - "phpstan/phpstan": "^0.11.7" + "phpstan/phpstan": "^0.12" }, "require-dev": { "doctrine/coding-standard": "^6.0", "infection/infection": "^0.11.4", - "maglnet/composer-require-checker": "^1.1", - "phpstan/phpstan-phpunit": "^0.11", - "phpstan/phpstan-strict-rules": "^0.11", + "phpstan/phpstan-phpunit": "^0.12", + "phpstan/phpstan-strict-rules": "^0.12", "phpunit/phpunit": "^7.4", - "roave/backward-compatibility-check": "^2.0", "squizlabs/php_codesniffer": "^3.4" }, "autoload": { diff --git a/phpstan.neon.dist b/phpstan.neon.dist index aea8cf4..ff2335d 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -8,7 +8,7 @@ parameters: paths: - %currentWorkingDirectory%/src - %currentWorkingDirectory%/tests/src - level: 7 + level: 8 services: scopeIsInClass: diff --git a/src/DisallowFloatAssignedToVariableRule.php b/src/DisallowFloatAssignedToVariableRule.php index ecb88f8..f706398 100644 --- a/src/DisallowFloatAssignedToVariableRule.php +++ b/src/DisallowFloatAssignedToVariableRule.php @@ -8,9 +8,13 @@ use PhpParser\PrettyPrinter\Standard; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Type\VerbosityLevel; use function sprintf; +/** + * @implements \PHPStan\Rules\Rule<\PhpParser\Node> + */ final class DisallowFloatAssignedToVariableRule implements Rule { /** @var Standard */ @@ -27,7 +31,7 @@ public function getNodeType() : string } /** - * @return string[] + * {@inheritDoc} */ public function processNode(Node $node, Scope $scope) : array { @@ -41,11 +45,11 @@ public function processNode(Node $node, Scope $scope) : array } return [ - sprintf( + RuleErrorBuilder::message(sprintf( 'Cannot assign %s to %s - floats are not allowed.', $resultType->describe(VerbosityLevel::typeOnly()), $this->printer->prettyPrintExpr($node->var) - ), + ))->build(), ]; } } diff --git a/src/DisallowFloatEverywhereRule.php b/src/DisallowFloatEverywhereRule.php index f79d0ad..37236e9 100644 --- a/src/DisallowFloatEverywhereRule.php +++ b/src/DisallowFloatEverywhereRule.php @@ -8,10 +8,13 @@ use PhpParser\Node\Expr; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Type\VerbosityLevel; -use function assert; use function sprintf; +/** + * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr> + */ class DisallowFloatEverywhereRule implements Rule { public function getNodeType() : string @@ -20,7 +23,7 @@ public function getNodeType() : string } /** - * @return string[] + * {@inheritDoc} */ public function processNode(Node $node, Scope $scope) : array { @@ -30,18 +33,16 @@ public function processNode(Node $node, Scope $scope) : array return []; } - assert($node instanceof Expr); - $nodeType = $scope->getType($node); if (! FloatTypeHelper::isFloat($nodeType)) { return []; } return [ - sprintf( + RuleErrorBuilder::message(sprintf( 'Cannot have %s as a result type of this expression - floats are not allowed.', $nodeType->describe(VerbosityLevel::typeOnly()) - ), + ))->build(), ]; } } diff --git a/src/DisallowFloatInFunctionSignatureRule.php b/src/DisallowFloatInFunctionSignatureRule.php index dfb0dcf..266407b 100644 --- a/src/DisallowFloatInFunctionSignatureRule.php +++ b/src/DisallowFloatInFunctionSignatureRule.php @@ -11,17 +11,21 @@ use PHPStan\Broker\Broker; use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\ParameterReflection; -use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; +use PHPStan\Reflection\ParametersAcceptor; use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleError; +use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Type\VerbosityLevel; use function array_filter; use function array_keys; use function array_map; use function array_merge; use function array_values; -use function assert; use function sprintf; +/** + * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\Function_> + */ final class DisallowFloatInFunctionSignatureRule implements Rule { /** @var Broker */ @@ -38,18 +42,16 @@ public function getNodeType() : string } /** - * @return string[] + * {@inheritDoc} */ public function processNode(Node $node, Scope $scope) : array { - assert($node instanceof Function_); - $functionName = new Name($node->name->toString()); - if (! $this->broker->hasCustomFunction($functionName, $scope)) { + if (! $this->broker->hasFunction($functionName, $scope)) { return []; } - $functionReflection = $this->broker->getCustomFunction($functionName, $scope); + $functionReflection = $this->broker->getFunction($functionName, $scope); $errors = []; @@ -61,43 +63,43 @@ public function processNode(Node $node, Scope $scope) : array return array_filter(array_merge([], ...$errors)); } - /** @return string[] */ + /** @return RuleError[] */ private function returnTypeViolations( - ParametersAcceptorWithPhpDocs $function, + ParametersAcceptor $function, FunctionReflection $functionReflection ) : array { if (! FloatTypeHelper::isFloat($function->getReturnType())) { return []; } - return [sprintf( + return [RuleErrorBuilder::message(sprintf( 'Function %s() cannot have %s as its return type - floats are not allowed.', $functionReflection->getName(), $function->getReturnType()->describe(VerbosityLevel::typeOnly()) - ), + ))->build(), ]; } - /** @return string[]|null[] */ + /** @return RuleError[]|null[] */ private function violationsForParameters( - ParametersAcceptorWithPhpDocs $function, + ParametersAcceptor $function, FunctionReflection $functionReflection ) : array { $parameters = $function->getParameters(); return array_map( - static function (ParameterReflection $parameter, int $index) use ($functionReflection) : ?string { + static function (ParameterReflection $parameter, int $index) use ($functionReflection) : ?RuleError { if (! FloatTypeHelper::isFloat($parameter->getType())) { return null; } - return sprintf( + return RuleErrorBuilder::message(sprintf( 'Parameter #%d $%s of function %s() cannot have %s as its type - floats are not allowed.', $index + 1, $parameter->getName(), $functionReflection->getName(), $parameter->getType()->describe(VerbosityLevel::typeOnly()) - ); + ))->build(); }, array_values($parameters), array_keys($parameters) diff --git a/src/DisallowFloatInMethodSignatureRule.php b/src/DisallowFloatInMethodSignatureRule.php index ea6b01a..905682f 100644 --- a/src/DisallowFloatInMethodSignatureRule.php +++ b/src/DisallowFloatInMethodSignatureRule.php @@ -11,6 +11,8 @@ use PHPStan\Reflection\ParameterReflection; use PHPStan\Reflection\ParametersAcceptor; use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleError; +use PHPStan\Rules\RuleErrorBuilder; use PHPStan\ShouldNotHappenException; use PHPStan\Type\VerbosityLevel; use function array_filter; @@ -18,9 +20,11 @@ use function array_map; use function array_merge; use function array_values; -use function assert; use function sprintf; +/** + * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\ClassMethod> + */ final class DisallowFloatInMethodSignatureRule implements Rule { public function getNodeType() : string @@ -29,12 +33,10 @@ public function getNodeType() : string } /** - * @return string[] + * {@inheritDoc} */ public function processNode(Node $node, Scope $scope) : array { - assert($node instanceof ClassMethod); - if (! $scope->isInClass()) { throw new ShouldNotHappenException(); } @@ -53,7 +55,7 @@ public function processNode(Node $node, Scope $scope) : array return array_filter(array_merge([], ...$errors)); } - /** @return string[] */ + /** @return RuleError[] */ private function returnTypeViolations( ParametersAcceptor $method, MethodReflection $methodReflection @@ -62,16 +64,16 @@ private function returnTypeViolations( return []; } - return [sprintf( + return [RuleErrorBuilder::message(sprintf( 'Method %s::%s() cannot have %s as its return type - floats are not allowed.', $methodReflection->getDeclaringClass()->getDisplayName(), $methodReflection->getName(), $method->getReturnType()->describe(VerbosityLevel::typeOnly()) - ), + ))->build(), ]; } - /** @return string[]|null[] */ + /** @return RuleError[]|null[] */ private function violationsForParameters( ParametersAcceptor $function, MethodReflection $methodReflection @@ -79,19 +81,19 @@ private function violationsForParameters( $parameters = $function->getParameters(); return array_map( - static function (ParameterReflection $parameter, int $index) use ($methodReflection) : ?string { + static function (ParameterReflection $parameter, int $index) use ($methodReflection) : ?RuleError { if (! FloatTypeHelper::isFloat($parameter->getType())) { return null; } - return sprintf( + return RuleErrorBuilder::message(sprintf( 'Parameter #%d $%s of method %s::%s() cannot have %s as its type - floats are not allowed.', $index + 1, $parameter->getName(), $methodReflection->getDeclaringClass()->getDisplayName(), $methodReflection->getName(), $parameter->getType()->describe(VerbosityLevel::typeOnly()) - ); + ))->build(); }, array_values($parameters), array_keys($parameters) diff --git a/src/DisallowFloatPropertyTypeRule.php b/src/DisallowFloatPropertyTypeRule.php index 9ef92dc..601c757 100644 --- a/src/DisallowFloatPropertyTypeRule.php +++ b/src/DisallowFloatPropertyTypeRule.php @@ -8,11 +8,14 @@ use PhpParser\Node\Stmt\PropertyProperty; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleErrorBuilder; use PHPStan\ShouldNotHappenException; use PHPStan\Type\VerbosityLevel; -use function assert; use function sprintf; +/** + * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\PropertyProperty> + */ final class DisallowFloatPropertyTypeRule implements Rule { public function getNodeType() : string @@ -21,12 +24,10 @@ public function getNodeType() : string } /** - * @return string[] + * {@inheritDoc} */ public function processNode(Node $node, Scope $scope) : array { - assert($node instanceof PropertyProperty); - if (! $scope->isInClass()) { throw new ShouldNotHappenException(); } @@ -34,18 +35,18 @@ public function processNode(Node $node, Scope $scope) : array $classReflection = $scope->getClassReflection(); $propertyName = $node->name->toString(); $property = $classReflection->getNativeProperty($node->name->toString()); - $propertyType = $property->getType(); + $propertyType = $property->getReadableType(); if (! FloatTypeHelper::isFloat($propertyType)) { return []; } return [ - sprintf( + RuleErrorBuilder::message(sprintf( 'Property %s::$%s cannot have %s as its type - floats are not allowed.', $property->getDeclaringClass()->getDisplayName(), $propertyName, $propertyType->describe(VerbosityLevel::typeOnly()) - ), + ))->build(), ]; } } diff --git a/tests/src/DisallowFloatAssignedToVariableRuleTest.php b/tests/src/DisallowFloatAssignedToVariableRuleTest.php index 174cc2b..6a37d65 100644 --- a/tests/src/DisallowFloatAssignedToVariableRuleTest.php +++ b/tests/src/DisallowFloatAssignedToVariableRuleTest.php @@ -9,6 +9,9 @@ use PHPStan\Testing\RuleTestCase; use Roave\PHPStan\Rules\Floats\DisallowFloatAssignedToVariableRule; +/** + * @extends \PHPStan\Testing\RuleTestCase<\Roave\PHPStan\Rules\Floats\DisallowFloatAssignedToVariableRule> + */ final class DisallowFloatAssignedToVariableRuleTest extends RuleTestCase { protected function getRule() : Rule diff --git a/tests/src/DisallowFloatEverywhereRuleTest.php b/tests/src/DisallowFloatEverywhereRuleTest.php index 9cd5c5f..fcc1f0c 100644 --- a/tests/src/DisallowFloatEverywhereRuleTest.php +++ b/tests/src/DisallowFloatEverywhereRuleTest.php @@ -8,6 +8,9 @@ use PHPStan\Testing\RuleTestCase; use Roave\PHPStan\Rules\Floats\DisallowFloatEverywhereRule; +/** + * @extends \PHPStan\Testing\RuleTestCase<\Roave\PHPStan\Rules\Floats\DisallowFloatEverywhereRule> + */ class DisallowFloatEverywhereRuleTest extends RuleTestCase { protected function getRule() : Rule diff --git a/tests/src/DisallowFloatinFunctionSignatureRuleTest.php b/tests/src/DisallowFloatInFunctionSignatureRuleTest.php similarity index 94% rename from tests/src/DisallowFloatinFunctionSignatureRuleTest.php rename to tests/src/DisallowFloatInFunctionSignatureRuleTest.php index 01e4fb1..232f4be 100644 --- a/tests/src/DisallowFloatinFunctionSignatureRuleTest.php +++ b/tests/src/DisallowFloatInFunctionSignatureRuleTest.php @@ -8,7 +8,10 @@ use PHPStan\Testing\RuleTestCase; use Roave\PHPStan\Rules\Floats\DisallowFloatInFunctionSignatureRule; -final class DisallowFloatinFunctionSignatureRuleTest extends RuleTestCase +/** + * @extends \PHPStan\Testing\RuleTestCase<\Roave\PHPStan\Rules\Floats\DisallowFloatInFunctionSignatureRule> + */ +final class DisallowFloatInFunctionSignatureRuleTest extends RuleTestCase { protected function getRule() : Rule { diff --git a/tests/src/DisallowFloatInMethodSignatureRuleTest.php b/tests/src/DisallowFloatInMethodSignatureRuleTest.php index b2045a1..60a3cf5 100644 --- a/tests/src/DisallowFloatInMethodSignatureRuleTest.php +++ b/tests/src/DisallowFloatInMethodSignatureRuleTest.php @@ -11,6 +11,9 @@ use PHPStan\Testing\RuleTestCase; use Roave\PHPStan\Rules\Floats\DisallowFloatInMethodSignatureRule; +/** + * @extends \PHPStan\Testing\RuleTestCase<\Roave\PHPStan\Rules\Floats\DisallowFloatInMethodSignatureRule> + */ final class DisallowFloatInMethodSignatureRuleTest extends RuleTestCase { protected function getRule() : Rule diff --git a/tests/src/DisallowFloatPropertyTypeRuleTest.php b/tests/src/DisallowFloatPropertyTypeRuleTest.php index b5c055a..367bb03 100644 --- a/tests/src/DisallowFloatPropertyTypeRuleTest.php +++ b/tests/src/DisallowFloatPropertyTypeRuleTest.php @@ -11,6 +11,9 @@ use PHPStan\Testing\RuleTestCase; use Roave\PHPStan\Rules\Floats\DisallowFloatPropertyTypeRule; +/** + * @extends \PHPStan\Testing\RuleTestCase<\Roave\PHPStan\Rules\Floats\DisallowFloatPropertyTypeRule> + */ final class DisallowFloatPropertyTypeRuleTest extends RuleTestCase { protected function getRule() : Rule