This Question is Possibly Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
5 Replies Last post: Aug 3, 2013 2:40 AM by Andriy Bazanov  
Andrea Sciamanna Newbie 60 posts since
Sep 27, 2011
Currently Being Moderated

Aug 2, 2013 1:45 PM

Define common global variables types as a project setting

I usually add PHPDoc comments to global variables to remove all the code inspection warnings and to get intellisense, but a client a work for is complaining about them and asked me to remove all these comments, as "unneeded".

So I have no choice: I must remove them.

 

I'm working in a WordPress related project, so there is a set of commonly used global variables. For instance I often use these:

 

/** @var $wpdb WPDB */

global $wpdb;

/** var $pagenow string */

global $pagenow;

/** @var $wp_query WP_Query */
global $wp_query;

I wonder however if I there is in PhpStorm, in Project settings, or somewhere else, a place where I can define them "globally", so I can still get all the advantages of intellisense and no code inspection warnings.

Andriy Bazanov Master 4,193 posts since
May 28, 2010
Currently Being Moderated
Aug 2, 2013 2:02 PM in response to: Andrea Sciamanna
Re: Define common global variables types as a project setting

Hi there,

 

I wonder however if I there is in PhpStorm, in Project settings, or somewhere else, a place where I can define them "globally", so I can still get all the advantages of intellisense and no code inspection warnings.

No.

 

You may try this: create a separate .php file and place it ANYWHERE in the project (e.g. will be available on your computer only). There you declare all such variables with proper PHPDocs -- should work.

Andriy Bazanov Master 4,193 posts since
May 28, 2010
Currently Being Moderated
Aug 3, 2013 2:14 AM in response to: Andrea Sciamanna
Re: Define common global variables types as a project setting

I don't see how this could work.

 

Code inspection and intellisense works for the current file.

 

If I add them in a separate file it will work in this separate file, not in all the other files where I use these variables.

So .. did you tried what I have suggested ... or you just speculating?

 

 

Could you please share your knowledge and explain why this one should not work (while it does work)?

screen01.png

Andriy Bazanov Master 4,193 posts since
May 28, 2010
Currently Being Moderated
Aug 3, 2013 2:40 AM in response to: Andrea Sciamanna
Re: Define common global variables types as a project setting

I'm not not member of JB stuff. But I have realised my words shortly after I typed them and altered my post. My appologies.

 

In any case: I did a test before writing it .. and it DOES work here (both PhpStorm v6 and v7 EAP). The simple code example shown on screenshot (previous post) also shows that it works.

 

Here are actual screenshot from wordpress code (v3.6 -- downloaded today; no modifications).

screen01.png

 

screen02.png

(on 2nd pic I have changed the name of varible to show how it otherwise looks in the code when no proper PHPDoc comment applied).

 

 

Another example code, file 1.php

<?php

class Kitten
{
    /**
     * @param string $what Bla Bla ($kitten)
     */
    public function sayIt($what = '')
    {
        echo $what ? $what : "Meow!\n";
    }
}

 

/** @var Kitten $k */
global $k;

 

file 2.php

<?php
$k->sayIt('ok');

 

This example works just fine here as well (tested in v7 EAP build)

More Like This

  • Retrieving data ...