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
10 changes: 9 additions & 1 deletion src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1095,9 +1095,17 @@ public function getPlusType(Expr $left, Expr $right, callable $getTypeCallback):
new BooleanType(),
]);

if ($plusable->isSuperTypeOf($leftType)->yes() && $plusable->isSuperTypeOf($rightType)->yes()) {
$plusableSuperTypeOfLeft = $plusable->isSuperTypeOf($leftType)->yes();
$plusableSuperTypeOfRight = $plusable->isSuperTypeOf($rightType)->yes();
if ($plusableSuperTypeOfLeft && $plusableSuperTypeOfRight) {
return TypeCombinator::union($leftType, $rightType);
}
if ($plusableSuperTypeOfLeft && $rightType instanceof MixedType) {
return $leftType;
}
if ($plusableSuperTypeOfRight && $leftType instanceof MixedType) {
return $rightType;
}
}

return $this->resolveCommonMath(new BinaryOp\Plus($left, $right), $leftType, $rightType);
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1039,4 +1039,9 @@ public function testBug11491(): void
$this->analyse([__DIR__ . '/data/bug-11491.php'], []);
}

public function testBug3759(): void
{
$this->analyse([__DIR__ . '/data/bug-3759.php'], []);
}

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

namespace Bug3759;

class A
{
/**
* @return mixed[]
*/
function modules(): array
{
$modules = moduleList();
if (!$modules) {
return [];
}

return $modules['major'] + $modules['minor'] + $modules['patch'];
}

/**
* @return mixed[]
*/
function moduleList(): array
{
return [
'major' => ['x' => 'x'],
'minor' => ['y' => 'y'],
'patch' => ['z' => 'z'],
];
}
}
Loading