Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0b6d92d

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Fix count($x) > $n === true negation
1 parent 6705ac1 commit 0b6d92d

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

‎src/Analyser/TypeSpecifier.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,13 @@ public function specifyTypesInCondition(
243243
&& $leftType->isInteger()->yes()
244244
) {
245245
if (
246-
$context->truthy() && (IntegerRangeType::createAllGreaterThanOrEqualTo(1 - $offset)->isSuperTypeOf($leftType)->yes())
247-
|| ($context->falsey() && (new ConstantIntegerType(1 - $offset))->isSuperTypeOf($leftType)->yes())
246+
$context->true() && (IntegerRangeType::createAllGreaterThanOrEqualTo(1 - $offset)->isSuperTypeOf($leftType)->yes())
247+
|| ($context->false() && (new ConstantIntegerType(1 - $offset))->isSuperTypeOf($leftType)->yes())
248248
) {
249249
$argType = $scope->getType($expr->right->getArgs()[0]->value);
250250
if ($argType->isArray()->yes()) {
251251
$newType = new NonEmptyArrayType();
252-
if ($context->truthy() && $argType->isList()->yes()) {
252+
if ($context->true() && $argType->isList()->yes()) {
253253
$newType = AccessoryArrayListType::intersectWith($newType);
254254
}
255255

‎tests/PHPStan/Analyser/NodeScopeResolverTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,7 @@ public function dataFileAsserts(): iterable
14671467
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6613.php');
14681468
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10187.php');
14691469
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10834.php');
1470+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10952.php');
14701471
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10893.php');
14711472
}
14721473

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug10952;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
/**
10+
* @return array<int, string>
11+
*/
12+
public function getArray(): array
13+
{
14+
return array_fill(0, random_int(0, 10), 'test');
15+
}
16+
17+
public function test(): void
18+
{
19+
$array = $this->getArray();
20+
21+
if (count($array) > 1) {
22+
assertType('non-empty-array<int, string>', $array);
23+
} else {
24+
assertType('array<int, string>', $array);
25+
}
26+
27+
match (true) {
28+
count($array) > 1 => assertType('non-empty-array<int, string>', $array),
29+
default => assertType('array<int, string>', $array),
30+
};
31+
}
32+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /