-
-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1639 from rectorphp/tv-make-array-return-doc-work…
…-only-with-arrays
- Loading branch information
Showing
8 changed files
with
87 additions
and
208 deletions.
There are no files selected for viewing
70 changes: 0 additions & 70 deletions
70
...n/Rector/ClassMethod/AddArrayReturnDocTypeRector/Fixture/call_reflection_resolver.php.inc
This file was deleted.
Oops, something went wrong.
46 changes: 0 additions & 46 deletions
46
...TypeDeclaration/Rector/ClassMethod/AddArrayReturnDocTypeRector/Fixture/has_offset.php.inc
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
rules/TypeDeclaration/TypeAnalyzer/IterableTypeAnalyzer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 0 additions & 42 deletions
42
tests/Issues/IssueEarlyReturnAndOrNarrow/Fixture/and_next_or.php.inc
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
tests/Issues/IssueEarlyReturnAndOrNarrow/IssueEarlyReturnAndOrNarrowTest.php
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
tests/Issues/IssueEarlyReturnAndOrNarrow/config/configured_rule.php
This file was deleted.
Oops, something went wrong.