Skip to content

Commit 618e671

Browse files
staabmphpstan-bot
andauthored
Remember call expression as truthy/falsey alongside @phpstan-assert-if-true argument narrowing (#5885)
Co-authored-by: phpstan-bot <79867460+phpstan-bot@users.noreply.github.com>
1 parent 614b589 commit 618e671

5 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎src/Analyser/ExprHandler/FuncCallHandler.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,9 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
936936
));
937937
$specifiedTypes = $typeSpecifier->specifyTypesFromAsserts($context, $expr, $asserts, $parametersAcceptor, $scope);
938938
if ($specifiedTypes !== null) {
939-
return $specifiedTypes;
939+
return $specifiedTypes
940+
->unionWith($typeSpecifier->handleDefaultTruthyOrFalseyContext($context, $expr, $scope))
941+
->setRootExpr($specifiedTypes->getRootExpr());
940942
}
941943
}
942944
}

‎src/Rules/Comparison/ImpossibleCheckTypeHelper.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,13 @@ private function getSpecifiedType(
350350
}
351351
}
352352
foreach ($sureTypes as $sureType) {
353+
if ($sureType[0] === $node) {
354+
// The check's own truthiness carries no information about
355+
// whether the check is redundant; the informative narrowing
356+
// lives in the argument entries.
357+
continue;
358+
}
359+
353360
if (self::isSpecified($typeSpecifierScope, $node, $sureType[0])) {
354361
$results[] = TrinaryLogic::createMaybe();
355362
continue;
@@ -384,6 +391,13 @@ private function getSpecifiedType(
384391
}
385392

386393
foreach ($sureNotTypes as $sureNotType) {
394+
if ($sureNotType[0] === $node) {
395+
// The check's own truthiness carries no information about
396+
// whether the check is redundant; the informative narrowing
397+
// lives in the argument entries.
398+
continue;
399+
}
400+
387401
if (self::isSpecified($typeSpecifierScope, $node, $sureNotType[0])) {
388402
$results[] = TrinaryLogic::createMaybe();
389403
continue;

‎src/Rules/Keywords/RequireFileExistsRule.php‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@
3333
final class RequireFileExistsRule implements Rule
3434
{
3535

36+
/**
37+
* Functions that, when they return true, guarantee the path exists on the
38+
* filesystem, so guarding a require/include with them suppresses the error.
39+
*/
40+
private const FILE_EXISTENCE_FUNCTIONS = [
41+
'file_exists',
42+
'is_file',
43+
'is_readable',
44+
'is_writable',
45+
'is_writeable',
46+
'is_executable',
47+
];
48+
3649
public function __construct(
3750
#[AutowiredParameter]
3851
private string $currentWorkingDirectory,
@@ -183,7 +196,7 @@ private function resolveFilePaths(Expr $expr, Scope $scope, bool &$magicDirFallb
183196

184197
private function isInFileExists(Include_ $node, Scope $scope): bool
185198
{
186-
foreach (['file_exists', 'is_file'] as $funcName) {
199+
foreach (self::FILE_EXISTENCE_FUNCTIONS as $funcName) {
187200
$expr = new FuncCall(new FullyQualified($funcName), [
188201
new Arg($node->expr),
189202
]);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14829;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function testFunction(string $path): void
8+
{
9+
// is_readable() has @phpstan-assert-if-true in stubs/file.stub
10+
if (is_readable($path)) {
11+
assertType('true', is_readable($path));
12+
assertType('non-empty-string', $path);
13+
}
14+
15+
if (!is_readable($path)) {
16+
return;
17+
}
18+
assertType('true', is_readable($path));
19+
}

‎tests/PHPStan/Rules/Keywords/data/include-in-file-exists.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@
88
require __DIR__ . '/../vendor/autoload.php';
99
}
1010

11+
if (is_readable(__DIR__ . '/../vendor/autoload.php')) {
12+
require __DIR__ . '/../vendor/autoload.php';
13+
}
14+
15+
if (is_writable(__DIR__ . '/../vendor/autoload.php')) {
16+
require __DIR__ . '/../vendor/autoload.php';
17+
}
18+
19+
if (is_writeable(__DIR__ . '/../vendor/autoload.php')) {
20+
require __DIR__ . '/../vendor/autoload.php';
21+
}
22+
23+
if (is_executable(__DIR__ . '/../vendor/autoload.php')) {
24+
require __DIR__ . '/../vendor/autoload.php';
25+
}
26+
1127
foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/../autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) {
1228
if (file_exists($file)) {
1329
require $file;

0 commit comments

Comments
 (0)