-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f87f9c
commit 9b4b885
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Deprecations; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Cast; | ||
use PHPStan\Analyser\Scope; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements \PHPStan\Rules\Rule<Cast> | ||
*/ | ||
class UsageOfDeprecatedCastRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Cast::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (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 [sprintf( | ||
'Casting class %s to string is deprecated.', | ||
$method->getDeclaringClass()->getName() | ||
)]; | ||
} | ||
|
||
return [sprintf( | ||
"Casting class %s to string is deprecated.:\n%s", | ||
$method->getDeclaringClass()->getName(), | ||
$description | ||
)]; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Deprecations; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<UsageOfDeprecatedCastRule> | ||
*/ | ||
class UsageOfDeprecatedCastRuleTest extends \PHPStan\Testing\RuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new UsageOfDeprecatedCastRule(); | ||
} | ||
|
||
public function testUsageOfDeprecatedTrait(): void | ||
{ | ||
require_once __DIR__ . '/data/usage-of-deprecated-cast.php'; | ||
$this->analyse( | ||
[__DIR__ . '/data/usage-of-deprecated-cast.php'], | ||
[ | ||
[ | ||
'Casting class UsageOfDeprecatedCast\Foo to string is deprecated.', | ||
17, | ||
], | ||
] | ||
); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
tests/Rules/Deprecations/data/usage-of-deprecated-cast.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace UsageOfDeprecatedCast; | ||
|
||
class Foo | ||
{ | ||
/** | ||
* @deprecated | ||
*/ | ||
public function __toString() | ||
{ | ||
return 'foo'; | ||
} | ||
} | ||
|
||
function foo(Foo $foo): string { | ||
return (string) $foo; | ||
} | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
function deprecatedScope(Foo $foo): string | ||
{ | ||
return (string) $foo; | ||
} |