Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpStorm not showing code hints inside array_map

I get no code hints for this code:

enter image description here

The PHPDoc for get_indexes I think is done properly and NetBeans seems to understand it and show hints properly:

/**
 *  Get Index
 * 
 * @global object $wpdb
 * @param String $extension_table_name
 * @return \ZRDN\Recipe[]
 */
public static function get_indexes($extension_table_name) {
    global $wpdb;
    $db_name = $wpdb->prefix . $extension_table_name;
    $selectStatement = "SELECT * FROM `{$db_name}`";
    $recipe_indexes = $wpdb->get_results($selectStatement);

    return $recipe_indexes;
}

Recipe is defined in the same file under same namespace:

class Recipe {
   /**
    * @var int
    */
   public $recipe_id;

   /**
    * @var int
    */
   public $post_id;
...

Any ideas what the issue might be?

like image 423
Gezim Avatar asked Feb 14 '26 03:02

Gezim


1 Answers

If you know that $recipes always contains objects of type Recipe then use it as the type of the $recipe argument of the map function:

$post_ids = array_map(function(Recipe $recipe) {
    return $recipe->recipe_id;
}, $recipes);

This way PhpStorm (and other IDEs) can help you with the autocomplete and also the PHP interpreter triggers a fatal error when it encounters a value in $recipes of the wrong type.

like image 191
axiac Avatar answered Feb 16 '26 17:02

axiac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!