Skip to content

Commit dbfc750

Browse files
authored
[TypeDeclaration] Skip callback with instanceof check on ParamTypeByMethodCallTypeRector (part 2) (#7513)
* [TypeDeclaration] Skip callback with instanceof check on ParamTypeByMethodCallTypeRector (part 2) * fix * cs
1 parent d349cfd commit dbfc750

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Fixture;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\FormBuilderInterface;
7+
use Symfony\Component\Validator\Constraints as Assert;
8+
9+
final class SkipInFormBuilderCallbackInstanceof extends AbstractType
10+
{
11+
public function buildForm(FormBuilderInterface $builder, array $options): void
12+
{
13+
$builder->add('someType', DocumentType::class, [
14+
'class' => SomeType::class,
15+
'required' => false,
16+
'label' => 'Some Type',
17+
'attr' => [
18+
'data-help' => 'some data help',
19+
],
20+
'constraints' => [
21+
new Assert\Callback(function ($someType, ExecutionContextInterface $context): void {
22+
if (! $someType instanceof SomeType) {
23+
return;
24+
}
25+
26+
$this->use($someType);
27+
28+
// some logic here
29+
})
30+
]
31+
]);
32+
}
33+
34+
private function use(SomeType $someType): void
35+
{
36+
}
37+
}

rules/TypeDeclaration/Guard/ParamTypeAddGuard.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use PhpParser\Node\Expr\Assign;
99
use PhpParser\Node\Expr\Ternary;
1010
use PhpParser\Node\Expr\Variable;
11+
use PhpParser\Node\FunctionLike;
1112
use PhpParser\Node\Param;
12-
use PhpParser\Node\Stmt\ClassMethod;
1313
use PhpParser\Node\Stmt\If_;
1414
use PhpParser\NodeVisitor;
1515
use Rector\NodeNameResolver\NodeNameResolver;
@@ -25,14 +25,14 @@ public function __construct(
2525
) {
2626
}
2727

28-
public function isLegal(Param $param, ClassMethod $classMethod): bool
28+
public function isLegal(Param $param, FunctionLike $functionLike): bool
2929
{
3030
$paramName = $this->nodeNameResolver->getName($param);
3131

3232
$isLegal = true;
3333

3434
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
35-
(array) $classMethod->stmts,
35+
(array) $functionLike->getStmts(),
3636
function (Node $subNode) use (&$isLegal, $paramName): ?int {
3737
if ($subNode instanceof Assign && $subNode->var instanceof Variable && $this->nodeNameResolver->isName(
3838
$subNode->var,

rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
use PHPStan\Analyser\Scope;
2323
use PHPStan\Reflection\ClassReflection;
2424
use PHPStan\Type\MixedType;
25-
use PHPStan\Type\Type;
2625
use Rector\NodeNameResolver\NodeNameResolver;
2726
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
2827
use Rector\PhpParser\AstResolver;
29-
use Rector\PHPStan\ScopeFetcher;
3028
use Rector\StaticTypeMapper\StaticTypeMapper;
3129

3230
final readonly class CallerParamMatcher
@@ -122,7 +120,6 @@ private function matchCallArgPosition(StaticCall | MethodCall | FuncCall $call,
122120
{
123121
$paramName = $this->nodeNameResolver->getName($param);
124122

125-
$scope = ScopeFetcher::fetch($call);
126123
foreach ($call->args as $argPosition => $arg) {
127124
if (! $arg instanceof Arg) {
128125
continue;
@@ -136,11 +133,6 @@ private function matchCallArgPosition(StaticCall | MethodCall | FuncCall $call,
136133
continue;
137134
}
138135

139-
$currentType = $scope->getType($arg->value);
140-
if ($currentType instanceof MixedType && $currentType->getSubtractedType() instanceof Type) {
141-
return null;
142-
}
143-
144136
return $argPosition;
145137
}
146138

rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function getNodeTypes(): array
102102
}
103103

104104
/**
105-
* @param ClassMethod|Function_|Closure $node
105+
* @param ClassMethod|Function_|Closure|ArrowFunction $node
106106
*/
107107
public function refactor(Node $node): ?Node
108108
{
@@ -146,10 +146,6 @@ private function shouldSkipParam(
146146
return true;
147147
}
148148

149-
if (! $functionLike instanceof ClassMethod) {
150-
return false;
151-
}
152-
153149
return ! $this->paramTypeAddGuard->isLegal($param, $functionLike);
154150
}
155151

0 commit comments

Comments
 (0)