Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NoUnsetOnPropertyFixer #3737

Merged
merged 1 commit into from May 27, 2018

Conversation

BackEndTea
Copy link
Contributor

@BackEndTea BackEndTea commented May 9, 2018

Fixes #3695

This adds the fixer as described in #3695

@keradus
Copy link
Member

keradus commented May 9, 2018

Hi !
nice to see this feature started existence !

unsetting key of property is not the same as unsetting the property,
please add test that proves unset($this->foo[$bar]) is not touch

'<?php unset($this->bar, self::$foo, $bar);',
],
'It does not replace unsets on arrays' => [
'<?php unset($bar->foo[0]);',
Copy link
Member

Choose a reason for hiding this comment

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

add '<?php unset($bar->foo{0});', as well.
all hail php...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I never knew you could do that, and now i wish i didn't know.

Copy link
Contributor

Choose a reason for hiding this comment

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

One thing about writing fixers is you come across a lot of syntax you wish didn't exist 😆

Copy link
Member

Choose a reason for hiding this comment

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

@BackEndTea , don't you worry. simply start using normalize_index_brace and forgot about that syntax in your life ;)
(well, just not in this PR ;P thanks for adding a test 👍)

Copy link
Contributor

Choose a reason for hiding this comment

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

you can also mix those up, so please add the test for <?php unset($bar->foo{0}[1]{2}{$a}[$b]);

*/
public function getPriority()
{
// should be run before CombineConsecutiveUnsetsFixer.
Copy link
Member

Choose a reason for hiding this comment

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

btw,
if it's not a sentence, why final dot is there?

@BackEndTea BackEndTea force-pushed the feature/no-unset-properties branch from a9745a6 to f21f318 Compare May 11, 2018 15:35
@BackEndTea
Copy link
Contributor Author

I updated doc-blocks, and made sure comments stay preserved.

One edge case that i dont know how to fix is the following:

- unset/*foo*/($a, $this->a);
+ unset(/*foo*/$a); $this->a = null;

If there is a comment between the unset and the ( in a combined unset, where at least one of the unsets needs to be changed to is null, that comment will be moved to after the (

@SpacePossum
Copy link
Contributor

the move of a bad placed comment to another not so perfect location is not an issue IMHO, as long as we preserve the comment it is up to the end user to fix the mess ;)

@BackEndTea BackEndTea force-pushed the feature/no-unset-properties branch from 8911a36 to c59ecad Compare May 11, 2018 15:48
@BackEndTea BackEndTea changed the title [WIP] Add no_unset_on_property rule Add no_unset_on_property rule May 11, 2018
@BackEndTea
Copy link
Contributor Author

The Cirlce CI failure looks unrelated to my PR, and i think this fixer is ready.

@keradus
Copy link
Member

keradus commented May 14, 2018

while definitely good rule, it is changing code behaviour if one is iterating over instance properties

https://3v4l.org/kiqvB

please mark fixer as risky with damn good description when it's risky.

@@ -1002,6 +1002,10 @@ Choose from the list of available rules:

*Risky rule: modifies the signature of functions; therefore risky when using systems (such as some Symfony components) that rely on those (for example through reflection).*

* **no_unset_on_property**
Copy link
Member

Choose a reason for hiding this comment

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

please add this rule to local .php_cs.dist file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

public function getDefinition()
{
return new FixerDefinition(
'Properties should be set to null instead of using `unset`.',
Copy link
Contributor

Choose a reason for hiding this comment

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

please update to `null`


return array_merge(
[
$filteredTokens[0]->isWhitespace() ? new Token([T_WHITESPACE, ' ']) : new Token(''),
Copy link
Contributor

Choose a reason for hiding this comment

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

can we avoid adding an empty token to this?

@keradus keradus changed the title Add no_unset_on_property rule Add NoUnsetOnPropertyFixer May 27, 2018
@keradus keradus added the RTM Ready To Merge label May 27, 2018
@keradus keradus modified the milestones: 2.11.2, 2.12.0 May 27, 2018
@keradus keradus removed the RTM Ready To Merge label May 27, 2018
@keradus keradus force-pushed the feature/no-unset-properties branch from 90ad623 to 3f685f1 Compare May 27, 2018 22:03
@keradus
Copy link
Member

keradus commented May 27, 2018

Thank you @BackEndTea.

@keradus keradus merged commit 3f685f1 into PHP-CS-Fixer:master May 27, 2018
keradus added a commit that referenced this pull request May 27, 2018
This PR was squashed before being merged into the 2.12-dev branch (closes #3737).

Discussion
----------

Add NoUnsetOnPropertyFixer

Fixes #3695

This adds the fixer as described in #3695

Commits
-------

3f685f1 Add NoUnsetOnPropertyFixer
@BackEndTea
Copy link
Contributor Author

Thank you @keradus @SpacePossum

@BackEndTea BackEndTea deleted the feature/no-unset-properties branch May 28, 2018 06:23
@SpacePossum
Copy link
Contributor

thank you @BackEndTea , you did all the work here :D great to see this in!

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

Successfully merging this pull request may close these issues.

None yet

4 participants