Skip to content
Open
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
26 changes: 22 additions & 4 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,25 +592,43 @@
$arrayTypeType = TypeCombinator::union(...$arrayTypeTypes);
$addArray = true;

// Pass 1: merge non-array iterables (ObjectType, IterableType)
$nonArrayMerged = false;
foreach ($otherTypeTypes as &$type) {
if (!$type->isIterable()->yes() || !$type->getIterableValueType()->isSuperTypeOf($arrayTypeType)->yes()) {
continue;
}

if ($type instanceof ObjectType && !$type instanceof GenericObjectType) {
$type = new IntersectionType([$type, new IterableType(new MixedType(), $arrayTypeType)]);
} elseif ($type instanceof ArrayType) {
$type = new ArrayType(new MixedType(), $arrayTypeType);
} elseif ($type instanceof ConstantArrayType) {
$type = new ArrayType(new MixedType(), $arrayTypeType);
$nonArrayMerged = true;
} elseif ($type instanceof IterableType) {
$type = new IterableType(new MixedType(), $arrayTypeType);
$nonArrayMerged = true;
} else {
continue;
}

$addArray = false;
}
unset($type);

// Pass 2: merge array types only if non-array iterables were also merged
if ($nonArrayMerged) {
foreach ($otherTypeTypes as &$type) {
if (!$type->isIterable()->yes() || !$type->getIterableValueType()->isSuperTypeOf($arrayTypeType)->yes()) {

Check warning on line 619 in src/PhpDoc/TypeNodeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ // Pass 2: merge array types only if non-array iterables were also merged if ($nonArrayMerged) { foreach ($otherTypeTypes as &$type) { - if (!$type->isIterable()->yes() || !$type->getIterableValueType()->isSuperTypeOf($arrayTypeType)->yes()) { + if (!$type->isIterable()->yes() || $type->getIterableValueType()->isSuperTypeOf($arrayTypeType)->no()) { continue; }

Check warning on line 619 in src/PhpDoc/TypeNodeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ // Pass 2: merge array types only if non-array iterables were also merged if ($nonArrayMerged) { foreach ($otherTypeTypes as &$type) { - if (!$type->isIterable()->yes() || !$type->getIterableValueType()->isSuperTypeOf($arrayTypeType)->yes()) { + if ($type->isIterable()->no() || !$type->getIterableValueType()->isSuperTypeOf($arrayTypeType)->yes()) { continue; }

Check warning on line 619 in src/PhpDoc/TypeNodeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ // Pass 2: merge array types only if non-array iterables were also merged if ($nonArrayMerged) { foreach ($otherTypeTypes as &$type) { - if (!$type->isIterable()->yes() || !$type->getIterableValueType()->isSuperTypeOf($arrayTypeType)->yes()) { + if (!$type->isIterable()->yes() || $type->getIterableValueType()->isSuperTypeOf($arrayTypeType)->no()) { continue; }

Check warning on line 619 in src/PhpDoc/TypeNodeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ // Pass 2: merge array types only if non-array iterables were also merged if ($nonArrayMerged) { foreach ($otherTypeTypes as &$type) { - if (!$type->isIterable()->yes() || !$type->getIterableValueType()->isSuperTypeOf($arrayTypeType)->yes()) { + if ($type->isIterable()->no() || !$type->getIterableValueType()->isSuperTypeOf($arrayTypeType)->yes()) { continue; }
continue;
}

if (!($type instanceof ArrayType) && !($type instanceof ConstantArrayType)) {
continue;
}

$type = new ArrayType(new MixedType(), $arrayTypeType);
$addArray = false;
}
unset($type);
}

if ($addArray) {
$otherTypeTypes[] = new ArrayType(new MixedType(), $arrayTypeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function testRule(): void
'Function MissingFunctionParameterTypehint\namespacedFunction() has parameter $d with no type specified.',
24,
],
[
'Function MissingFunctionParameterTypehint\intIterableTypehint() has parameter $a with no value type specified in iterable type array.',
31,
MissingTypehintCheck::MISSING_ITERABLE_VALUE_TYPE_TIP,
],
[
'Function MissingFunctionParameterTypehint\missingArrayTypehint() has parameter $a with no value type specified in iterable type array.',
36,
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3938,4 +3938,13 @@ public function testBug11463(): void
]);
}

public function testBug3128(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = false;
$this->analyse([__DIR__ . '/data/bug-3128.php'], []);
}

}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-3128.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace Bug3128;

use stdClass;

final class HelloWorld
{
/**
* @param array|stdClass[] $foo
*/
public function addTos($foo): void {
}

/**
* @param stdClass[]|array $bar
*/
public function addTosReversed($bar): void {
}
}

$a = new HelloWorld();
$a->addTos([1]);
$a->addTosReversed([1]);
Loading