Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpStorm: extract() identifying variables

Does anyone know whether there is a setting in PhpStorm that can trigger identifying variables generated using extract() function?

Example would be something like the following:

/**
 * @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 that's been extracted in the test is highlighted (unrecognised), but perhaps there is a setting that can change this behaviour?

like image 718
Sebastian Sulinski Avatar asked Dec 04 '16 10:12

Sebastian Sulinski


1 Answers

The solution that LazyOne offered actually works. However there is a bit more context you need in order to implement it.

To accurately inform PHPSTORM about the variables you want to declare the comment must be placed directly above extract() and 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 ( couldn't copy yours for some reason ).

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

Bonus: if you intend to use extract, I highly recommend you use an isset to ensure the array you're parsing contains the fields you are expecting. example in code above

like image 105
Cody Wikman Avatar answered Oct 03 '22 01:10

Cody Wikman