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
2 changes: 2 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ public function specifyTypesInCondition(
$context,
$rootExpr,
);
} elseif ($expr instanceof Expr\Cast\Bool_) {
return $this->resolveEqual(new Expr\BinaryOp\Equal($expr->expr, new ConstFetch(new Name\FullyQualified('true'))), $scope, $context, $rootExpr);
} elseif ($expr instanceof Node\Expr\BinaryOp\Equal) {
return $this->resolveEqual($expr, $scope, $context, $rootExpr);
} elseif ($expr instanceof Node\Expr\BinaryOp\NotEqual) {
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public function dataAssignInIf(): array
$testScope,
'matches3',
TrinaryLogic::createYes(),
'array{0?: string}',
'array{}|array{string}',
],
[
$testScope,
Expand Down Expand Up @@ -415,7 +415,7 @@ public function dataAssignInIf(): array
$testScope,
'ternaryMatches',
TrinaryLogic::createYes(),
'array{0?: string}',
'array{string}',
],
[
$testScope,
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10528.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Bug10528;

use function PHPStan\Testing\assertType;

function bug10528(string $string): void {
$pos = strpos('*', $string);
assert((bool) $pos);

assertType('int<1, max>', $pos);

$sub = substr($string, 0, $pos);
assert($pos !== FALSE);
$sub = substr($string, 0, $pos);
}

19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-6006.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bug6006;

use function PHPStan\Testing\assertType;

function bug6006() {
/** @var array<string, null|string> $data */
$data = [
'name' => 'John',
'dob' => null,
];

$data = array_filter($data, fn(?string $input): bool => (bool)$input);

assertType('array<string, non-falsy-string>', $data);
}


17 changes: 17 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-7685.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace bug7685;

use function PHPStan\Testing\assertType;

interface Reader {
public function getFilePath(): string|false;
}

function bug7685(Reader $reader): void {
$filePath = $reader->getFilePath();
if (false !== (bool) $filePath) {
assertType('non-falsy-string', $filePath);
}
}

29 changes: 29 additions & 0 deletions tests/PHPStan/Analyser/nsrt/narrow-bool-cast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace NarrowBoolCast;

use function PHPStan\Testing\assertType;

/** @param array<mixed> $arr */
function doFoo(string $x, array $arr): void {
if ((bool) strlen($x)) {
assertType('string', $x); // could be non-empty-string
} else {
assertType('string', $x);
}
assertType('string', $x);

if ((bool) array_search($x, $arr, true)) {
assertType('non-empty-array', $arr);
} else {
assertType('array', $arr);
}
assertType('string', $x);

if ((bool) preg_match('~.*~', $x, $matches)) {
assertType('array{string}', $matches);
} else {
assertType('array{}', $matches);
}
assertType('array{}|array{string}', $matches);
}
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,11 @@ public function testBug11518(): void
$this->analyse([__DIR__ . '/data/bug-11518.php'], []);
}

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

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

namespace Bug8881;

/**
* @param int[] $a
* @return int
*/
function pop($a)
{
assert((bool)$a);
return array_pop($a);
}
Loading