PhpStorm: extract () identifying variables

Does anyone know if there is a parameter in PhpStorm that can trigger the identification of variables generated using the extract() function?

An example might look something like this:

 /** * @return array */ protected function orderSet() : array { //... return [ 'colour' => $colour, 'green' => $green, 'orange' => $orange ]; } /** * @test */ public function returns_correct_attribute_names() { $params = $this->orderSet(); extract($params); $this->assertEquals( 'Colour', $colour->name ); } 

At the moment, any variable extracted in the test is highlighted (unrecognized), but maybe there is a parameter that can change this behavior?

+5
source share
1 answer

The solution offered by LazyOne really works. However, its implementation requires a little more context.

To accurately inform PHPSTORM about the variables you want to declare, a comment should be placed directly above extract () , not the parent function.

  public function db(){ $db = new SQLite3('db/mysqlitedb.db'); $payments = $db->query('SELECT * FROM payments'); while ($pay = $payments->fetchArray()){ /** * @var string $to_user * @var string $from_user * @var number $amount */ extract($pay); if (isset($to_user, $from_user, $amount)) echo "TO: {$to_user}| FROM: {$from_user}| $ {$amount} \n"; }; } 

This is a working sample from my code (for some reason, it did not copy your data).

You can see immediately before I use the extract () function, which I declare hidden variables and data types in the comment block above it.

Bonus: if you intend to use extraction, I highly recommend that you use isset to make sure that the array you are processing contains the fields that you expect. example in the code above

+6
source

Source: https://habr.com/ru/post/1260778/


All Articles