Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
64 changes: 64 additions & 0 deletions tests/PHPStan/Analyser/nsrt/narrow-bool-cast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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);
}


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

function bug7685(Reader $reader): void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extremely cool! But please follow the usual convention for regression tests - one file per issue, with bug-xxx.php filename and namespace. I'm used to look for them in this format.

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

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);
}

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);
}
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