Skip to content

Commit a5f7b4d

Browse files
paxalondrejmirtes
authored andcommitted
Rename tests for typehint deprecation check in class method
1 parent b341c77 commit a5f7b4d

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

src/Rules/Deprecations/TypeHintDeprecatedInClassMethodSignatureRule.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,42 @@ public function processNode(Node $node, Scope $scope): array
5050
/** @var ParameterReflection $parameter */
5151
$deprecatedClasses = $this->filterDeprecatedClasses($parameter->getType()->getReferencedClasses());
5252
foreach ($deprecatedClasses as $deprecatedClass) {
53+
if ($method->getDeclaringClass()->isAnonymous()) {
54+
$errors[] = sprintf(
55+
'Parameter $%s of method %s() in anonymous class has typehint with deprecated %s %s%s',
56+
$parameter->getName(),
57+
$method->getName(),
58+
$this->getClassType($deprecatedClass),
59+
$deprecatedClass->getName(),
60+
$this->getClassDeprecationDescription($deprecatedClass)
61+
);
62+
} else {
63+
$errors[] = sprintf(
64+
'Parameter $%s of method %s::%s() has typehint with deprecated %s %s%s',
65+
$parameter->getName(),
66+
$method->getDeclaringClass()->getName(),
67+
$method->getName(),
68+
$this->getClassType($deprecatedClass),
69+
$deprecatedClass->getName(),
70+
$this->getClassDeprecationDescription($deprecatedClass)
71+
);
72+
}
73+
}
74+
}
75+
76+
$deprecatedClasses = $this->filterDeprecatedClasses($methodSignature->getReturnType()->getReferencedClasses());
77+
foreach ($deprecatedClasses as $deprecatedClass) {
78+
if ($method->getDeclaringClass()->isAnonymous()) {
5379
$errors[] = sprintf(
54-
'Parameter $%s of method %s::%s() has typehint with deprecated %s %s%s',
55-
$parameter->getName(),
80+
'Return type of method %s() in anonymous class has typehint with deprecated %s %s%s',
81+
$method->getName(),
82+
$this->getClassType($deprecatedClass),
83+
$deprecatedClass->getName(),
84+
$this->getClassDeprecationDescription($deprecatedClass)
85+
);
86+
} else {
87+
$errors[] = sprintf(
88+
'Return type of method %s::%s() has typehint with deprecated %s %s%s',
5689
$method->getDeclaringClass()->getName(),
5790
$method->getName(),
5891
$this->getClassType($deprecatedClass),
@@ -62,18 +95,6 @@ public function processNode(Node $node, Scope $scope): array
6295
}
6396
}
6497

65-
$deprecatedClasses = $this->filterDeprecatedClasses($methodSignature->getReturnType()->getReferencedClasses());
66-
foreach ($deprecatedClasses as $deprecatedClass) {
67-
$errors[] = sprintf(
68-
'Return type of method %s::%s() has typehint with deprecated %s %s%s',
69-
$method->getDeclaringClass()->getName(),
70-
$method->getName(),
71-
$this->getClassType($deprecatedClass),
72-
$deprecatedClass->getName(),
73-
$this->getClassDeprecationDescription($deprecatedClass)
74-
);
75-
}
76-
7798
return $errors;
7899
}
79100

tests/Rules/Deprecations/TypeHintDeprecatedInClassMethodSignatureRuleTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ protected function getRule(): \PHPStan\Rules\Rule
1414

1515
public function test(): void
1616
{
17-
require_once __DIR__ . '/data/typehint-deprecated-class-definition.php';
17+
require_once __DIR__ . '/data/typehint-class-method-deprecated-class-definition.php';
1818
$this->analyse(
19-
[__DIR__ . '/data/typehint-deprecated-class.php'],
19+
[__DIR__ . '/data/typehint-class-method-deprecated-class.php'],
2020
[
21-
['Parameter $property of method TypeHintDeprecatedInFunctionSignature\Foo::setProperties() has typehint with deprecated class TypeHintDeprecatedInFunctionSignature\DeprecatedProperty.', 13],
22-
['Parameter $property2 of method TypeHintDeprecatedInFunctionSignature\Foo::setProperties() has typehint with deprecated interface TypeHintDeprecatedInFunctionSignature\DeprecatedInterface.', 13],
23-
["Parameter \$property4 of method TypeHintDeprecatedInFunctionSignature\Foo::setProperties() has typehint with deprecated class TypeHintDeprecatedInFunctionSignature\VerboseDeprecatedProperty:\nI'll be back", 13],
24-
['Parameter $property6 of method TypeHintDeprecatedInFunctionSignature\Foo::setProperties() has typehint with deprecated class TypeHintDeprecatedInFunctionSignature\DeprecatedProperty.', 13],
25-
['Return type of method TypeHintDeprecatedInFunctionSignature\Foo::setProperties() has typehint with deprecated class TypeHintDeprecatedInFunctionSignature\DeprecatedProperty.', 13],
26-
['Parameter $property of method TypeHintDeprecatedInFunctionSignature\FooImplNoOverride::oops() has typehint with deprecated class TypeHintDeprecatedInFunctionSignature\DeprecatedProperty.', 50],
21+
['Parameter $property of method TypeHintDeprecatedInClassMethodSignature\Foo::setProperties() has typehint with deprecated class TypeHintDeprecatedInClassMethodSignature\DeprecatedProperty.', 13],
22+
['Parameter $property2 of method TypeHintDeprecatedInClassMethodSignature\Foo::setProperties() has typehint with deprecated interface TypeHintDeprecatedInClassMethodSignature\DeprecatedInterface.', 13],
23+
["Parameter \$property4 of method TypeHintDeprecatedInClassMethodSignature\Foo::setProperties() has typehint with deprecated class TypeHintDeprecatedInClassMethodSignature\VerboseDeprecatedProperty:\nI'll be back", 13],
24+
['Parameter $property6 of method TypeHintDeprecatedInClassMethodSignature\Foo::setProperties() has typehint with deprecated class TypeHintDeprecatedInClassMethodSignature\DeprecatedProperty.', 13],
25+
['Return type of method TypeHintDeprecatedInClassMethodSignature\Foo::setProperties() has typehint with deprecated class TypeHintDeprecatedInClassMethodSignature\DeprecatedProperty.', 13],
26+
['Parameter $property of method TypeHintDeprecatedInClassMethodSignature\FooImplNoOverride::oops() has typehint with deprecated class TypeHintDeprecatedInClassMethodSignature\DeprecatedProperty.', 50],
27+
['Parameter $property of method __construct() in anonymous class has typehint with deprecated class TypeHintDeprecatedInClassMethodSignature\DeprecatedProperty.', 71],
28+
['Return type of method getProperty() in anonymous class has typehint with deprecated class TypeHintDeprecatedInClassMethodSignature\DeprecatedProperty.', 76],
2729
]
2830
);
2931
}

tests/Rules/Deprecations/data/typehint-deprecated-class-definition.php renamed to tests/Rules/Deprecations/data/typehint-class-method-deprecated-class-definition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace TypeHintDeprecatedInFunctionSignature;
3+
namespace TypeHintDeprecatedInClassMethodSignature;
44

55
/**
66
* @deprecated

tests/Rules/Deprecations/data/typehint-deprecated-class.php renamed to tests/Rules/Deprecations/data/typehint-class-method-deprecated-class.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace TypeHintDeprecatedInFunctionSignature;
3+
namespace TypeHintDeprecatedInClassMethodSignature;
44

55
class Foo
66
{
@@ -64,3 +64,17 @@ public function bar(DeprecatedProperty $property)
6464
}
6565

6666
}
67+
68+
new class {
69+
private $property;
70+
71+
public function __construct(DeprecatedProperty $property = null)
72+
{
73+
$this->property = $property;
74+
}
75+
76+
public function getProperty(): ?DeprecatedProperty
77+
{
78+
return $this->property;
79+
}
80+
};

0 commit comments

Comments
 (0)