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,41 @@
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\NullsafeMethodCall\RemoveNullsafeOnCollectionRector\Fixture;

use Doctrine\Common\Collections\Collection;

final class IncludeMixOfDocblock
{
/**
* @var Collection<string>|null
*/
public Collection $items;

public function someMethod()
{
$values = $this->items?->count();
}
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\NullsafeMethodCall\RemoveNullsafeOnCollectionRector\Fixture;

use Doctrine\Common\Collections\Collection;

final class IncludeMixOfDocblock
{
/**
* @var Collection<string>|null
*/
public Collection $items;

public function someMethod()
{
$values = $this->items->count();
}
}

?>
10 changes: 9 additions & 1 deletion rules/TypedCollections/TypeAnalyzer/CollectionTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public function __construct(
public function isCollectionNonNullableType(Expr $expr): bool
{
$exprType = $this->nodeTypeResolver->getType($expr);

return $this->isCollectionObjectType($exprType);
}

public function isCollectionType(Expr $expr): bool
{
$exprType = $this->nodeTypeResolver->getType($expr);

if ($exprType instanceof IntersectionType) {
foreach ($exprType->getTypes() as $intersectionedType) {
if ($this->isCollectionObjectType($intersectionedType)) {
Expand All @@ -48,6 +48,14 @@ public function isCollectionType(Expr $expr): bool

private function isCollectionObjectType(Type $exprType): bool
{
if ($exprType instanceof IntersectionType) {
foreach ($exprType->getTypes() as $intersectionedType) {
if ($this->isCollectionObjectType($intersectionedType)) {
return true;
}
}
}

if (! $exprType instanceof ObjectType) {
return false;
}
Expand Down
8 changes: 7 additions & 1 deletion stubs/Common/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
return;
}

interface Collection
/**
* @psalm-template TKey of array-key
* @psalm-template T
* @template-extends ReadableCollection<TKey, T>
* @template-extends ArrayAccess<TKey, T>
*/
interface Collection extends ReadableCollection, \ArrayAccess
{
}