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
8 changes: 7 additions & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1939,9 +1939,15 @@ private function processBooleanSureConditionalTypes(Scope $scope, SpecifiedTypes
continue;
}

$scopeType = $scope->getType($expr);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we need a similar fix in processBooleanNotSureConditionalTypes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe, but I didn't yet succeed finding a non regression test for processBooleanNotSureConditionalTypes, so I thought about letting this to another PR.

processBooleanNotSureConditionalTypes use intersect rather than remove so it's kinda different.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I do agree your example is a bug but the similar changes in processBooleanNotSureConditionalTypes

$scopeType = $scope->getType($expr);
			$conditionType = TypeCombinator::intersect($scopeType, $type);
			if ($scopeType->equals($conditionType)) {
				continue;
			}

			$conditionExpressionTypes[$exprString] = ExpressionTypeHolder::createYes(
				$expr,
				$conditionType,
			);

Does not solve this issue.

I would prefer open an issue about it an run the bot to find a dedicated fix.

This Pr will unlock #5386 which closes ~7 issues

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ok - opened a new bug phpstan/phpstan#14455

$conditionType = TypeCombinator::remove($scopeType, $type);
if ($scopeType->equals($conditionType)) {
continue;
}

$conditionExpressionTypes[$exprString] = ExpressionTypeHolder::createYes(
$expr,
TypeCombinator::remove($scope->getType($expr), $type),
$conditionType,
);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/PHPStan/Analyser/nsrt/pr-5445.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
Copy link
Copy Markdown
Contributor

@staabm staabm Apr 12, 2026

Choose a reason for hiding this comment

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

I think you should log a regular bug for this example and use the bug number as file-name.

having a test file named type-specifier might come in our way with autocompletion or other IDE features when searching for the TypeSpecifier class while doing regular development on the codebase

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I renamed it Pr5445, there is no need to open a issue when it's already described in the pr


namespace BugPr5445;

use function PHPStan\Testing\assertType;

/**
* @param array<string, mixed> $aggregation
* @param non-falsy-string $type
*/
function testTriviallyTrueConditionSkipped(array $aggregation, string $type): void
{
if (empty($aggregation['field']) && $type !== 'filter') {
return;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would add 2 more assertions directly after the IF

	assertType("array<string, mixed>", $aggregation);
	assertType('non-falsy-string', $type);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added


assertType("array<string, mixed>", $aggregation);
assertType('non-falsy-string', $type);

if ($type !== 'filter') {
assertType("array<string, mixed>", $aggregation);
}

assertType('non-falsy-string', $type);
}
Loading