Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Rules\Deprecations;

use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Rules\RestrictedUsage\RestrictedMethodUsageExtension;
use PHPStan\Rules\RestrictedUsage\RestrictedUsage;
Expand Down Expand Up @@ -63,6 +64,34 @@ public function isRestrictedMethodUsage(
);
}

$deprecatedDeclaringTrait = $this->findDeprecatedDeclaringTrait(
$methodReflection->getDeclaringClass(),
$methodReflection->getName(),
);
if ($deprecatedDeclaringTrait !== null) {
$traitDescription = $deprecatedDeclaringTrait->getDeprecatedDescription();
if ($traitDescription === null) {
return RestrictedUsage::create(
sprintf(
'Call to method %s() of deprecated trait %s.',
$methodReflection->getName(),
$deprecatedDeclaringTrait->getName(),
),
sprintf('%s.deprecatedTrait', $methodReflection->isStatic() ? 'staticMethod' : 'method'),
);
}

return RestrictedUsage::create(
sprintf(
"Call to method %s() of deprecated trait %s:\n%s",
$methodReflection->getName(),
$deprecatedDeclaringTrait->getName(),
$traitDescription,
),
sprintf('%s.deprecatedTrait', $methodReflection->isStatic() ? 'staticMethod' : 'method'),
);
}

if (!$methodReflection->isDeprecated()->yes()) {
return null;
}
Expand Down Expand Up @@ -113,4 +142,17 @@ public function isRestrictedMethodUsage(
);
}

private function findDeprecatedDeclaringTrait(ClassReflection $declaringClass, string $methodName): ?ClassReflection
{
foreach ($declaringClass->getTraits() as $trait) {
if (!$trait->hasNativeMethod($methodName) || !$trait->isDeprecated()) {
continue;
}

return $trait;
}

return null;
}

}
8 changes: 8 additions & 0 deletions tests/Rules/Deprecations/CallToDeprecatedMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public function testDeprecatedMethodCall(): void
"Call to deprecated method prophesize() of class CheckDeprecatedMethodCall\\UsingDeprecatedMethodFromTrait:\nUse TraitReplacingDeprecatedMethod::prophesize()",
64,
],
[
'Call to method methodFromDeprecatedTrait() of deprecated trait CheckDeprecatedMethodCall\DeprecatedTrait.',
79,
],
[
"Call to method methodFromDeprecatedTraitWithDescription() of deprecated trait CheckDeprecatedMethodCall\\DeprecatedTraitWithDescription:\nUse something else.",
80,
],
],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public function testDeprecatedStaticMethodCall(): void
'Call to method doDeprecatedBar() of deprecated class CheckDeprecatedStaticMethodCall\DeprecatedBar.',
81,
],
[
'Call to method staticMethodFromDeprecatedTrait() of deprecated trait CheckDeprecatedStaticMethodCall\DeprecatedTrait.',
83,
],
[
"Call to method staticMethodFromDeprecatedTraitWithDescription() of deprecated trait CheckDeprecatedStaticMethodCall\\DeprecatedTraitWithDescription:\nUse something else.",
84,
],
],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,39 @@ protected function prophesize(): void
}
}

/**
* @deprecated
*/
trait DeprecatedTrait
{

public function methodFromDeprecatedTrait(): void
{
}

public static function staticMethodFromDeprecatedTrait(): void
{
}

}

/**
* @deprecated Use something else.
*/
trait DeprecatedTraitWithDescription
{

public function methodFromDeprecatedTraitWithDescription(): void
{
}

}

class ClassUsingDeprecatedTrait
{

use DeprecatedTrait;
use DeprecatedTraitWithDescription;

}

14 changes: 14 additions & 0 deletions tests/Rules/Deprecations/data/call-to-deprecated-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ public function callProphesize(): void
$this->prophesize();
}
}

$obj = new ClassUsingDeprecatedTrait();
$obj->methodFromDeprecatedTrait();
$obj->methodFromDeprecatedTraitWithDescription();

/**
* @deprecated
*/
function deprecated_scope_for_deprecated_trait(): void
{
$obj = new ClassUsingDeprecatedTrait();
$obj->methodFromDeprecatedTrait();
$obj->methodFromDeprecatedTraitWithDescription();
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,35 @@ class DeprecatedBaz extends Foo
{

}

/**
* @deprecated
*/
trait DeprecatedTrait
{

public static function staticMethodFromDeprecatedTrait(): void
{
}

}

/**
* @deprecated Use something else.
*/
trait DeprecatedTraitWithDescription
{

public static function staticMethodFromDeprecatedTraitWithDescription(): void
{
}

}

class ClassUsingDeprecatedTrait
{

use DeprecatedTrait;
use DeprecatedTraitWithDescription;

}
12 changes: 12 additions & 0 deletions tests/Rules/Deprecations/data/call-to-deprecated-static-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,15 @@ public static function foo()
}

DeprecatedBar::doDeprecatedBar();

ClassUsingDeprecatedTrait::staticMethodFromDeprecatedTrait();
ClassUsingDeprecatedTrait::staticMethodFromDeprecatedTraitWithDescription();

/**
* @deprecated
*/
function deprecated_scope_for_deprecated_trait(): void
{
ClassUsingDeprecatedTrait::staticMethodFromDeprecatedTrait();
ClassUsingDeprecatedTrait::staticMethodFromDeprecatedTraitWithDescription();
}
Loading