Skip to content

Commit

Permalink
Merge pull request #1639 from rectorphp/tv-make-array-return-doc-work…
Browse files Browse the repository at this point in the history
…-only-with-arrays
  • Loading branch information
TomasVotruba authored Jan 5, 2022
2 parents 840bd5e + b45ff7a commit 49f1596
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 208 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Rector\Privatization\TypeManipulator\TypeNormalizer;
use Rector\TypeDeclaration\NodeTypeAnalyzer\DetailedTypeAnalyzer;
use Rector\TypeDeclaration\TypeAnalyzer\AdvancedArrayAnalyzer;
use Rector\TypeDeclaration\TypeAnalyzer\IterableTypeAnalyzer;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer\ReturnTypeDeclarationReturnTypeInfererTypeInferer;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
Expand All @@ -45,6 +46,7 @@ public function __construct(
private readonly ReturnTagRemover $returnTagRemover,
private readonly DetailedTypeAnalyzer $detailedTypeAnalyzer,
private readonly TypeNormalizer $typeNormalizer,
private readonly IterableTypeAnalyzer $iterableTypeAnalyzer,
) {
}

Expand Down Expand Up @@ -178,6 +180,10 @@ private function shouldSkipType(
ClassMethod $classMethod,
PhpDocInfo $phpDocInfo
): bool {
if (! $this->iterableTypeAnalyzer->isIterableType($newType)) {
return true;
}

if ($newType instanceof ArrayType && $this->shouldSkipArrayType($newType, $classMethod, $phpDocInfo)) {
return true;
}
Expand Down
69 changes: 69 additions & 0 deletions rules/TypeDeclaration/TypeAnalyzer/IterableTypeAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclaration\TypeAnalyzer;

use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IterableType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;

final class IterableTypeAnalyzer
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
) {
}

public function isIterableType(Type $type): bool
{
if ($this->isUnionOfIterableTypes($type)) {
return true;
}

if ($type instanceof ArrayType) {
return true;
}

if ($type instanceof IterableType) {
return true;
}

if ($type instanceof GenericObjectType) {
if (! $this->reflectionProvider->hasClass($type->getClassName())) {
return false;
}

$genericObjectTypeClassReflection = $this->reflectionProvider->getClass($type->getClassName());
if ($genericObjectTypeClassReflection->implementsInterface('Traversable')) {
return true;
}
}

return false;
}

private function isUnionOfIterableTypes(Type $type): bool
{
if (! $type instanceof UnionType) {
return false;
}

foreach ($type->getTypes() as $unionedType) {
// nullable union is allowed
if ($unionedType instanceof NullType) {
continue;
}

if (! $this->isIterableType($unionedType)) {
return false;
}
}

return true;
}
}
12 changes: 12 additions & 0 deletions src/functions/node_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
use PhpParser\PrettyPrinter\Standard;
use Tracy\Dumper;

if (! function_exists('dump_with_depth')) {
/**
* @param mixed $value
*/
function dump_with_depth($value, int $depth = 2): void
{
Dumper::dump($value, [
Dumper::DEPTH => $depth,
]);
}
}

if (! function_exists('dn')) {
function dn(Node $node, int $depth = 2): void
{
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 49f1596

Please sign in to comment.