Skip to content

Add ParenthesizeNegatedInstanceofRector - #7785

Closed
jeromegamez wants to merge 1 commit into
rectorphp:mainfrom
jeromegamez:add-parenthesize-negated-instanceof-rector
Closed

Add ParenthesizeNegatedInstanceofRector#7785
jeromegamez wants to merge 1 commit into
rectorphp:mainfrom
jeromegamez:add-parenthesize-negated-instanceof-rector

Conversation

@jeromegamez

@jeromegamez jeromegamez commented Dec 31, 2025

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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) 😅

@jeromegamez

Copy link
Copy Markdown
Author

FYI, here's the follow up: PHP-CS-Fixer/PHP-CS-Fixer#9326

@jeromegamez
jeromegamez deleted the add-parenthesize-negated-instanceof-rector branch January 2, 2026 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants