Skip to content

Commit

Permalink
[Naming] Skip DateTime from renaming, to keep convention of named dat…
Browse files Browse the repository at this point in the history
…es (#5184)
  • Loading branch information
TomasVotruba committed Oct 18, 2023
1 parent 369f16a commit beeba17
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

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

final class KeepDate
{
private function getItemByRefOrNull(?\DateTime $startDate)
{
}
}
Expand Up @@ -4,9 +4,9 @@

namespace Rector\DeadCode\Rector\If_;

use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
Expand Down
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node\Param;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Naming\Naming\PropertyNaming;
use Rector\Naming\ValueObject\ExpectedName;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand All @@ -29,7 +30,7 @@ public function resolve(Param $param): ?string

// include nullable too
// skip date time + date time interface, as should be kept
if ($staticType->isSuperTypeOf(new ObjectType('DateTimeInterface'))->yes()) {
if ($this->isDateTimeType($staticType)) {
return null;
}

Expand All @@ -40,4 +41,14 @@ public function resolve(Param $param): ?string

return $expectedName->getName();
}

private function isDateTimeType(Type $type): bool
{
if ($type->isSuperTypeOf(new ObjectType('DateTimeInterface'))->yes()) {
return true;
}

return $type->isSuperTypeOf(new ObjectType('DateTime'))
->yes();
}
}
2 changes: 1 addition & 1 deletion src/PhpParser/Node/BetterNodeFinder.php
Expand Up @@ -192,7 +192,7 @@ static function (Node $subNode) use ($types, &$isFoundNode): ?int {
* @return T[]
*/
public function findInstancesOfInFunctionLikeScoped(
ClassMethod | Function_ | Closure $functionLike,
ClassMethod | Function_ | Closure $functionLike,
string|array $types
): array {
if (is_string($types)) {
Expand Down
Expand Up @@ -3,12 +3,12 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector;
use Rector\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector;
use Rector\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
DowngradeSetAccessibleReflectionPropertyRector::class,
DowngradeMatchToSwitchRector::class,
DowngradeSetAccessibleReflectionPropertyRector::class,
DowngradeMatchToSwitchRector::class,
]);
};

0 comments on commit beeba17

Please sign in to comment.