Skip to content

Commit 39892b6

Browse files
authored
TypeSpecifier: Narrow (bool) $expr like $expr == true
1 parent 24bae94 commit 39892b6

File tree

8 files changed

+106
-2
lines changed

8 files changed

+106
-2
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ public function specifyTypesInCondition(
196196
$context,
197197
$rootExpr,
198198
);
199+
} elseif ($expr instanceof Expr\Cast\Bool_) {
200+
return $this->resolveEqual(new Expr\BinaryOp\Equal($expr->expr, new ConstFetch(new Name\FullyQualified('true'))), $scope, $context, $rootExpr);
199201
} elseif ($expr instanceof Node\Expr\BinaryOp\Equal) {
200202
return $this->resolveEqual($expr, $scope, $context, $rootExpr);
201203
} elseif ($expr instanceof Node\Expr\BinaryOp\NotEqual) {

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public function dataAssignInIf(): array
355355
$testScope,
356356
'matches3',
357357
TrinaryLogic::createYes(),
358-
'array{0?: string}',
358+
'array{}|array{string}',
359359
],
360360
[
361361
$testScope,
@@ -415,7 +415,7 @@ public function dataAssignInIf(): array
415415
$testScope,
416416
'ternaryMatches',
417417
TrinaryLogic::createYes(),
418-
'array{0?: string}',
418+
'array{string}',
419419
],
420420
[
421421
$testScope,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Bug10528;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function bug10528(string $string): void {
8+
$pos = strpos('*', $string);
9+
assert((bool) $pos);
10+
11+
assertType('int<1, max>', $pos);
12+
13+
$sub = substr($string, 0, $pos);
14+
assert($pos !== FALSE);
15+
$sub = substr($string, 0, $pos);
16+
}
17+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Bug6006;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function bug6006() {
8+
/** @var array<string, null|string> $data */
9+
$data = [
10+
'name' => 'John',
11+
'dob' => null,
12+
];
13+
14+
$data = array_filter($data, fn(?string $input): bool => (bool)$input);
15+
16+
assertType('array<string, non-falsy-string>', $data);
17+
}
18+
19+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace bug7685;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
interface Reader {
8+
public function getFilePath(): string|false;
9+
}
10+
11+
function bug7685(Reader $reader): void {
12+
$filePath = $reader->getFilePath();
13+
if (false !== (bool) $filePath) {
14+
assertType('non-falsy-string', $filePath);
15+
}
16+
}
17+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace NarrowBoolCast;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/** @param array<mixed> $arr */
8+
function doFoo(string $x, array $arr): void {
9+
if ((bool) strlen($x)) {
10+
assertType('string', $x); // could be non-empty-string
11+
} else {
12+
assertType('string', $x);
13+
}
14+
assertType('string', $x);
15+
16+
if ((bool) array_search($x, $arr, true)) {
17+
assertType('non-empty-array', $arr);
18+
} else {
19+
assertType('array', $arr);
20+
}
21+
assertType('string', $x);
22+
23+
if ((bool) preg_match('~.*~', $x, $matches)) {
24+
assertType('array{string}', $matches);
25+
} else {
26+
assertType('array{}', $matches);
27+
}
28+
assertType('array{}|array{string}', $matches);
29+
}

tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,11 @@ public function testBug11518(): void
290290
$this->analyse([__DIR__ . '/data/bug-11518.php'], []);
291291
}
292292

293+
public function testBug8881(): void
294+
{
295+
$this->checkExplicitMixed = true;
296+
$this->checkNullables = true;
297+
$this->analyse([__DIR__ . '/data/bug-8881.php'], []);
298+
}
299+
293300
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug8881;
4+
5+
/**
6+
* @param int[] $a
7+
* @return int
8+
*/
9+
function pop($a)
10+
{
11+
assert((bool)$a);
12+
return array_pop($a);
13+
}

0 commit comments

Comments
 (0)