Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/Analyser/ExprHandler/MethodCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,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
4 changes: 3 additions & 1 deletion src/Analyser/ExprHandler/StaticCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,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
52 changes: 52 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14829.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

namespace Bug14829;

use function PHPStan\Testing\assertType;

class Checker
{

/** @phpstan-assert-if-true =non-empty-string $path */
public function isReadable(string $path): bool
{
return $path !== '';
}

/** @phpstan-assert-if-true =non-empty-string $path */
public static function staticIsReadable(string $path): bool
{
return $path !== '';
}

}

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

function testMethod(Checker $c, string $path): void
{
if ($c->isReadable($path)) {
assertType('true', $c->isReadable($path));
assertType('non-empty-string', $path);
}
}

function testStaticMethod(string $path): void
{
if (Checker::staticIsReadable($path)) {
assertType('true', Checker::staticIsReadable($path));
assertType('non-empty-string', $path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,17 @@ public function testBug8555(): void
$this->analyse([__DIR__ . '/data/bug-8555.php'], []);
}

public function testSelfContradiction(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/self-contradiction.php'], [
[
'Result of && is always false.',
25,
],
]);
}

#[RequiresPhp('>= 8.1.0')]
public function testBug14807(): void
{
Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/self-contradiction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace SelfContradiction;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Scalar;

class Foo {
/**
* @phpstan-assert-if-true Scalar|ClassConstFetch|ConstFetch $node
*/
private static function isSubjectNode(Expr $node): bool
{
return $node instanceof Scalar || $node instanceof ClassConstFetch || $node instanceof ConstFetch;
}

/**
* @return array{subject: Expr, value: Scalar|ClassConstFetch|ConstFetch}|null
*/
private function getSubjectAndValue(Identical $comparison): ?array
{
if (self::isSubjectNode($comparison->left) && !self::isSubjectNode($comparison->left)) {
return ['subject' => $comparison->right, 'value' => $comparison->left];
}

if (!self::isSubjectNode($comparison->left) && self::isSubjectNode($comparison->right)) {
return ['subject' => $comparison->left, 'value' => $comparison->right];
}

return null;
}
}
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