Skip to content

Commit

Permalink
Fix RenamePropertyToMatchTypeRector to skip Laravel collections and a…
Browse files Browse the repository at this point in the history
…void accident rename (#4811)
  • Loading branch information
TomasVotruba committed Aug 18, 2023
1 parent 5363a67 commit 00f736f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Fixture;

use Illuminate\Support\Collection;

final class SkipCollectionOfType
{
private Collection $items;

public function __construct(Collection $items)
{
$this->items = $items;
}

public function getItems(): Collection
{
return $this->items;
}
}
12 changes: 9 additions & 3 deletions rules/Naming/Naming/PropertyNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ public function getExpectedNameFromMethodName(string $methodName): ?ExpectedName

public function getExpectedNameFromType(Type $type): ?ExpectedName
{
// keep doctrine collections untouched
if ($type instanceof ObjectType && $type->isInstanceOf('Doctrine\Common\Collections\Collection')->yes()) {
return null;
// keep collections untouched
if ($type instanceof ObjectType) {
if ($type->isInstanceOf('Doctrine\Common\Collections\Collection')->yes()) {
return null;
}

if ($type->isInstanceOf('Illuminate\Support\Collection')->yes()) {
return null;
}
}

$className = $this->resolveClassNameFromType($type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -135,11 +134,9 @@ private function getReturnTypeRecursive(ClassMethod $classMethod): ?Type
return null;
}

$parameterAcceptor = ParametersAcceptorSelector::selectSingle(
$parentMethodReflection->getVariants()
);
$parameterAcceptor = ParametersAcceptorSelector::selectSingle($parentMethodReflection->getVariants());

if (!$parameterAcceptor instanceof ParametersAcceptorWithPhpDocs) {
if (! $parameterAcceptor instanceof ParametersAcceptorWithPhpDocs) {
return null;
}

Expand Down
37 changes: 37 additions & 0 deletions stubs/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Illuminate\Support;

use Traversable;

if (class_exists('Illuminate\Support\Collection')) {
return;
}

class Collection implements \ArrayAccess, \IteratorAggregate
{
public function offsetGet(mixed $offset): mixed
{
// TODO: Implement offsetGet() method.
}

public function getIterator(): Traversable
{
// TODO: Implement getIterator() method.
}

public function offsetExists(mixed $offset): bool
{
// TODO: Implement offsetExists() method.
}

public function offsetSet(mixed $offset, mixed $value): void
{
// TODO: Implement offsetSet() method.
}

public function offsetUnset(mixed $offset): void
{
// TODO: Implement offsetUnset() method.
}
}

0 comments on commit 00f736f

Please sign in to comment.