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
4 changes: 4 additions & 0 deletions config/sets/typed-collections-docblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\TypedCollections\Rector\Class_\CompletePropertyDocblockFromToManyRector;
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector;
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\DefaultCollectionKeyRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
// safe rules that handle only docblocks
CollectionDocblockGenericTypeRector::class,
DefaultCollectionKeyRector::class,
CompletePropertyDocblockFromToManyRector::class,
]);
};
4 changes: 0 additions & 4 deletions config/sets/typed-collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
use Rector\Doctrine\TypedCollections\Rector\Assign\ArrayDimFetchAssignToAddCollectionCallRector;
use Rector\Doctrine\TypedCollections\Rector\Assign\ArrayOffsetSetToSetCollectionCallRector;
use Rector\Doctrine\TypedCollections\Rector\Class_\CompleteParamDocblockFromSetterToCollectionRector;
use Rector\Doctrine\TypedCollections\Rector\Class_\CompletePropertyDocblockFromToManyRector;
use Rector\Doctrine\TypedCollections\Rector\Class_\CompleteReturnDocblockFromToManyRector;
use Rector\Doctrine\TypedCollections\Rector\Class_\InitializeCollectionInConstructorRector;
use Rector\Doctrine\TypedCollections\Rector\Class_\RemoveNullFromInstantiatedArrayCollectionPropertyRector;
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\CollectionGetterNativeTypeRector;
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\CollectionParamTypeSetterToCollectionPropertyRector;
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\CollectionSetterParamNativeTypeRector;
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\DefaultCollectionKeyRector;
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\NarrowArrayCollectionToCollectionRector;
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\NarrowParamUnionToCollectionRector;
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\NarrowReturnUnionToCollectionRector;
Expand Down Expand Up @@ -73,15 +71,13 @@
RemoveNullFromNullableCollectionTypeRector::class,

// docblocks
DefaultCollectionKeyRector::class,
NarrowArrayCollectionToCollectionRector::class,

// @param docblock
CompleteParamDocblockFromSetterToCollectionRector::class,
NarrowParamUnionToCollectionRector::class,

// @var docblock
CompletePropertyDocblockFromToManyRector::class,
NarrowPropertyUnionToCollectionRector::class,

// @return docblock
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\Class_\CompletePropertyDocblockFromToManyRector\Fixture\Odm;

use Rector\Doctrine\Tests\TypedCollections\Rector\Class_\CompletePropertyDocblockFromToManyRector\Source\Training;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations\ReferenceMany;

#[Document]
final class OdmMany
{
#[ReferenceMany(targetDocument: Training::class)]
private $items = [];

public function getItems()
{
return $this->items;
}
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\Class_\CompletePropertyDocblockFromToManyRector\Fixture\Odm;

use Rector\Doctrine\Tests\TypedCollections\Rector\Class_\CompletePropertyDocblockFromToManyRector\Source\Training;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations\ReferenceMany;

#[Document]
final class OdmMany
{
/**
* @var \Doctrine\Common\Collections\Collection<int, \Rector\Doctrine\Tests\TypedCollections\Rector\Class_\CompletePropertyDocblockFromToManyRector\Source\Training>
*/
#[ReferenceMany(targetDocument: Training::class)]
private $items = [];

public function getItems()
{
return $this->items;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @api
*/
final class OdmMappingKey
final class DocumentMappingKey
{
/**
* @var string
Expand Down
7 changes: 6 additions & 1 deletion src/NodeAnalyzer/TargetEntityResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Doctrine\CodeQuality\Enum\DocumentMappingKey;
use Rector\Doctrine\CodeQuality\Enum\EntityMappingKey;
use Rector\Exception\NotImplementedYetException;
use Rector\NodeNameResolver\NodeNameResolver;
Expand All @@ -29,7 +30,11 @@ public function resolveFromAttribute(Attribute $attribute): ?string
continue;
}

if ($arg->name->toString() !== EntityMappingKey::TARGET_ENTITY) {
if (! in_array(
$arg->name->toString(),
[EntityMappingKey::TARGET_ENTITY, DocumentMappingKey::TARGET_DOCUMENT],
true
)) {
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions src/NodeManipulator/ToManyRelationPropertyTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Rector\BetterPhpDocParser\PhpDoc\StringNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Doctrine\CodeQuality\Enum\CollectionMapping;
use Rector\Doctrine\CodeQuality\Enum\DocumentMappingKey;
use Rector\Doctrine\CodeQuality\Enum\EntityMappingKey;
use Rector\Doctrine\CodeQuality\Enum\OdmMappingKey;
use Rector\Doctrine\Enum\DoctrineClass;
use Rector\Doctrine\NodeAnalyzer\AttributeFinder;
use Rector\Doctrine\PhpDoc\ShortClassExpander;
Expand Down Expand Up @@ -48,7 +48,7 @@ public function resolve(Property $property): ?Type
$expr = $this->attributeFinder->findAttributeByClassesArgByNames(
$property,
CollectionMapping::TO_MANY_CLASSES,
[EntityMappingKey::TARGET_ENTITY, OdmMappingKey::TARGET_DOCUMENT]
[EntityMappingKey::TARGET_ENTITY, DocumentMappingKey::TARGET_DOCUMENT]
);

if (! $expr instanceof Expr) {
Expand All @@ -64,7 +64,7 @@ private function processToManyRelation(
): Type|null {
$targetEntityArrayItemNode = $doctrineAnnotationTagValueNode->getValue(
EntityMappingKey::TARGET_ENTITY
) ?: $doctrineAnnotationTagValueNode->getValue(OdmMappingKey::TARGET_DOCUMENT);
) ?: $doctrineAnnotationTagValueNode->getValue(DocumentMappingKey::TARGET_DOCUMENT);
if (! $targetEntityArrayItemNode instanceof ArrayItemNode) {
// most likely mapped superclass
return new ObjectType(DoctrineClass::COLLECTION);
Expand Down
4 changes: 2 additions & 2 deletions src/TypeAnalyzer/CollectionTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Doctrine\CodeQuality\Enum\CollectionMapping;
use Rector\Doctrine\CodeQuality\Enum\DocumentMappingKey;
use Rector\Doctrine\CodeQuality\Enum\EntityMappingKey;
use Rector\Doctrine\CodeQuality\Enum\OdmMappingKey;
use Rector\Doctrine\NodeAnalyzer\AttrinationFinder;
use Rector\Doctrine\NodeAnalyzer\TargetEntityResolver;
use Rector\Doctrine\PhpDoc\ShortClassExpander;
Expand Down Expand Up @@ -111,7 +111,7 @@ public function resolveFromToManyProperty(Property $property): ?FullyQualifiedOb
if ($doctrineAnnotationTagValueNodeOrAttribute instanceof Attribute) {
$targetEntityExpr = $this->findExprByArgNames(
$doctrineAnnotationTagValueNodeOrAttribute->args,
[EntityMappingKey::TARGET_ENTITY, OdmMappingKey::TARGET_DOCUMENT]
[EntityMappingKey::TARGET_ENTITY, DocumentMappingKey::TARGET_DOCUMENT]
);

if (! $targetEntityExpr instanceof ClassConstFetch) {
Expand Down