Skip to content

[typed-collections] Add nullable support to array wrapper rule #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2025
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,39 @@
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Fixture;

use Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Source\ItemType;
use Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Source\SomeClassWithSetter;

final class HandleDocBlockParamType
{
public function run()
{
$someVariable = [new ItemType(), new ItemType()];

$someClassWithSetter = new SomeClassWithSetter();
$someClassWithSetter->setDocblockItemsWithNullable($someVariable);
}
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Fixture;

use Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Source\ItemType;
use Rector\Doctrine\Tests\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector\Source\SomeClassWithSetter;

final class HandleDocBlockParamType
{
public function run()
{
$someVariable = [new ItemType(), new ItemType()];

$someClassWithSetter = new SomeClassWithSetter();
$someClassWithSetter->setDocblockItemsWithNullable(new \Doctrine\Common\Collections\ArrayCollection($someVariable));
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ final class SomeClassWithSetter
public function setItems(Collection $items, Collection $nextItems): void
{
}

/**
* @param Collection<int, ItemType> $items
*/
public function setDocblockItemsWithNullable($items = null): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ private function isCallWithCollectionParam(MethodCall|New_ $methodCallOrNew, int
return false;
}

if ($activeParameterReflection->getType() instanceof ObjectType) {
/** @var ObjectType $paramObjectType */
$paramObjectType = $activeParameterReflection->getType();
$parameterType = $activeParameterReflection->getType();

return $paramObjectType->isInstanceOf(DoctrineClass::COLLECTION)->yes();
// to include nullables
$parameterType = TypeCombinator::removeNull($parameterType);
if (! $parameterType instanceof ObjectType) {
return false;
}

return false;
return $parameterType->isInstanceOf(DoctrineClass::COLLECTION)->yes();
}
}
Loading