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
11 changes: 8 additions & 3 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2335,8 +2335,13 @@
$leftNumberType = $leftType->toNumber();
$rightNumberType = $rightType->toNumber();

// a template type of mixed does not absorb the other operand in the union
$typesMixed = $types instanceof MixedType
|| $leftType instanceof MixedType
|| $rightType instanceof MixedType;

if (
!$types instanceof MixedType
!$typesMixed
&& (
$rightNumberType instanceof IntegerRangeType
|| $rightNumberType instanceof ConstantIntegerType
Expand Down Expand Up @@ -2397,14 +2402,14 @@

$resultType = TypeCombinator::union($leftNumberType, $rightNumberType);
if ($expr instanceof Expr\BinaryOp\Div) {
if ($types instanceof MixedType || $resultType->isInteger()->yes()) {
if ($typesMixed || $resultType->isInteger()->yes()) {

Check warning on line 2405 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $resultType = TypeCombinator::union($leftNumberType, $rightNumberType); if ($expr instanceof Expr\BinaryOp\Div) { - if ($typesMixed || $resultType->isInteger()->yes()) { + if ($typesMixed || !$resultType->isInteger()->no()) { return new BenevolentUnionType([new IntegerType(), new FloatType()]); }

Check warning on line 2405 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $resultType = TypeCombinator::union($leftNumberType, $rightNumberType); if ($expr instanceof Expr\BinaryOp\Div) { - if ($typesMixed || $resultType->isInteger()->yes()) { + if ($typesMixed || !$resultType->isInteger()->no()) { return new BenevolentUnionType([new IntegerType(), new FloatType()]); }
return new BenevolentUnionType([new IntegerType(), new FloatType()]);
}

return new UnionType([new IntegerType(), new FloatType()]);
}

if ($types instanceof MixedType
if ($typesMixed
|| $leftType instanceof BenevolentUnionType
|| $rightType instanceof BenevolentUnionType
) {
Expand Down
53 changes: 53 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15388.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types = 1);

namespace Bug15388;

use function PHPStan\Testing\assertType;

/** @param list<int> $vals */
function foo(array $vals): void {
assertType('(float|int)', array_reduce($vals, function($carry, $val) {
return $carry + map($val);
}, 0));
assertType('(float|int)', array_reduce($vals, function($carry, $val) {
return $carry + $val;
}, 0));
assertType('(float|int)', array_reduce($vals, function($carry, int $val) {
return $carry - $val;
}, 0));
assertType('(float|int)', array_reduce($vals, function($carry, int $val) {
return $carry * $val;
}, 1));
}

function map(int $val): int {
return $val;
}

/**
* @template T
* @param T $t
* @param mixed $m
*/
function templateOfMixed($t, $m, int $i): void
{
assertType('(float|int)', $m + $i);
assertType('(float|int)', $t + $i);
assertType('(float|int)', $i + $t);
assertType('(float|int)', $t + 1);
assertType('(float|int)', $t - $i);
assertType('(float|int)', $t - 1);
assertType('(float|int)', $t * $i);
assertType('(float|int)', $t / $i);
assertType('(float|int)', $t ** $i);
assertType('(float|int)', $t << $i);
assertType('int', $t % $i);

$a = $t;
$a += $i;
assertType('(float|int)', $a);

$a = $t;
$a++;
assertType('(float|int)', $a);
}
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,13 @@ public function testBug11507(): void
$this->analyse([__DIR__ . '/data/bug-11507.php'], []);
}

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

public function testBug15245(): void
{
$this->checkNullables = true;
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-15388.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace Bug15388ReturnType;

/** @param list<int> $vals */
function foo(array $vals): int {
return array_reduce($vals, function($carry, $val) {
return $carry + map($val);
}, 0);
}

/** @param list<int> $vals */
function bar(array $vals): int {
return array_reduce($vals, function($carry, $val) {
return $carry + $val;
}, 0);
}

function map(int $val): int {
return $val;
}
5 changes: 4 additions & 1 deletion turbo-ext/src/InitializerExprTypeResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2678,7 +2678,10 @@ class InitializerExprTypeResolver
IETR_VAL(leftNumberType, callOn(leftType, mToNumber));
IETR_VAL(rightNumberType, callOn(rightType, mToNumber));

bool typesMixed = isA(types.raw(), pt_ce_mixed_type);
// a template type of mixed does not absorb the other operand in the union
bool typesMixed = isA(types.raw(), pt_ce_mixed_type)
|| isA(leftType, pt_ce_mixed_type)
|| isA(rightType, pt_ce_mixed_type);
if (
!typesMixed
&& (
Expand Down
Loading