Skip to content

Commit 9b4b885

Browse files
BackEndTeaondrejmirtes
authored andcommitted
Add a UsageOfDeprecatedCastRule
1 parent 6f87f9c commit 9b4b885

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

rules.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ rules:
1717
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInClassMethodSignatureRule
1818
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInClosureSignatureRule
1919
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInFunctionSignatureRule
20+
- PHPStan\Rules\Deprecations\UsageOfDeprecatedCastRule
2021
- PHPStan\Rules\Deprecations\UsageOfDeprecatedTraitRule
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\Cast;
7+
use PHPStan\Analyser\Scope;
8+
use function sprintf;
9+
10+
/**
11+
* @implements \PHPStan\Rules\Rule<Cast>
12+
*/
13+
class UsageOfDeprecatedCastRule implements \PHPStan\Rules\Rule
14+
{
15+
16+
public function getNodeType(): string
17+
{
18+
return Cast::class;
19+
}
20+
21+
public function processNode(Node $node, Scope $scope): array
22+
{
23+
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
24+
return [];
25+
}
26+
27+
$castedType = $scope->getType($node->expr);
28+
if (! $castedType->hasMethod('__toString')->yes()) {
29+
return [];
30+
}
31+
$method = $castedType->getMethod('__toString', $scope);
32+
33+
if (! $method->isDeprecated()->yes()) {
34+
return [];
35+
}
36+
$description = $method->getDeprecatedDescription();
37+
if ($description === null) {
38+
return [sprintf(
39+
'Casting class %s to string is deprecated.',
40+
$method->getDeclaringClass()->getName()
41+
)];
42+
}
43+
44+
return [sprintf(
45+
"Casting class %s to string is deprecated.:\n%s",
46+
$method->getDeclaringClass()->getName(),
47+
$description
48+
)];
49+
}
50+
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
/**
6+
* @extends \PHPStan\Testing\RuleTestCase<UsageOfDeprecatedCastRule>
7+
*/
8+
class UsageOfDeprecatedCastRuleTest extends \PHPStan\Testing\RuleTestCase
9+
{
10+
11+
protected function getRule(): \PHPStan\Rules\Rule
12+
{
13+
return new UsageOfDeprecatedCastRule();
14+
}
15+
16+
public function testUsageOfDeprecatedTrait(): void
17+
{
18+
require_once __DIR__ . '/data/usage-of-deprecated-cast.php';
19+
$this->analyse(
20+
[__DIR__ . '/data/usage-of-deprecated-cast.php'],
21+
[
22+
[
23+
'Casting class UsageOfDeprecatedCast\Foo to string is deprecated.',
24+
17,
25+
],
26+
]
27+
);
28+
}
29+
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace UsageOfDeprecatedCast;
4+
5+
class Foo
6+
{
7+
/**
8+
* @deprecated
9+
*/
10+
public function __toString()
11+
{
12+
return 'foo';
13+
}
14+
}
15+
16+
function foo(Foo $foo): string {
17+
return (string) $foo;
18+
}
19+
20+
/**
21+
* @deprecated
22+
*/
23+
function deprecatedScope(Foo $foo): string
24+
{
25+
return (string) $foo;
26+
}

0 commit comments

Comments
 (0)