-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DeprecationHelper DeprecatedScopeResolver (#714)
* Add DeprecationHelper DeprecatedScopeResolver * test with phpstan 1.10.x-dev * call stack released in 1.10.55 * fix linting and tests on d9 * Use getFunctionCallStackWithParameters * require phpstan ^1.10.56
- Loading branch information
Showing
5 changed files
with
161 additions
and
2 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
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\DeprecatedScope; | ||
|
||
use Drupal\Component\Utility\DeprecationHelper; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Rules\Deprecations\DeprecatedScopeResolver; | ||
|
||
final class DeprecationHelperScope implements DeprecatedScopeResolver | ||
{ | ||
public function isScopeDeprecated(Scope $scope): bool | ||
{ | ||
if (!class_exists(DeprecationHelper::class)) { | ||
return false; | ||
} | ||
$callStack = $scope->getFunctionCallStackWithParameters(); | ||
if (count($callStack) === 0) { | ||
return false; | ||
} | ||
[$function, $parameter] = $callStack[0]; | ||
if (!$function instanceof MethodReflection) { | ||
return false; | ||
} | ||
if ($function->getName() !== 'backwardsCompatibleCall' | ||
|| $function->getDeclaringClass()->getName() !== DeprecationHelper::class | ||
) { | ||
return false; | ||
} | ||
return $parameter !== null && $parameter->getName() === 'deprecatedCallable'; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Tests\DeprecatedScope; | ||
|
||
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase; | ||
use PHPStan\Rules\Deprecations\CallToDeprecatedFunctionRule; | ||
use PHPStan\Rules\Deprecations\DeprecatedScopeHelper; | ||
use PHPStan\Rules\Rule; | ||
|
||
final class DeprecationHelperScopeTest extends DrupalRuleTestCase { | ||
|
||
protected function getRule(): Rule | ||
{ | ||
// @phpstan-ignore-next-line | ||
return new CallToDeprecatedFunctionRule( | ||
self::createReflectionProvider(), | ||
self::getContainer()->getByType(DeprecatedScopeHelper::class) | ||
); | ||
} | ||
|
||
public function testCustomScope(): void | ||
{ | ||
[$version] = explode('.', \Drupal::VERSION, 2); | ||
if ($version < '10') { | ||
self::markTestSkipped('Not tested on < Drupal 10'); | ||
} | ||
require_once __DIR__ . '/data/deprecated-data-definition.php'; | ||
$this->analyse( | ||
[__DIR__ . '/data/deprecation-helper-test.php'], | ||
[ | ||
[ | ||
'Call to deprecated function Deprecated\deprecated_function().', | ||
26, | ||
], | ||
[ | ||
'Call to deprecated function Deprecated\deprecated_function().', | ||
42, | ||
], | ||
] | ||
); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
tests/src/DeprecatedScope/data/deprecation-helper-test.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,76 @@ | ||
<?php | ||
|
||
namespace GroupLegacy; | ||
|
||
use Drupal\Component\Utility\DeprecationHelper; | ||
use function Deprecated\deprecated_function; | ||
|
||
final class FooTest { | ||
|
||
public function methodCallingThings(): void { | ||
|
||
\Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall( | ||
\Drupal::VERSION, | ||
'10.1.0', | ||
fn() => count([]), | ||
fn() => deprecated_function() | ||
); | ||
|
||
DeprecationHelper::backwardsCompatibleCall( | ||
\Drupal::VERSION, | ||
'10.1.0', | ||
fn() => count([]), | ||
fn() => deprecated_function() | ||
); | ||
|
||
deprecated_function(); | ||
|
||
DeprecationHelper::backwardsCompatibleCall( | ||
\Drupal::VERSION, | ||
'10.1.0', | ||
function() { | ||
count([]); | ||
}, | ||
function() { | ||
deprecated_function(); | ||
} | ||
); | ||
|
||
DeprecationHelper::backwardsCompatibleCall( | ||
\Drupal::VERSION, | ||
'10.1.0', | ||
fn() => deprecated_function(), | ||
fn() => count([]) | ||
); | ||
|
||
DeprecationHelper::backwardsCompatibleCall( | ||
\Drupal::VERSION, | ||
'10.1.0', | ||
$this->currentCallable(...), | ||
$this->deprecatedCallable(...) | ||
); | ||
|
||
DeprecationHelper::backwardsCompatibleCall( | ||
\Drupal::VERSION, | ||
'10.1.0', | ||
// @todo somehow this should trigger an error as well. | ||
$this->deprecatedCallable(...), | ||
$this->currentCallable(...) | ||
); | ||
} | ||
|
||
/** | ||
* @deprecated | ||
* | ||
* @note if using reference callables they must be tagged as deprecated. | ||
*/ | ||
public function deprecatedCallable() | ||
{ | ||
deprecated_function(); | ||
} | ||
|
||
public function currentCallable() | ||
{ | ||
count([]); | ||
} | ||
} |