Skip to content

Commit

Permalink
Skip DateTime in ExpectedNameResolver (#5186)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 18, 2023
1 parent c9b0c27 commit 96fae07
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector\Fixture;

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;

final class SkipRewindableGenerator
{
public function __construct(RewindableGenerator $items)
{
public function __construct(
RewindableGenerator $items
) {
}
}
15 changes: 11 additions & 4 deletions rules/Naming/Guard/BreakingVariableRenameGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,16 @@ private function isDateTimeAtNamingConvention(Param $param): bool

private function isGenerator(Param $param): bool
{
return $this->nodeTypeResolver->isObjectType(
$param,
new ObjectType('Symfony\Component\DependencyInjection\Argument\RewindableGenerator')
);
if (! $param->type instanceof Node) {
return false;
}

$paramType = $this->nodeTypeResolver->getType($param);
if (! $paramType instanceof ObjectType) {
return false;
}

return $paramType->isInstanceOf('Symfony\Component\DependencyInjection\Argument\RewindableGenerator')
->yes();
}
}
20 changes: 18 additions & 2 deletions rules/Naming/Naming/ExpectedNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ public function resolveForCall(MethodCall | StaticCall | FuncCall $expr): ?strin
return null;
}

if ($returnedType instanceof ObjectType && $returnedType->isInstanceOf('DateTimeInterface')->yes()) {
// skip date time, as custom naming
if ($this->isDateTimeType($returnedType)) {
return null;
}

Expand Down Expand Up @@ -213,4 +212,21 @@ private function resolveReturnTypeFromArrayType(ArrayType $arrayType): ?Type

return $arrayType->getItemType();
}

/**
* Skip date time, as custom naming
*/
private function isDateTimeType(Type $type): bool
{
if (! $type instanceof ObjectType) {
return false;
}

if ($type->isInstanceOf('DateTimeInterface')->yes()) {
return true;
}

return $type->isInstanceOf('DateTime')
->yes();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Symfony\Component\DependencyInjection\Argument;

if (class_exists('Symfony\\Component\\DependencyInjection\\Argument\\RewindableGenerator')) {
return;
}

/**
* @internal
*/
class RewindableGenerator implements \IteratorAggregate, \Countable
{
private $generator;
private $count;

/**
* @param int|callable $count
*/
public function __construct(callable $generator, $count)
{
$this->generator = $generator;
$this->count = $count;
}

public function getIterator(): \Traversable
{
$g = $this->generator;

return $g();
}

public function count(): int
{
if (\is_callable($count = $this->count)) {
$this->count = $count();
}

return $this->count;
}
}

0 comments on commit 96fae07

Please sign in to comment.