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
4 changes: 3 additions & 1 deletion src/Analyser/ExprHandler/FuncCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,9 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
));
$specifiedTypes = $typeSpecifier->specifyTypesFromAsserts($context, $expr, $asserts, $parametersAcceptor, $scope);
if ($specifiedTypes !== null) {
return $specifiedTypes;
return $specifiedTypes
->unionWith($typeSpecifier->handleDefaultTruthyOrFalseyContext($context, $expr, $scope))
->setRootExpr($specifiedTypes->getRootExpr());
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,13 @@ private function getSpecifiedType(
}
}
foreach ($sureTypes as $sureType) {
if ($sureType[0] === $node) {
// The check's own truthiness carries no information about
// whether the check is redundant; the informative narrowing
// lives in the argument entries.
continue;
}

if (self::isSpecified($typeSpecifierScope, $node, $sureType[0])) {
$results[] = TrinaryLogic::createMaybe();
continue;
Expand Down Expand Up @@ -384,6 +391,13 @@ private function getSpecifiedType(
}

foreach ($sureNotTypes as $sureNotType) {
if ($sureNotType[0] === $node) {
// The check's own truthiness carries no information about
// whether the check is redundant; the informative narrowing
// lives in the argument entries.
continue;
}

if (self::isSpecified($typeSpecifierScope, $node, $sureNotType[0])) {
$results[] = TrinaryLogic::createMaybe();
continue;
Expand Down
15 changes: 14 additions & 1 deletion src/Rules/Keywords/RequireFileExistsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@
final class RequireFileExistsRule implements Rule
{

/**
* Functions that, when they return true, guarantee the path exists on the
* filesystem, so guarding a require/include with them suppresses the error.
*/
private const FILE_EXISTENCE_FUNCTIONS = [
'file_exists',
'is_file',
'is_readable',
'is_writable',
'is_writeable',
'is_executable',
];

public function __construct(
#[AutowiredParameter]
private string $currentWorkingDirectory,
Expand Down Expand Up @@ -183,7 +196,7 @@ private function resolveFilePaths(Expr $expr, Scope $scope, bool &$magicDirFallb

private function isInFileExists(Include_ $node, Scope $scope): bool
{
foreach (['file_exists', 'is_file'] as $funcName) {
foreach (self::FILE_EXISTENCE_FUNCTIONS as $funcName) {
$expr = new FuncCall(new FullyQualified($funcName), [
new Arg($node->expr),
]);
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14829.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace Bug14829;

use function PHPStan\Testing\assertType;

function testFunction(string $path): void
{
// is_readable() has @phpstan-assert-if-true in stubs/file.stub
if (is_readable($path)) {
assertType('true', is_readable($path));
assertType('non-empty-string', $path);
}

if (!is_readable($path)) {
return;
}
assertType('true', is_readable($path));
}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Keywords/data/include-in-file-exists.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@
require __DIR__ . '/../vendor/autoload.php';
}

if (is_readable(__DIR__ . '/../vendor/autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';
}

if (is_writable(__DIR__ . '/../vendor/autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';
}

if (is_writeable(__DIR__ . '/../vendor/autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';
}

if (is_executable(__DIR__ . '/../vendor/autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';
}

foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/../autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) {
if (file_exists($file)) {
require $file;
Expand Down
Loading