Skip to content

Commit

Permalink
Add a UsageOfDeprecatedCastRule
Browse files Browse the repository at this point in the history
  • Loading branch information
BackEndTea authored and ondrejmirtes committed May 30, 2020
1 parent 6f87f9c commit 9b4b885
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ rules:
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInClassMethodSignatureRule
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInClosureSignatureRule
- PHPStan\Rules\Deprecations\TypeHintDeprecatedInFunctionSignatureRule
- PHPStan\Rules\Deprecations\UsageOfDeprecatedCastRule
- PHPStan\Rules\Deprecations\UsageOfDeprecatedTraitRule
51 changes: 51 additions & 0 deletions src/Rules/Deprecations/UsageOfDeprecatedCastRule.php
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 tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php
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 tests/Rules/Deprecations/data/usage-of-deprecated-cast.php
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;
}

0 comments on commit 9b4b885

Please sign in to comment.