Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+-operator with non-empty-array should lead to non-empty-array #739

Merged
merged 5 commits into from
Oct 26, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\BooleanType;
Expand Down Expand Up @@ -1290,6 +1291,7 @@ private function resolveType(Expr $node): Type

return TypeCombinator::union(...$resultTypes);
}

$arrayType = new ArrayType(new MixedType(), new MixedType());

if ($arrayType->isSuperTypeOf($leftType)->yes() && $arrayType->isSuperTypeOf($rightType)->yes()) {
Expand All @@ -1306,10 +1308,16 @@ private function resolveType(Expr $node): Type
}
$keyType = TypeCombinator::union(...$keyTypes);
}
return new ArrayType(

$arrayType = new ArrayType(
$keyType,
TypeCombinator::union($leftType->getIterableValueType(), $rightType->getIterableValueType())
);

if ($leftType->isIterableAtLeastOnce()->yes() || $rightType->isIterableAtLeastOnce()->yes()) {
return TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
}
return $arrayType;
}

if ($leftType instanceof MixedType && $rightType instanceof MixedType) {
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2830,7 +2830,7 @@ public function dataBinaryOperations(): array
'[1, 2, 3] + [4, 5, 6]',
],
[
'array<int>',
'non-empty-array<int>',
'$arrayOfUnknownIntegers + [1, 2, 3]',
],
[
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-map.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-map-closure.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-sum.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-plus.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4573.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4577.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4579.php');
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Analyser/data/array-plus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace ArrayPlus;

use function PHPStan\Testing\assertType;

class Foo
{
/**
* @param non-empty-array $s2
*/
public function doFoo(array $s1, $s2)
{
assertType('non-empty-array', $s1 + $s2);
assertType('non-empty-array', $s2 + $s1);
assertType('non-empty-array', $s2 + $s2);

assertType('array', $s1 + $s1);
}
}