Defining a variable type in comment

Yesterday was the first day at work this year and we committed new feature, which allow to define a type for a variable in a comment. The comment has to be defined in specific form as is displayed on the picture.

The comment has to be /\* @var $variable type \*/ . If the comment is written in the right form, then the var tag has bold font.

You can use this helper, when NetBeans is not able to recognize the type of the variable. On the picture below you can see that NetBeans recognizes that the variable $media in the echo statement is Book type. But then another object is assigned to the $media variable through the getLastMovie() function. This function defines the return type Movie, so from this line NetBeans knows that $media variable contains an object of Movie type. 

NetBeans handles the variable name and the type from the comment helper as are handled in PHP Doc. So for example mark occurrences works as usual.

Next time I will show you how NetBeans can help you with writing the comment helper.

Comments:

Hello,

great new feature :-)
But I found a bug:
When I use this Code everything is OK:

/\* @var $l_value Image \*/
foreach ( $imageList as $l_value )
{
$l_value->...
}

But with this Code, I get an Exception:
foreach ( $imageList as $l_value ) /\* @var $l_value Image \*/
{
$l_value->...
}

Greetings,
Stefan Sturm

Posted by Stefan Sturm on January 06, 2009 at 02:58 AM CET #

To Stefan: thanks for finding this problem. I'm going to fix it today.

Posted by Petr on January 06, 2009 at 03:52 AM CET #

Good one. It was implemented first in Zend Studio 6 if I remember correctly. Helped me a lot when I was using ZSE.

Posted by Sam on January 06, 2009 at 04:39 AM CET #

Great feature, but how about arrays of object? How to make Netbeans to complete this?:

$a = array();
$a[] = new Movie();
$a[] = new Movie();

$a[0]-> [code competition here]

foreach ($a as $element) {
$element-> [code competition here]
}

Posted by Leszczu on January 06, 2009 at 04:39 AM CET #

The problem, which was mentioned by Stefan is now fixed.

Posted by Petr on January 06, 2009 at 11:49 AM CET #

Code completion doesn't seem to work in the following situation.

$something->anotherthing()->|

even tho I have PHPDoc'ed the function anotherthing to return a different object type, I still get the codecompletion of the something object.

Posted by Jacob on January 06, 2009 at 05:59 PM CET #

This is the most important feature I was waiting for.
Thanks a lot.

Posted by Omar on January 07, 2009 at 03:09 AM CET #

To Jacob: strange it works for me. At least what I tried:

<?php

class Issue {
public function getDescription() {
}
}

class Bug {
function getDefaultPriority() {
return 3;
}

/\*\*
\* @return Bug
\*/
public static function getLastBug() {
}

/\*\*
\* @return Issue
\*/
public function depends() {

}
}

/\*\*
\* @return Bug
\*/
function getFirstBug() {
}

/\* @var $something DOMAttr \*/
$something->appendChild();

$something = Bug::getLastBug();
$something->depends()->getDescription();

?>

Could you try this example? Let me know, whether it work for you. If yes, then we should investigate more, why it doesn't work in your case.

Thanks,
Petr

Posted by Petr on January 07, 2009 at 04:21 AM CET #

Regarding Leszczu's question.

It would be nice to support something like:

/\*\*
\* @var array[Bar]
\*/
$foo = array(new Bar, new Bar);

Posted by Amenthes on January 07, 2009 at 02:46 PM CET #

This is the most important feature I was waiting for. Thanks a lot.

Now is Netbeans practically better than commercial Zend Studio!!!

I miss only Zend Toolbar feature. It's very practical for run debugger from any state of application. How I can in Netbeans log to web application, make same operation and then run debugger after form submit?

Posted by Tomas Prochazka on January 07, 2009 at 11:01 PM CET #

I'm not sure what I am doing wrong, but it is not working for me. I use lazy loading. Could that be it?

Posted by Keith Davis on January 08, 2009 at 11:53 AM CET #

Hi Keith,

it's not connected with lazy loading. Which build do you use?

Posted by Petr on January 08, 2009 at 01:47 PM CET #

Fantastic!! I have been waiting for this for so long.

Posted by jsd on January 08, 2009 at 06:40 PM CET #

Just wondering how you get the build in which this has been enabled? I'm using Netbeans 6.5 on Windows Vista.

Posted by planetthoughtful on January 08, 2009 at 11:41 PM CET #

to planetthoughtful: download development version here: http://bits.netbeans.org/download/trunk/nightly/latest/

Posted by gawan on January 09, 2009 at 02:00 AM CET #

Thanks for your effort - lack of this feature often makes me think of getting back to Eclipse.
Doesn't work for me with member vars though:

class a
{
function x() {}
}

class b
{
/\* @var member a; \*/
var $var_of_type_a;

function x()
{
$this->var_of_type_a-> // No suggestions
}
}

Posted by infozone on January 10, 2009 at 09:29 AM CET #

Sorry for typo:
/\* @var member a; \*/ should had been
/\* @var var_of_type_a a \*/ of course.
Doesn't work anyway. For what it worth, tested with 200901091401 nightly build.

Posted by infozone on January 10, 2009 at 09:39 AM CET #

To infozone:

There is PHP doc definition for classfield:
http://manual.phpdoc.org/HTMLframesConverter/default/phpDocumentor/tutorial_tags.var.pkg.html

As you can see, it's used the @var field, but the form is:

/\*\* @var type \*/

So for class fields use the PHP doc.

Regards,
Petr

Posted by Petr on January 10, 2009 at 03:39 PM CET #

Oh, ok :) Thanks Petr, you're indeed correct. Then, wouldn't it be too difficult to make vdoc distinct whether it is used on a class member or a standalone var and apply corresponding syntax automatically?
Or better yet, make auto documentor (or whatever you call it) work similarly? I mean, if you type:

/\*\*<enter>

above class var declaration, it results in

/\*\*
\* @var <type>
\*/

with cursor located on the right of <type> placeholder, so that you need to erase it manually to indicate a type - very inconvenient. Should it select the placeholder and allow to type in an appropriate type over it, it would be much better. Actually even just removal of the <type> placeholder would be good in this case - although this doesn't solve similar issue with methods params, where you are getting something like:

/\*\*
\*
\* @param <type> $a
\* @param <type> $b
\*/

with cursor located after $b, and then cannot even select <type> with just double-click, as it selects only word "type" but not the angle braces around it, so they require additional typing to be deleted.

Thanks again for your effort.

Posted by infozone on January 11, 2009 at 11:32 AM CET #

Hi Petr, nice feature, but I found a bug. Try this example:

<?
/\*\*
\* @property string $title
\* @property string $body
\*/
class Blog {}

/\*\*
\* @property string $name of user
\* @property Blog $blog
\*/
class User {}

/\* @var $user User \*/
$user->blog->

?>

and after CTRL + SPACE netbeans offers me property of User not of Blog. I already added it into issuezilla: http://www.netbeans.org/issues/show_bug.cgi?id=156614

Little enhancement: Could you add text after @property into PHPDoc popup window (e.g. for property $name -> "$name of user") ?

Posted by gawan on January 12, 2009 at 06:54 AM CET #

To infozone:
I will think about your suggestion. You are right, that the current approach is not consistent.

To gowan:
Thanks for entering the issue. I have put a comment there.

Posted by Petr on January 12, 2009 at 05:32 PM CET #

Great!
The PHP plugin for netbeans improves every day helping our work!

Thanks!

Posted by Joao Neto on February 25, 2009 at 07:13 AM CET #

This is a fantastic addition. I'm so close to leaving Eclipse PDT behind.

I am having one problem though.
See example

abstract class SimpleObject {
public function methodOne() {
}

public function methodTwo() {
}
}

class ComplexObject extends SimpleObject {
public function methodThree() {
}
}

class Foo {

/\*\*
\* @var SimpleObject
\*/
private $_object;

public function __construct(SimpleObject $example) {
$this->_object = $example;
}

/\*\*
\* Example method returns somthing like SimpleObject
\*
\* @return SimpleObject
\*/
public function getOject() {
return $this->_object;
}
}

$foo = new Foo();
$fooObject = $foo->getOject();
/\* @var $fooObject ComplexObject \*/
$fooObject->

Here I would want the vardoc to change what the editor thinks it has in $fooObject. Because I know what the object type is.

Posted by James Dempster on March 08, 2009 at 04:21 PM CET #

Hi - I have a problem not far from the problem Jacob statet. I have problems "type-casting" $this - and return value of methods are not properly working (when using an object "type-cast" via comments)

<?php

class Issue {
public function getDescription() {
}
/\*\*
\*
\* @return Bug
\*/
public function getLastBug() {

}
}

class Bug {
function getDefaultPriority() {
return 3;
}

/\*\*
\* @return Issue
\*/
public function depends() {

}
}

/\* @var $this Issue \*/
//$this resolves to "Bug" (Error)
$this->depends();

/\* @var $bug Bug \*/
//$bug resolves to Bug - All ok...
$bug->depends();

/\* @var $issue Issue \*/
//Return value of getLastBug resolves to Issue (Error)
$issue->getLastBug()->getLastBug()->getLastBug();

//Here everything is working:
$issue2 = new Issue();
$issue2->getLastBug()->depends();

I use $this "type-casting" alot since i use a Zend Framework approach to MVC. so a fix for this would be great! (Netbeans reacts so much faster than Zend Studio - speeding up production a lot!)

Im using the Netbeans 6.7 Beta (Just downloaded yesterday)

Posted by Henrik on May 18, 2009 at 02:59 AM CEST #

Hi

Really appreciate this feature but I think this feature is built keeping PHP as structured language.

My code is
function _initViewHelpers(){
$view = $this->_layout->getView();
}

where getView is a ZendFramework and returns Zend_View_Interface, but IDE shows ? for object $view after that line of code.

My work around which I hate to do is

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
/\*\*
\*
\* @var Zend_View_Interface
\*/
protected $_view;

function _initViewHelpers()
{
$this->_view = $this->_layout->getView();
}
}

As this you can see i have to put $this on every line.

Posted by Ali on July 04, 2009 at 10:14 AM CEST #

Just wondering how you get the build in which this has been enabled? I'm using Netbeans 6.5 on Windows Vista.....
Thanks for sharing

Posted by medyum on August 20, 2009 at 09:51 AM CEST #

It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.

Posted by links of london on October 29, 2009 at 08:09 PM CET #

Why doesn't it work with regular phpdoc syntax?

/\*\*
\* @var $usermodel ResidentsModel
\*/

Posted by thinsoldier on December 21, 2009 at 11:26 AM CET #

how would I add class identification using normal phpdoc syntax?

example from http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.var.pkg.html

class class1
{

/\*\*
\* example of documenting a variable's type
\* @var string
\*/
var $variable;

/\*\*
\* example of documenting a variable's type
\* @var string contains class1 information
\*/
var $variable_with_desc;

/\*\*
\* this variable is documented as type "mixed" since no @var tag is present
\*/
var $mixed_variable;
}

Posted by thinsoldier on December 21, 2009 at 11:31 AM CET #

Regarding my previous comment

I have a few files where this works:

/\*\*
\* @var EmployeesModel $employeesModel
\*/
public $employeesModel;

But it doesn't seem to start working anywhere close to immediately after typing it in a new file.

Only seems to work in files after I've saved them, closed them, waited a few minutes, and reopened them.

Posted by thinsoldier on December 21, 2009 at 12:32 PM CET #

Single line format:
/\* @var $VARIABLE CLASS \*/
This works.

Multi-line PHPDoc Style format:
/\*\*
\* @var CLASS $VARIABLE
\*/
This works

But they're listing the items in completely different orders. Single line has class after variable. Multi line has class before variable.

With the single line format, how can I also add a text description of the variable along with its class type all on one line?

Posted by thinsoldier on December 21, 2009 at 12:35 PM CET #

Something needs to be done about the spam in your comments

Posted by thinsoldier on December 21, 2009 at 12:40 PM CET #

I've got a big problem with that /\* @var $media Book \*/ Example.

Some of my files only have working code completion if I write the comment like
/\*\* @var Book $media \*/

Others only work if i write the comment like
/\* @var $media Book \*/

Posted by thinsoldier on January 22, 2010 at 03:07 PM CET #

"... the var tag has bold font..."
-------
In the Netbeans 6.8 bold Highlighting doesn't work anymore

Posted by zurom on March 04, 2010 at 03:11 AM CET #

But it doesn't seem to start working anywhere close to immediately after typing it in a new file.

Posted by alexander mcqueen heels on August 17, 2010 at 01:02 AM CEST #

Thanks NetBeans PHP team for the great work you do! Can't wait for version 7 to come out.

Posted by Emanuil on March 09, 2011 at 02:59 AM CET #

I'm new to PHP and therefore also NetBeans. Therefore please forgive my question if it is daft.

Why would you want to place the variable type in a comment? Why does it matter? Is there any purpose besides having the information in the code?

Just trying to understand the purpose of this feature a little better.

Thanks

Posted by Stefan Mikhail on October 22, 2011 at 02:21 PM CEST #

@Stefan: In certain cases the IDE is not able to recognized the type of a variable. PHP is not strongly typed language as for example Java is. So sometimes you need to help to ide to provide right code completion and other features, that are typed based.

Posted by Petr Pisl on October 23, 2011 at 12:40 PM CEST #

@Petr - thanks for the information. So in my case I have a variable called $clause. I used gettype to find the type and it is a string, so how would I make use of this feature for my variable?

Posted by Stefan Mikhail on October 23, 2011 at 02:06 PM CEST #

It seems that it has been removed in Netbeans 7.2

WHY ?!!

Posted by magnetik on October 03, 2012 at 05:24 PM CEST #

It wasn't. It still works.

Posted by Ondrej Brejla on October 04, 2012 at 01:07 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

This blogs is written by NetBeans developers who contribute to the PHP support mainly.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today