Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector\Fixture;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;

final class SkipInFormBuilderCallbackInstanceof extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('someType', DocumentType::class, [
'class' => SomeType::class,
'required' => false,
'label' => 'Some Type',
'attr' => [
'data-help' => 'some data help',
],
'constraints' => [
new Assert\Callback(function ($someType, ExecutionContextInterface $context): void {
if (! $someType instanceof SomeType) {
return;
}

$this->use($someType);

// some logic here
})
]
]);
}

private function use(SomeType $someType): void
{
}
}
6 changes: 3 additions & 3 deletions rules/TypeDeclaration/Guard/ParamTypeAddGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\If_;
use PhpParser\NodeVisitor;
use Rector\NodeNameResolver\NodeNameResolver;
Expand All @@ -25,14 +25,14 @@ public function __construct(
) {
}

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

$isLegal = true;

$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
(array) $classMethod->stmts,
(array) $functionLike->getStmts(),
function (Node $subNode) use (&$isLegal, $paramName): ?int {
if ($subNode instanceof Assign && $subNode->var instanceof Variable && $this->nodeNameResolver->isName(
$subNode->var,
Expand Down
8 changes: 0 additions & 8 deletions rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\PhpParser\AstResolver;
use Rector\PHPStan\ScopeFetcher;
use Rector\StaticTypeMapper\StaticTypeMapper;

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

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

$currentType = $scope->getType($arg->value);
if ($currentType instanceof MixedType && $currentType->getSubtractedType() instanceof Type) {
return null;
}

return $argPosition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function getNodeTypes(): array
}

/**
* @param ClassMethod|Function_|Closure $node
* @param ClassMethod|Function_|Closure|ArrowFunction $node
*/
public function refactor(Node $node): ?Node
{
Expand Down Expand Up @@ -146,10 +146,6 @@ private function shouldSkipParam(
return true;
}

if (! $functionLike instanceof ClassMethod) {
return false;
}

return ! $this->paramTypeAddGuard->isLegal($param, $functionLike);
}

Expand Down