Skip to content

Conversation

gharlan
Copy link
Member

@gharlan gharlan commented Jul 22, 2025

No description provided.

@coveralls
Copy link

coveralls commented Jul 22, 2025

Coverage Status

coverage: 94.76% (+0.001%) from 94.759%
when pulling a837f2e on gharlan:NullableTypeDeclarationFixer-instanceof-static
into d5efbeb on PHP-CS-Fixer:master.

@gharlan gharlan changed the title fix: NullableTypeDeclarationFixer - handle instanceof static fix: NullableTypeTransformer - handle instanceof static Jul 22, 2025
@gharlan gharlan changed the title fix: NullableTypeTransformer - handle instanceof static fix: NullableTypeTransformer - handle instanceof static Jul 22, 2025
@kubawerlos kubawerlos changed the title fix: NullableTypeTransformer - handle instanceof static fix: TernaryOperatorSpacesFixer - handle instanceof static Jul 22, 2025

if (
$tokens[$prevIndex]->isGivenKind(\T_STATIC)
&& $tokens[$tokens->getPrevMeaningfulToken($prevIndex)]->isGivenKind(\T_INSTANCEOF)
Copy link
Member

Choose a reason for hiding this comment

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

Any case that justifies this line (with T_INSTANCEOF?

If not, I'd say let's add T_STATIC to self::TYPES.

Copy link
Member Author

Choose a reason for hiding this comment

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

T_STATIC is already part of self::TYPES, but it needs extra handling because in the sequence static ? the ? is not always the "nullable type".

Copy link
Member Author

@gharlan gharlan Jul 22, 2025

Choose a reason for hiding this comment

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

So at the moment the ? token in $a instanceof static ? $b : $c is wrongly transformed into CT::T_NULLABLE_TYPE. This is fixed here.

@@ -82,6 +82,11 @@ function A(){}
echo $guard?1:2;}',
];

yield 'handle instanceof static' => [
'<?php $a instanceof static ? $b : $c;',
'<?php $a instanceof static?$b:$c;',
Copy link
Member Author

Choose a reason for hiding this comment

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

this wasn't fixed before.

@@ -41,6 +41,10 @@ public function testFix(string $expected, ?string $input = null): void
*/
public static function provideFixCases(): iterable
{
yield [
'<?php $a instanceof static ? \DateTime::class : $c;',
Copy link
Member Author

Choose a reason for hiding this comment

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

this was wrongly changed to:
<?php $a instanceof static ?\DateTime::class : $c;

Copy link
Member

@kubawerlos kubawerlos left a comment

Choose a reason for hiding this comment

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

Sorry, my bad. I got confused that ? in static ? is sometimes a nullable type as well.

@kubawerlos kubawerlos merged commit 69431f5 into PHP-CS-Fixer:master Jul 23, 2025
30 checks passed
@gharlan gharlan deleted the NullableTypeDeclarationFixer-instanceof-static branch August 24, 2025 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants