Php-cs-fixer: NoUnsetOnPropertyFixer breaks on function call as key in array class-property

Created on 28 Jun 2018  路  8Comments  路  Source: FriendsOfPHP/PHP-CS-Fixer

The PHP version you are using ($ php -v):

PHP 7.1.18 (cli) (built: May 25 2018 19:18:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.18, Copyright (c) 1999-2018, by Zend Technologies

PHP CS Fixer version you are using ($ php-cs-fixer -V):

PHP CS Fixer 2.12.1 Long Journey by Fabien Potencier and Dariusz Ruminski

The command you use to run PHP CS Fixer:

vendor/bin/php-cs-fixer fix --rules=no_unset_on_property filename.php

If applicable, please provide minimum samples of PHP code (as plain text, not screenshots):

  • before running PHP CS Fixer (no changes):
unset($this->profile->getSettings()[Settings::RANDOM_SETTING]);
  • with unexpected changes applied when running PHP CS Fixer:
    Linting error after fixing

  • with the changes you expected instead:
    None, it shouldn't have changed it.

The getSettings() call returns a magic subclass of ArrayObject, however the fixer thinks it's a property. I know it's ugly syntax but it's used all over the place because $this->profile->settings was changed to protected, and all occurrences were replaced with the getter (because lazy loading).

kinbug

Most helpful comment

I'll take a look either later today or tomorrow

All 8 comments

@SpacePossum , can you take a look ?

I might but let me ping the author of the fixer as well @BackEndTea ;)

I'll take a look either later today or tomorrow

I created a test case for

unset($this->profile->getSettings()[Settings::RANDOM_SETTING]);

But it does not get changed by the fixer. Is there any other place in the file that uses an unset which might cause thsi bug?

Before reporting the issue I tried disabling this fixer, and there is no problem. Re-enabling the fixer and commenting out this line, and again no problem.

It might conflict with other fixers or its surrounding code, which I will be able to test again tomorrow.

So yeah... different line now, don't know if inconsistent or just my bad 馃槰
Validated with -vvv --diff --using-cache=no in addition to the original command

It changes

unset($this->property[array_search(\Types::TYPE_RANDOM, $this->property)]);

To

unset($this->property[array_search(\Types::TYPE_RANDOM); $this->property)] = null;

Ah i think i know what causes that,and how to fix it

Sorry for the delay on this, but i haven't had much time to work on it, and i'm kinda unsure on how to fix it atm.

So if someone wants to take a look, feel free to do so.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aidantwoods picture aidantwoods  路  3Comments

BackEndTea picture BackEndTea  路  3Comments

datenmeister picture datenmeister  路  3Comments

amitbisht511 picture amitbisht511  路  3Comments

OskarStark picture OskarStark  路  3Comments