Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;

use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject;

final class ObjectsOverruleEmptyArray
{
public function go(array $emptyArray)
{
$this->run($this->getItemsWithArray());
$this->run($this->getItems());

$this->run($emptyArray);
}

public function run(array $result)
{
}

/**
* @return SomeReturnedObject[]|array
*/
private function getItems()
{
return [];
}

/**
* @return SomeReturnedObject[]
*/
private function getItemsWithArray()
{
return [];
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;

use Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject;

final class ObjectsOverruleEmptyArray
{
public function go(array $emptyArray)
{
$this->run($this->getItemsWithArray());
$this->run($this->getItems());

$this->run($emptyArray);
}

/**
* @param \Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject[] $result
*/
public function run(array $result)
{
}

/**
* @return SomeReturnedObject[]|array
*/
private function getItems()
{
return [];
}

/**
* @return SomeReturnedObject[]
*/
private function getItemsWithArray()
{
return [];
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source;

final class SomeReturnedObject
{
}
28 changes: 24 additions & 4 deletions rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\VariadicPlaceholder;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;
Expand Down Expand Up @@ -83,7 +84,7 @@ public function resolveTypesFromCalls(array $calls): array
}

// unite to single type
return $this->unionToSingleType($staticTypesByArgumentPosition);
return $this->unionToSingleType($staticTypesByArgumentPosition, true);
}

private function resolveStrictArgValueType(Arg $arg): Type
Expand Down Expand Up @@ -113,13 +114,19 @@ private function correctSelfType(Type $argValueType): Type
* @param array<int, Type[]> $staticTypesByArgumentPosition
* @return array<int, Type>
*/
private function unionToSingleType(array $staticTypesByArgumentPosition): array
private function unionToSingleType(array $staticTypesByArgumentPosition, bool $removeMixedArray = false): array
{
$staticTypeByArgumentPosition = [];

foreach ($staticTypesByArgumentPosition as $position => $staticTypes) {
$unionedType = $this->typeFactory->createMixedPassedOrUnionType($staticTypes);
if ($removeMixedArray) {
$staticTypes = array_filter(
$staticTypes,
fn (Type $type): bool => ! $this->isArrayMixedMixedType($type)
);
}

// narrow parents to most child type
$unionedType = $this->typeFactory->createMixedPassedOrUnionType($staticTypes);

$staticTypeByArgumentPosition[$position] = $this->narrowParentObjectTreeToSingleObjectChildType(
$unionedType
Expand Down Expand Up @@ -212,4 +219,17 @@ private function isEmptyArray(Expr $expr): bool

return $expr->items === [];
}

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

if (! $type->getItemType() instanceof MixedType) {
return false;
}

return $type->getKeyType() instanceof MixedType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public function correct(Type $mainType): Type

if ($type instanceof ArrayType
&& $type->getIterableValueType() instanceof IntersectionType
&& $type->getIterableValueType()->isString()->yes()) {
&& $type->getIterableValueType()
->isString()
->yes()) {
return new ArrayType(new MixedType(), new StringType());
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/PostRector/Collector/UseNodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ public function getUseImportTypesByNode(File $file): array
return $objectTypes;
}

public function hasImport(
File $file,
FullyQualifiedObjectType $fullyQualifiedObjectType
): bool {
public function hasImport(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType): bool
{
$useImports = $this->getUseImportTypesByNode($file);

foreach ($useImports as $useImport) {
Expand Down
Loading