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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbidden usage of extract #48

Merged
merged 1 commit into from
Mar 29, 2018
Merged

Forbidden usage of extract #48

merged 1 commit into from
Mar 29, 2018

Conversation

carusogabriel
Copy link
Contributor

@carusogabriel carusogabriel commented Mar 28, 2018

I'd like to propose the forbidden usage of the extract function. list should be used instead.

Some reasons are:

  • PHPStan does not recognize the extracted variables
  • Sometimes we don't want to whole array's indexes, just some of them
  • Control of the extract variables names and values

When #39 gets merged, and this one gets approved, list will turn into [] 馃憤

<?php

$bar = [
    'foo' => 1,
    'bar' => 2,
    'baz' => 3,
];
-extract($bar);
+list($fooValue, $barValue, $bazValue) = $bar;

alcaeus
alcaeus previously approved these changes Mar 29, 2018
@@ -47,6 +47,7 @@
close => closedir,
delete => unset,
doubleval => floatval,
extract => list,
Copy link
Contributor

Choose a reason for hiding this comment

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

This should not suggest a replacement here, list is not a function (also we use [...] instead of list(...)) so replacement suggestion is not correct imho.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, as https://php.net/list says:

Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation.

Thanks, gonna remove it 馃憤

Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

Looking good! Can we also ban compact() (separate PR)?

@mikeSimonson mikeSimonson self-requested a review March 29, 2018 12:30
@Majkl578 Majkl578 merged commit f31e869 into master Mar 29, 2018
@Majkl578 Majkl578 deleted the extract branch March 29, 2018 14:02
@Majkl578 Majkl578 assigned Majkl578 and unassigned carusogabriel Apr 1, 2018
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

7 participants