Skip to content
Draft
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
5 changes: 5 additions & 0 deletions issue-bot/src/Console/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
set_exception_handler(static function (\Throwable $e): void {
fwrite(STDERR, 'Swallowed by global exception handler: ' . $e->getMessage() . "\n");
});

$phpVersion = (int) $input->getArgument('phpVersion');
$commaSeparatedPlaygroundHashes = $input->getArgument('playgroundHashes');
$playgroundHashes = explode(',', $commaSeparatedPlaygroundHashes);
Expand Down Expand Up @@ -223,6 +227,7 @@ private function analyseHash(LoopInterface $loop, OutputInterface $output, int $
'analyse',
'--error-format',
'json',
'-vvv',
'--no-progress',
'-c',
escapeshellarg($neonPath),
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
6 changes: 6 additions & 0 deletions src/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$errorFormatter = $container->getService($errorFormatterServiceName);

if (count($internalErrorsTuples) > 0) {
foreach ($internalErrorsTuples as [$internalError]) {
$errorOutput->writeLineFormatted($internalError->getMessage());
$errorOutput->writeLineFormatted('');
}

$analysisResult = new AnalysisResult(
array_values($internalFileSpecificErrors),
array_map(static fn (InternalError $internalError) => $internalError->getMessage(), $internalErrors),
Expand All @@ -500,6 +505,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput(), $analysisResult->getProcessedFiles());

$errorOutput->writeLineFormatted('internal errors '. implode("\n", array_map(static fn (InternalError $internalError) => $internalError->getMessage(), $internalErrors)));
$errorOutput->writeLineFormatted('⚠️ Result is incomplete because of severe errors. ⚠️');
$errorOutput->writeLineFormatted(' Fix these errors first and then re-run PHPStan');
$errorOutput->writeLineFormatted(' to get all reported errors.');
Expand Down
33 changes: 33 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14829.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@

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
Expand All @@ -17,3 +34,19 @@ function testFunction(string $path): void
}
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,22 @@ 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,
],
[
'Result of && is always false.',
51,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.'
],
]);
}

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

class Bar {
/**
* @return ($node is Scalar|ClassConstFetch|ConstFetch ? true : false)
*/
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;
}
}
Loading