This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 146248 - need a way to 'hint' var type to the code completion
Summary: need a way to 'hint' var type to the code completion
Status: RESOLVED FIXED
Alias: None
Product: php
Classification: Unclassified
Component: Editor (show other bugs)
Version: 6.x
Hardware: All All
: P2 blocker with 20 votes (vote)
Assignee: Ondrej Brejla
URL:
Keywords:
: 147023 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-09-04 22:02 UTC by jdrukman
Modified: 2012-11-30 12:34 UTC (History)
9 users (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description jdrukman 2008-09-04 22:02:19 UTC
in php, it is very common to use the singleton pattern:

function get_singleton($class) {
  // returns singleton object of type '$class'
}

$user = get_singleton('user'); // $user is now of class user
$photo = get_singleton('photo'); // $photo is now of class photo

at this point, code completion is useless for $user and $photo.  you'll either get completions for the class of the
get_singleton method (if it has one) or you'll get "No suggestions."  There needs to be a mechanism whereby one can tell
the editor that $user is class x, $photo is class y, and so on.  in other IDE's (eg: komodo, phped) you can use the @var
phpdoc construct to hint the class of the variable.  I don't particular care how netbeans implements it, just that it
does get implemented.  Thanks.
Comment 1 Tomasz Slota 2008-09-10 10:11:00 UTC
We already support hinting the type with PHPDoc @return and @var directives, see issue 141652  and issue 145692.
Comment 2 jdrukman 2008-09-10 18:16:52 UTC
@return only works for functions that return a single type of object.

@var only works for instance variables.

i require a mechanism that works for functions that can return many different types of objects.  or more precisely, i 
need a way to specify that ANY variable (not just a class instance variable) is of type x.

example:
/**
 * @var user
 */
$x = get_object_of_type('user');
$x->  // now netbeans knows that $x is a user object

this only works for class instance variables currently.  it needs to be extended to work for any variable.  yes i know 
that the 'official' spec for @var says it only works for class instance variables.
Comment 3 Tomasz Slota 2008-09-12 08:34:24 UTC
*** Issue 147023 has been marked as a duplicate of this issue. ***
Comment 4 tprochazka 2008-09-13 13:20:59 UTC
Eclipse PDT use this for hint var type:

/* @var $a PageLayout */
$a-> (will be used class PageLayout)

Netbeans may use different/better hint declaration.
But I think, that will be better support also this, for compatibility in team which use Eclipse PDT.

Problem is also return value with array of some objects. For example:

/**
 * @return array of User
 */
public function getUsers() {
  ...
}

Next I want iterate it:

foreach($users as $user) {
 $user->
}

Will be great if Netbeans be able to detect '@return array of User' and use User class for autocomplete in $user 
variable.
Comment 5 tprochazka 2008-10-15 16:01:48 UTC
Some relevant information:
http://www.alexatnet.com/node/179
Comment 6 chsnyder 2008-11-13 14:07:18 UTC
This issue also applies to objects in included files, where the object is instantiated in the parent. This is common in
MVC setups where a controller instantiates a number of global objects, which are then manipulated by included action
files and rendered in included template files.

Having used the Zend "@var $variable type" hack for many years, I have two suggestions:

1) It would be *ideal* if you could declare a project-wide hint-sheet for global variables, so that anytime NetBeans
sees $foo in a project file, it can assume that that is an instance of class myFoo.

2) Rather than using the Zend @var hack, I'd suggest creating an @type keyword, with the syntax @type type $variable. 

Based on #2, I'm hoping to just include something that looks like this at the top of my files:

/**
 * Global objects used in this action:
 *
 * @type fcnyObject $object
 * @type fcnyAuthority $authority
 * @type fcnyCollection $collection
 * ...etc
 */
Comment 7 tprochazka 2008-11-13 18:52:30 UTC
2chsnyder: Yes it's maybe usefull, but I prefer use static methods for accessing global related object, like 
Core::getAuth() or Auth::getInstance(). I completly doesn't use something like global variables. Problem is, that @type 
is unknown for phpDoc specification. Zend hack is simple comment, it doesn't related to phpDoc, only syntax is similar.
Comment 8 rmm 2008-11-27 13:28:43 UTC
I'would also vote for the @type keyword!
Comment 9 sandange 2008-11-27 23:33:44 UTC
//index.php ------------------------------------------------------

/**
 *  @var Zend_Db_Adapter_Pdo_Pgsql
 */
$db = Zend_Db::factory($config->database);

//$db is viewed just as a Zend_Db_Adapter_Abstract

Zend_Db_Table_Abstract::setDefaultAdapter($db);
Zend_Registry::set('db', $db);

//index.phtml ------------------------------------------------------

/** @var Zend_View $this */
$this->headTitle('My page title');
$this->placeholder('title')->set('Just a title');

//$this <- no suggestions

/**
 * @var Zend_Db_Adapter_Pdo_Pgsql
 */
$db = Zend_Registry::get('db');
//$db <- no suggestions

print_r($db->describeTable('sessions'));

Solving this issue is really needed, since using a framework, may get to making code completion useless as it is now.
Comment 10 tprochazka 2008-11-28 13:27:42 UTC
I think, that Zend_Registry::set('db', $db); is totally bad idea, but suppor for /* @var ... */ solve this.

I must correct my suggestion Sat Sep 13. I wrote '@return array of User' as hint for array result. But much better is 
@return User[] for hint array type. This syntax use also official PHP manuall.


Comment 11 maghiel 2009-12-11 22:28:25 UTC
Placing a database instance in a registry is a bad idea, but that doens't mean the registry pattern is bad ;) So for the good situations hinting would definatly be useful.

And as stated above, it would be really welcome for views. It's one of my biggest irritations not having completion in views!

The 'Zend hack' idea sounds the best to me as it doenst involve using invalid phpdoc, it's 'just' comment :)
Comment 12 Tomasz Slota 2010-02-10 08:44:53 UTC
reassigning to default owner
Comment 13 Loic_Etibe.com 2010-02-15 09:12:14 UTC
What's up with this issue ?

I personally wish I could define some variables types project-wide. At the moment, I've got some trouble using previously instanced objects (header file) and that's quite boring to write /** @var */ into each new file using them ...
Comment 14 kRk 2010-07-26 10:12:09 UTC
Hello,

any changes here?
Comment 15 doldev 2010-09-07 17:21:46 UTC
(In reply to comment #14)
> Hello,
> 
> any changes here?

There is already a support for this format:

/* @var $config Zend_Config */

Don't mix them up with /** .. */ comments.
Comment 16 kRk 2010-09-08 08:07:58 UTC
(In reply to comment #15)
> There is already a support for this format:
> 
> /* @var $config Zend_Config */

Yes, i know, but this dont work with variables declared in included files, ie:

-- main.php --

/* @var $variable ClassName */
$variable = (...);
$variable-> // ok, suggestions work 

-- file.php --
include('main.php');
$variable->  // no suggestions
Comment 17 SilentSierra 2010-09-14 01:27:09 UTC
Same problem, I would like to document type of array to iterate later.

Thanks.

(In reply to comment #4)
> Eclipse PDT use this for hint var type:
> 
> /* @var $a PageLayout */
> $a-> (will be used class PageLayout)
> 
> Netbeans may use different/better hint declaration.
> But I think, that will be better support also this, for compatibility in team
> which use Eclipse PDT.
> 
> Problem is also return value with array of some objects. For example:
> 
> /**
>  * @return array of User
>  */
> public function getUsers() {
>   ...
> }
> 
> Next I want iterate it:
> 
> foreach($users as $user) {
>  $user->
> }
> 
> Will be great if Netbeans be able to detect '@return array of User' and use
> User class for autocomplete in $user 
> variable.
Comment 18 Filip Zamboj 2010-09-15 12:26:28 UTC
batch reassigning
Comment 19 Sc0tTyXL 2011-03-14 22:55:49 UTC
(In reply to comment #17)
> Same problem, I would like to document type of array to iterate later.
> 
> Thanks.
> 
> (In reply to comment #4)
> > Eclipse PDT use this for hint var type:
> > 
> > /* @var $a PageLayout */
> > $a-> (will be used class PageLayout)
> > 
> > Netbeans may use different/better hint declaration.
> > But I think, that will be better support also this, for compatibility in team
> > which use Eclipse PDT.
> > 
> > Problem is also return value with array of some objects. For example:
> > 
> > /**
> >  * @return array of User
> >  */
> > public function getUsers() {
> >   ...
> > }
> > 
> > Next I want iterate it:
> > 
> > foreach($users as $user) {
> >  $user->
> > }
> > 
> > Will be great if Netbeans be able to detect '@return array of User' and use
> > User class for autocomplete in $user 
> > variable.


This is bascily the only thing i use this for because phpdoc doesn't support arrays as of yet:

foreach($users as /* @var $user User */ $user) {
  $user->
}
( Untested in RC1 but worked in the last nightly i used )
Comment 20 Ondrej Brejla 2012-02-08 14:25:16 UTC
Works for me in 7.2 dev.
Comment 21 everflux 2012-06-10 13:09:57 UTC
It does not work for me when trying to hint for a method input parameter.
Example:

/*
 * @var $criteria Topic
 */
function lockTopic($criteria)
{
 $criteria->

No completion here.

Using this syntax it works, but is a little ugly

function lockTopic($con, $criteria)
{
    /* @var $criteria Topic */
   $criteria->[suggestions]


What does work is this syntax:

/**
 * @param Topic $criteria
 */
function lockTopic($con, $criteria)
{
    $criteria->[suggestions]

I am not sure there is sufficient documentation for the various types of hinting.
Comment 22 frosty22 2012-06-10 13:50:14 UTC
(In reply to comment #21)
> It does not work for me when trying to hint for a method input parameter.
> Example:
> 
> /*
>  * @var $criteria Topic
>  */
> function lockTopic($criteria)
> {
>  $criteria->
> 
> No completion here.
> 
> Using this syntax it works, but is a little ugly
> 
> function lockTopic($con, $criteria)
> {
>     /* @var $criteria Topic */
>    $criteria->[suggestions]
> 
> 
> What does work is this syntax:
> 
> /**
>  * @param Topic $criteria
>  */
> function lockTopic($con, $criteria)
> {
>     $criteria->[suggestions]
> 
> I am not sure there is sufficient documentation for the various types of
> hinting.


@param Topic $criteria
-> this is correct syntax of phpdoc:
http://www.phpdoc.org/docs/latest/for-users/tags/param.html
Comment 23 chsnyder 2012-06-11 20:33:30 UTC
(In reply to comment #21)
> It does not work for me when trying to hint for a method input parameter.

The @var syntax is special for type hinting global variables. This is necessary if you declare variables or instantiate objects in another file that include()s the current one, or in an auto-prepend script.

To provide type hints for function parameters, use @param.
Comment 24 amit147 2012-11-30 12:20:16 UTC
How about class variables?

This does not work:

/* @var $service \Reference\Service */
private $service;

nor this:

/* @var $this->service \Reference\Service */
private $service;

nor this (somewerhe in the class):
...
/* @var $this->service \Reference\Service */
$service-> [no suggestions]

nor this:
...
/* @var $service \Reference\Service */
$service-> [no suggestions]
Comment 25 amit147 2012-11-30 12:23:35 UTC
(In reply to comment #24)
> How about class variables?
> 
> This does not work:
> 
> /* @var $service \Reference\Service */
> private $service;
> 
> nor this:
> 
> /* @var $this->service \Reference\Service */
> private $service;
> 

Sorry typo, forgot to put $this, should be like this, problem is the same.
> nor this (somewerhe in the class):
> ...
> /* @var $this->service \Reference\Service */
> $this->service-> [no suggestions]
> 
> nor this:
> ...
> /* @var $service \Reference\Service */
> $this->service-> [no suggestions]
Comment 26 Ondrej Brejla 2012-11-30 12:34:51 UTC
Class properties are handled by "@property" PHPDocs. Use them. http://manual.phpdoc.org/HTMLSmartyConverter/PHP/phpDocumentor/tutorial_tags.property.pkg.html