Skip to content

Commit 6fe5889

Browse files
committed
Initial commit
1 parent 55ffe9c commit 6fe5889

File tree

50 files changed

+2334
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2334
-1
lines changed

Diff for: composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@
2727
"psr-4": {
2828
"PHPStan\\": "src/"
2929
}
30+
},
31+
"autoload-dev": {
32+
"classmap": ["tests/"]
3033
}
3134
}

Diff for: phpstan.neon

+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
includes:
22
- rules.neon
3+
4+
parameters:
5+
excludes_analyse:
6+
- */tests/*/data/*

Diff for: rules.neon

+12-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
rules: []
1+
rules:
2+
- PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule
3+
- PHPStan\Rules\Deprecations\AccessDeprecatedStaticPropertyRule
4+
- PHPStan\Rules\Deprecations\CallToDeprecatedFunctionRule
5+
- PHPStan\Rules\Deprecations\CallToDeprecatedMethodRule
6+
- PHPStan\Rules\Deprecations\CallToDeprecatedStaticMethodRule
7+
- PHPStan\Rules\Deprecations\FetchingClassConstOfDeprecatedClassRule
8+
- PHPStan\Rules\Deprecations\ImplementationOfDeprecatedInterfaceRule
9+
- PHPStan\Rules\Deprecations\InheritanceOfDeprecatedClassRule
10+
- PHPStan\Rules\Deprecations\InheritanceOfDeprecatedInterfaceRule
11+
- PHPStan\Rules\Deprecations\InstantiationOfDeprecatedClassRule
12+
- PHPStan\Rules\Deprecations\UsageOfDeprecatedTraitRule
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\PropertyFetch;
7+
use PhpParser\Node\Identifier;
8+
use PHPStan\Analyser\Scope;
9+
use PHPStan\Broker\Broker;
10+
use PHPStan\Reflection\DeprecatableReflection;
11+
use PHPStan\Type\TypeUtils;
12+
13+
class AccessDeprecatedPropertyRule implements \PHPStan\Rules\Rule
14+
{
15+
16+
/** @var Broker */
17+
private $broker;
18+
19+
public function __construct(Broker $broker)
20+
{
21+
$this->broker = $broker;
22+
}
23+
24+
public function getNodeType(): string
25+
{
26+
return PropertyFetch::class;
27+
}
28+
29+
/**
30+
* @param PropertyFetch $node
31+
* @param \PHPStan\Analyser\Scope $scope
32+
* @return string[] errors
33+
*/
34+
public function processNode(Node $node, Scope $scope): array
35+
{
36+
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
37+
return [];
38+
}
39+
40+
if (!$node->name instanceof Identifier) {
41+
return [];
42+
}
43+
44+
$propertyName = $node->name->name;
45+
$propertyAccessedOnType = $scope->getType($node->var);
46+
$referencedClasses = TypeUtils::getDirectClassNames($propertyAccessedOnType);
47+
48+
foreach ($referencedClasses as $referencedClass) {
49+
try {
50+
$classReflection = $this->broker->getClass($referencedClass);
51+
$propertyReflection = $classReflection->getProperty($propertyName, $scope);
52+
53+
if (!$propertyReflection instanceof DeprecatableReflection) {
54+
continue;
55+
}
56+
57+
if ($propertyReflection->isDeprecated()) {
58+
return [sprintf(
59+
'Access to deprecated property $%s of class %s.',
60+
$propertyName,
61+
$propertyReflection->getDeclaringClass()->getName()
62+
)];
63+
}
64+
} catch (\PHPStan\Broker\ClassNotFoundException $e) {
65+
// Other rules will notify if the class is not found
66+
} catch (\PHPStan\Reflection\MissingPropertyFromReflectionException $e) {
67+
// Other rules will notify if the property is not found
68+
}
69+
}
70+
71+
return [];
72+
}
73+
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\StaticPropertyFetch;
7+
use PhpParser\Node\Identifier;
8+
use PhpParser\Node\Name;
9+
use PHPStan\Analyser\Scope;
10+
use PHPStan\Broker\Broker;
11+
use PHPStan\Reflection\DeprecatableReflection;
12+
use PHPStan\Rules\RuleLevelHelper;
13+
use PHPStan\Type\ErrorType;
14+
use PHPStan\Type\Type;
15+
16+
class AccessDeprecatedStaticPropertyRule implements \PHPStan\Rules\Rule
17+
{
18+
19+
/** @var Broker */
20+
private $broker;
21+
22+
/** @var RuleLevelHelper */
23+
private $ruleLevelHelper;
24+
25+
public function __construct(Broker $broker, RuleLevelHelper $ruleLevelHelper)
26+
{
27+
$this->broker = $broker;
28+
$this->ruleLevelHelper = $ruleLevelHelper;
29+
}
30+
31+
public function getNodeType(): string
32+
{
33+
return StaticPropertyFetch::class;
34+
}
35+
36+
/**
37+
* @param StaticPropertyFetch $node
38+
* @param \PHPStan\Analyser\Scope $scope
39+
* @return string[] errors
40+
*/
41+
public function processNode(Node $node, Scope $scope): array
42+
{
43+
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
44+
return [];
45+
}
46+
47+
if (!$node->name instanceof Identifier) {
48+
return [];
49+
}
50+
51+
$propertyName = $node->name->name;
52+
$referencedClasses = [];
53+
54+
if ($node->class instanceof Name) {
55+
$referencedClasses[] = (string) $node->class;
56+
} else {
57+
$classTypeResult = $this->ruleLevelHelper->findTypeToCheck(
58+
$scope,
59+
$node->class,
60+
'', // We don't care about the error message
61+
function (Type $type) use ($propertyName) {
62+
return $type->canAccessProperties()->yes() && $type->hasProperty($propertyName);
63+
}
64+
);
65+
66+
if ($classTypeResult->getType() instanceof ErrorType) {
67+
return [];
68+
}
69+
70+
$referencedClasses = $classTypeResult->getReferencedClasses();
71+
}
72+
73+
foreach ($referencedClasses as $referencedClass) {
74+
try {
75+
$class = $this->broker->getClass($referencedClass);
76+
$property = $class->getProperty($propertyName, $scope);
77+
} catch (\PHPStan\Broker\ClassNotFoundException $e) {
78+
continue;
79+
} catch (\PHPStan\Reflection\MissingPropertyFromReflectionException $e) {
80+
continue;
81+
}
82+
83+
if ($property instanceof DeprecatableReflection && $property->isDeprecated()) {
84+
return [sprintf(
85+
'Access to deprecated static property $%s of class %s.',
86+
$propertyName,
87+
$referencedClass
88+
)];
89+
}
90+
}
91+
92+
return [];
93+
}
94+
95+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\FuncCall;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Broker\Broker;
9+
10+
class CallToDeprecatedFunctionRule implements \PHPStan\Rules\Rule
11+
{
12+
13+
/** @var Broker */
14+
private $broker;
15+
16+
public function __construct(Broker $broker)
17+
{
18+
$this->broker = $broker;
19+
}
20+
21+
public function getNodeType(): string
22+
{
23+
return FuncCall::class;
24+
}
25+
26+
/**
27+
* @param FuncCall $node
28+
* @param \PHPStan\Analyser\Scope $scope
29+
* @return string[] errors
30+
*/
31+
public function processNode(Node $node, Scope $scope): array
32+
{
33+
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
34+
return [];
35+
}
36+
37+
if (!($node->name instanceof \PhpParser\Node\Name)) {
38+
return [];
39+
}
40+
41+
try {
42+
$function = $this->broker->getFunction($node->name, $scope);
43+
} catch (\PHPStan\Broker\FunctionNotFoundException $e) {
44+
// Other rules will notify if the function is not found
45+
return [];
46+
}
47+
48+
if ($function->isDeprecated()) {
49+
return [sprintf(
50+
'Call to deprecated function %s().',
51+
$function->getName()
52+
)];
53+
}
54+
55+
return [];
56+
}
57+
58+
}
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\MethodCall;
7+
use PhpParser\Node\Identifier;
8+
use PHPStan\Analyser\Scope;
9+
use PHPStan\Broker\Broker;
10+
use PHPStan\Reflection\DeprecatableReflection;
11+
use PHPStan\Type\TypeUtils;
12+
13+
class CallToDeprecatedMethodRule implements \PHPStan\Rules\Rule
14+
{
15+
16+
/** @var Broker */
17+
private $broker;
18+
19+
public function __construct(Broker $broker)
20+
{
21+
$this->broker = $broker;
22+
}
23+
24+
public function getNodeType(): string
25+
{
26+
return MethodCall::class;
27+
}
28+
29+
/**
30+
* @param MethodCall $node
31+
* @param \PHPStan\Analyser\Scope $scope
32+
* @return string[] errors
33+
*/
34+
public function processNode(Node $node, Scope $scope): array
35+
{
36+
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
37+
return [];
38+
}
39+
40+
if (!$node->name instanceof Identifier) {
41+
return [];
42+
}
43+
44+
$methodName = $node->name->name;
45+
$methodCalledOnType = $scope->getType($node->var);
46+
$referencedClasses = TypeUtils::getDirectClassNames($methodCalledOnType);
47+
48+
foreach ($referencedClasses as $referencedClass) {
49+
try {
50+
$classReflection = $this->broker->getClass($referencedClass);
51+
$methodReflection = $classReflection->getMethod($methodName, $scope);
52+
53+
if (!$methodReflection instanceof DeprecatableReflection) {
54+
continue;
55+
}
56+
57+
if ($methodReflection->isDeprecated()) {
58+
return [sprintf(
59+
'Call to deprecated method %s() of class %s.',
60+
$methodReflection->getName(),
61+
$methodReflection->getDeclaringClass()->getName()
62+
)];
63+
}
64+
} catch (\PHPStan\Broker\ClassNotFoundException $e) {
65+
// Other rules will notify if the class is not found
66+
} catch (\PHPStan\Reflection\MissingMethodFromReflectionException $e) {
67+
// Other rules will notify if the the method is not found
68+
}
69+
}
70+
71+
return [];
72+
}
73+
74+
}

0 commit comments

Comments
 (0)