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 SkipInFormBuilderCallback 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 === null) {
return;
}

$this->use($someType);

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

private function use(SomeType $someType): void
{
}
}
8 changes: 8 additions & 0 deletions rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
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 @@ -120,6 +122,7 @@ 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 @@ -133,6 +136,11 @@ 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
11 changes: 11 additions & 0 deletions stubs/Symfony/Component/Form/AbstractType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\Form;

if (class_exists('Symfony\Component\Form\AbstractType')) {
return;
}

class AbstractType
{
}
12 changes: 12 additions & 0 deletions stubs/Symfony/Component/Form/FormBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symfony\Component\Form;

if (interface_exists('Symfony\Component\Form\FormBuilderInterface')) {
return;
}

class FormBuilderInterface
{
public function add(string|self $child, ?string $type = null, array $options = []): static;
}
20 changes: 20 additions & 0 deletions stubs/Symfony/Component/Validator/Constraints/Callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Symfony\Component\Validator\Constraints;

if (class_exists('Symfony\Component\Validator\Constraints\Callback')) {
return;
}

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Callback
{
/**
* @var string|callable
*/
public $callback;

public function __construct(array|string|callable|null $callback = null, ?array $groups = null, mixed $payload = null, array $options = [])
{
}
}