File tree Expand file tree Collapse file tree 4 files changed +108
-0
lines changed Expand file tree Collapse file tree 4 files changed +108
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments