Skip to content

Commit 518faec

Browse files
authored
add generic template to node-returning methods with same param and return union (#799)
Narrows the return type to the passed node type via @template, or an explicit conditional return type for factory methods that build a new node. Satisfies the new PreferGenericTemplateOnSameUnionTypeRule from symplify/phpstan-rules.
1 parent 29c5512 commit 518faec

6 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎rules/CodeQuality/Rector/MethodCall/AssertFalseStrposToContainsRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public function refactor(Node $node): ?Node
8686
return $this->changeArgumentsOrder($node);
8787
}
8888

89+
/**
90+
* @template TCall of MethodCall|StaticCall
91+
* @param TCall $node
92+
* @return TCall|null
93+
*/
8994
private function changeArgumentsOrder(MethodCall|StaticCall $node): MethodCall|StaticCall|null
9095
{
9196
$oldArguments = $node->getArgs();

‎rules/CodeQuality/Rector/MethodCall/UseSpecificWillMethodRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public function refactor(Node $node): ?Node
127127
return null;
128128
}
129129

130+
/**
131+
* @template TCall of StaticCall|MethodCall
132+
* @param TCall $call
133+
* @return TCall|null
134+
*/
130135
private function refactorNew(New_ $new, StaticCall|MethodCall $call): null|MethodCall|StaticCall
131136
{
132137
if (! $this->isName($new->class, 'PHPUnit\Framework\MockObject\Stub\ReturnValueMap')) {

‎rules/PHPUnit60/Rector/MethodCall/DelegateExceptionArgumentsRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ public function refactor(Node $node): ?Node
146146
return null;
147147
}
148148

149+
/**
150+
* @template TCall of StaticCall|MethodCall
151+
* @param TCall $call
152+
* @return ($call is MethodCall ? MethodCall : StaticCall)
153+
*/
149154
private function createFirstArgExtraMethodCall(StaticCall|MethodCall $call): MethodCall|StaticCall
150155
{
151156
/** @var Identifier $identifierNode */

‎rules/PHPUnit80/Rector/MethodCall/AssertEqualsParameterToSpecificMethodsTypeRector.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public function refactor(Node $node): ?Node
125125
return $this->processAssertEqualsWithDelta($node);
126126
}
127127

128+
/**
129+
* @template TCall of MethodCall|StaticCall
130+
* @param TCall $call
131+
* @return ($call is MethodCall ? MethodCall : StaticCall)|null
132+
*/
128133
private function processAssertEqualsIgnoringCase(MethodCall|StaticCall $call): StaticCall|MethodCall|null
129134
{
130135
$args = $call->getArgs();
@@ -149,6 +154,11 @@ private function processAssertEqualsIgnoringCase(MethodCall|StaticCall $call): S
149154
return $newMethodCall;
150155
}
151156

157+
/**
158+
* @template TCall of MethodCall|StaticCall
159+
* @param TCall $call
160+
* @return ($call is MethodCall ? MethodCall : StaticCall)|null
161+
*/
152162
private function processAssertEqualsCanonicalizing(MethodCall|StaticCall $call): MethodCall|StaticCall|null
153163
{
154164
$args = $call->getArgs();
@@ -176,6 +186,11 @@ private function processAssertEqualsCanonicalizing(MethodCall|StaticCall $call):
176186
return $newMethodCall;
177187
}
178188

189+
/**
190+
* @template TCall of MethodCall|StaticCall
191+
* @param TCall $call
192+
* @return ($call is MethodCall ? MethodCall : StaticCall)|null
193+
*/
179194
private function processAssertEqualsWithDelta(MethodCall|StaticCall $call): MethodCall|StaticCall|null
180195
{
181196
$args = $call->getArgs();

‎rules/PHPUnit90/Rector/MethodCall/ExplicitPhpErrorApiRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ public function refactor(Node $node): null|MethodCall|StaticCall
112112
return null;
113113
}
114114

115+
/**
116+
* @template TCall of MethodCall|StaticCall
117+
* @param TCall $node
118+
* @return ($node is MethodCall ? MethodCall : StaticCall)|null
119+
*/
115120
private function replaceExceptionWith(
116121
MethodCall|StaticCall $node,
117122
string $exceptionClass,

‎src/NodeFactory/AssertCallFactory.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
final class AssertCallFactory
1111
{
12+
/**
13+
* @template TCall of StaticCall|MethodCall
14+
* @param TCall $node
15+
* @return ($node is MethodCall ? MethodCall : StaticCall)
16+
*/
1217
public function createCallWithName(StaticCall|MethodCall $node, string $name): StaticCall|MethodCall
1318
{
1419
if ($node instanceof MethodCall) {

0 commit comments

Comments
 (0)