Skip to content

Commit 3562b56

Browse files
committed
Implement RestrictedFunctionUsageExtension
1 parent ce7a039 commit 3562b56

File tree

4 files changed

+71
-82
lines changed

4 files changed

+71
-82
lines changed

rules.neon

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ services:
1616
-
1717
class: PHPStan\Rules\Deprecations\CallWithDeprecatedIniOptionRule
1818

19+
-
20+
class: PHPStan\Rules\Deprecations\RestrictedDeprecatedFunctionUsageExtension
21+
tags:
22+
- phpstan.restrictedFunctionUsageExtension
23+
1924
-
2025
class: PHPStan\Rules\Deprecations\RestrictedDeprecatedMethodUsageExtension
2126
tags:
@@ -31,7 +36,6 @@ services:
3136
rules:
3237
- PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule
3338
- PHPStan\Rules\Deprecations\AccessDeprecatedStaticPropertyRule
34-
- PHPStan\Rules\Deprecations\CallToDeprecatedFunctionRule
3539
- PHPStan\Rules\Deprecations\FetchingClassConstOfDeprecatedClassRule
3640
- PHPStan\Rules\Deprecations\FetchingDeprecatedConstRule
3741
- PHPStan\Rules\Deprecations\UsageOfDeprecatedCastRule

src/Rules/Deprecations/CallToDeprecatedFunctionRule.php

-76
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PHPStan\Analyser\Scope;
6+
use PHPStan\Reflection\FunctionReflection;
7+
use PHPStan\Rules\RestrictedUsage\RestrictedFunctionUsageExtension;
8+
use PHPStan\Rules\RestrictedUsage\RestrictedUsage;
9+
use function sprintf;
10+
11+
class RestrictedDeprecatedFunctionUsageExtension implements RestrictedFunctionUsageExtension
12+
{
13+
14+
private DeprecatedScopeHelper $deprecatedScopeHelper;
15+
16+
public function __construct(DeprecatedScopeHelper $deprecatedScopeHelper)
17+
{
18+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
19+
}
20+
21+
public function isRestrictedFunctionUsage(
22+
FunctionReflection $functionReflection,
23+
Scope $scope
24+
): ?RestrictedUsage
25+
{
26+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
27+
return null;
28+
}
29+
30+
if (!$functionReflection->isDeprecated()->yes()) {
31+
return null;
32+
}
33+
34+
$description = $functionReflection->getDeprecatedDescription();
35+
if ($description === null) {
36+
return RestrictedUsage::create(
37+
sprintf(
38+
'Call to deprecated function %s().',
39+
$functionReflection->getName(),
40+
),
41+
'function.deprecated',
42+
);
43+
}
44+
45+
return RestrictedUsage::create(
46+
sprintf(
47+
"Call to deprecated function %s():\n%s",
48+
$functionReflection->getName(),
49+
$description,
50+
),
51+
'function.deprecated',
52+
);
53+
}
54+
55+
}

tests/Rules/Deprecations/CallToDeprecatedFunctionRuleTest.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
22

33
namespace PHPStan\Rules\Deprecations;
44

5+
use PHPStan\Rules\RestrictedUsage\RestrictedFunctionUsageRule;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Testing\RuleTestCase;
78

89
/**
9-
* @extends RuleTestCase<CallToDeprecatedFunctionRule>
10+
* @extends RuleTestCase<RestrictedFunctionUsageRule>
1011
*/
1112
class CallToDeprecatedFunctionRuleTest extends RuleTestCase
1213
{
1314

1415
protected function getRule(): Rule
1516
{
16-
return new CallToDeprecatedFunctionRule(
17-
$this->createReflectionProvider(),
18-
new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]),
19-
);
17+
return self::getContainer()->getByType(RestrictedFunctionUsageRule::class);
2018
}
2119

2220
public function testDeprecatedFunctionCall(): void
@@ -41,4 +39,12 @@ public function testDeprecatedFunctionCall(): void
4139
);
4240
}
4341

42+
public static function getAdditionalConfigFiles(): array
43+
{
44+
return [
45+
__DIR__ . '/../../../rules.neon',
46+
...parent::getAdditionalConfigFiles(),
47+
];
48+
}
49+
4450
}

0 commit comments

Comments
 (0)