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

Fixer for changing array_push($a, $b) to $a[] = $b #3483

Closed
ntzm opened this issue Jan 29, 2018 · 1 comment
Closed

Fixer for changing array_push($a, $b) to $a[] = $b #3483

ntzm opened this issue Jan 29, 2018 · 1 comment

Comments

@ntzm
Copy link
Contributor

ntzm commented Jan 29, 2018

Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

Source: https://secure.php.net/manual/en/function.array-push.php

I think this fixer may be risky because there is a change in behaviour:

Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.

SpacePossum added a commit that referenced this issue Apr 6, 2020
This PR was merged into the 2.17-dev branch.

Discussion
----------

ArrayPushFixer - introduction

```
possum@aquarium:/home/possum/work/PHP-CS-Fixer$ php php-cs-fixer describe array_push
Description of array_push rule.
Converts simple usages of ``array_push($x, $y);`` to ``$x[] = $y;``.

Fixer applying this rule is risky.
Risky when the function `array_push` is overridden.

Fixing examples:
 * Example #1.
```
```diff
--- Original
+++ New
@@ -1,2 +1,2 @@
 <?php
-array_push($x, $y);
+$x[] = $y;
```

closes #3483

Commits
-------

4400ea5 ArrayPushFixer - introduction
@SpacePossum
Copy link
Contributor

added in #4790

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

No branches or pull requests

2 participants