Skip to content

Conversation

@jeromegamez
Copy link

@jeromegamez jeromegamez commented Dec 31, 2025

Adds (or removes) parentheses around negated instanceof expressions:

# rector.php
use Rector\Instanceof_\Rector\BooleanNot\ParenthesizeNegatedInstanceofRector;

RectorConfig::configure()
    ->withRules([
        ParenthesizeNegatedInstanceofRector::class // defaults to `add_parentheses`
    ])
	// or
    ->withConfiguredRule(ParenthesizeNegatedInstanceofRector::class, [)
        'mode' => ParenthesizeNegatedInstanceofRector::ADD_PARENTHESES, // `add_parentheses`
    ])
	// or
    ->withConfiguredRule(ParenthesizeNegatedInstanceofRector::class, [)
        'mode' => ParenthesizeNegatedInstanceofRector::REMOVE_PARENTHESES, // `remove_parentheses`
    ])
;

this will either

# With `add_parentheses`
-!$foo instanceof Foo
+!($foo instanceof Foo)

# With `remove_parentheses`
-!($foo instanceof Foo)
+!$foo instanceof Foo

:octocat:

@jeromegamez jeromegamez force-pushed the add-parenthesize-negated-instanceof-rector branch from 883ced6 to ec52285 Compare December 31, 2025 01:32
@TomasVotruba
Copy link
Member

Hi, thanks for proposal. Rector area of focus is logic code structures.
As this rule brings syntactical improvement, it fits tools like php-cs-fixer. Contribute this rule there 👍

@jeromegamez
Copy link
Author

Thank you for looking into this, I hope I can use parts of it, this was a long debugging session (I first thought setting the attribute to false would be enough) 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants