diff --git a/composer.json b/composer.json index e0dcdaa..6e378f4 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ ], "require": { "php": "^7.4 || ^8.0", - "phpstan/phpstan": "^2.1.13" + "phpstan/phpstan": "^2.1.15" }, "require-dev": { "php-parallel-lint/php-parallel-lint": "^1.2", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 12bd53e..c8a4d71 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -54,6 +54,12 @@ parameters: count: 1 path: tests/Rules/Deprecations/TypeHintDeprecatedInFunctionSignatureRuleTest.php + - + message: '#^Accessing PHPStan\\Rules\\RestrictedUsage\\RestrictedUsageOfDeprecatedStringCastRule\:\:class is not covered by backward compatibility promise\. The class might change in a minor PHPStan version\.$#' + identifier: phpstanApi.classConstant + count: 1 + path: tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php + - message: '#^Accessing PHPStan\\Rules\\Classes\\ExistingClassInTraitUseRule\:\:class is not covered by backward compatibility promise\. The class might change in a minor PHPStan version\.$#' identifier: phpstanApi.classConstant diff --git a/rules.neon b/rules.neon index 1a6874c..aa4a8a0 100644 --- a/rules.neon +++ b/rules.neon @@ -45,7 +45,6 @@ services: rules: - PHPStan\Rules\Deprecations\FetchingDeprecatedConstRule - - PHPStan\Rules\Deprecations\UsageOfDeprecatedCastRule conditionalTags: PHPStan\Rules\Deprecations\CallWithDeprecatedIniOptionRule: diff --git a/src/Rules/Deprecations/RestrictedDeprecatedMethodUsageExtension.php b/src/Rules/Deprecations/RestrictedDeprecatedMethodUsageExtension.php index fb2ee82..d8bb035 100644 --- a/src/Rules/Deprecations/RestrictedDeprecatedMethodUsageExtension.php +++ b/src/Rules/Deprecations/RestrictedDeprecatedMethodUsageExtension.php @@ -68,6 +68,27 @@ public function isRestrictedMethodUsage( } $description = $methodReflection->getDeprecatedDescription(); + if (strtolower($methodReflection->getName()) === '__tostring') { + if ($description === null) { + return RestrictedUsage::create( + sprintf( + 'Casting class %s to string is deprecated.', + $methodReflection->getDeclaringClass()->getName(), + ), + 'class.toStringDeprecated', + ); + } + + return RestrictedUsage::create( + sprintf( + "Casting class %s to string is deprecated.:\n%s", + $methodReflection->getDeclaringClass()->getName(), + $description, + ), + 'class.toStringDeprecated', + ); + } + if ($description === null) { return RestrictedUsage::create( sprintf( diff --git a/src/Rules/Deprecations/UsageOfDeprecatedCastRule.php b/src/Rules/Deprecations/UsageOfDeprecatedCastRule.php deleted file mode 100644 index 1dd34cc..0000000 --- a/src/Rules/Deprecations/UsageOfDeprecatedCastRule.php +++ /dev/null @@ -1,64 +0,0 @@ - - */ -class UsageOfDeprecatedCastRule implements Rule -{ - - private DeprecatedScopeHelper $deprecatedScopeHelper; - - public function __construct(DeprecatedScopeHelper $deprecatedScopeHelper) - { - $this->deprecatedScopeHelper = $deprecatedScopeHelper; - } - - public function getNodeType(): string - { - return Cast::class; - } - - public function processNode(Node $node, Scope $scope): array - { - if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { - return []; - } - - $castedType = $scope->getType($node->expr); - if (! $castedType->hasMethod('__toString')->yes()) { - return []; - } - $method = $castedType->getMethod('__toString', $scope); - - if (! $method->isDeprecated()->yes()) { - return []; - } - $description = $method->getDeprecatedDescription(); - if ($description === null) { - return [ - RuleErrorBuilder::message(sprintf( - 'Casting class %s to string is deprecated.', - $method->getDeclaringClass()->getName(), - ))->identifier('class.toStringDeprecated')->build(), - ]; - } - - return [ - RuleErrorBuilder::message(sprintf( - "Casting class %s to string is deprecated.:\n%s", - $method->getDeclaringClass()->getName(), - $description, - ))->identifier('class.toStringDeprecated')->build(), - ]; - } - -} diff --git a/tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php b/tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php index f2280a2..58149e7 100644 --- a/tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php +++ b/tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php @@ -2,23 +2,22 @@ namespace PHPStan\Rules\Deprecations; +use PHPStan\Rules\RestrictedUsage\RestrictedUsageOfDeprecatedStringCastRule; use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; /** - * @extends RuleTestCase + * @extends RuleTestCase */ class UsageOfDeprecatedCastRuleTest extends RuleTestCase { protected function getRule(): Rule { - return new UsageOfDeprecatedCastRule( - new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]), - ); + return self::getContainer()->getByType(RestrictedUsageOfDeprecatedStringCastRule::class); } - public function testUsageOfDeprecatedTrait(): void + public function testUsageOfDeprecatedCast(): void { require_once __DIR__ . '/data/usage-of-deprecated-cast.php'; $this->analyse( @@ -32,4 +31,12 @@ public function testUsageOfDeprecatedTrait(): void ); } + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/../../../rules.neon', + ...parent::getAdditionalConfigFiles(), + ]; + } + } diff --git a/tests/Rules/Deprecations/data/usage-of-deprecated-cast.php b/tests/Rules/Deprecations/data/usage-of-deprecated-cast.php index 1bb3444..0602624 100644 --- a/tests/Rules/Deprecations/data/usage-of-deprecated-cast.php +++ b/tests/Rules/Deprecations/data/usage-of-deprecated-cast.php @@ -24,3 +24,7 @@ function deprecatedScope(Foo $foo): string { return (string) $foo; } + +function foo2(Foo $foo): string { + return (int) $foo; +}