Bug 184676 - PHPDoc multiple types @return
Summary: PHPDoc multiple types @return
Status: CLOSED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: PDT (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 enhancement with 2 votes (vote)
Target Milestone: ---   Edit
Assignee: PHP UI CLA
QA Contact:
URL:
Whiteboard:
Keywords: bugday
Depends on:
Blocks:
 
Reported: 2007-04-30 05:42 EDT by Sebastian CLA
Modified: 2020-05-14 11:37 EDT (History)
9 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastian CLA 2007-04-30 05:42:26 EDT
Build ID: M20070212-1330

Steps To Reproduce:
create this:


class A{
	var $a;
	
}

class B{
	var $b;
	
}

class Factory{
	
	/**
	 * @param string $name
	 * @return A|B some description here..
	 */
	function get($name){
		// some code here..
	}
}

$factory = new Factory();
$a = $factory->get("A");
$a-> <-- should get autocompletition hint for class A or at least for all return types

More information:
of course if we change
* @return A|B
to
* @return A
or 
* @return B

everything works fine..

I think that multiple types should work because it is defined in standard 
(http://manual.phpdoc.org/HTMLframesConverter/default/phpDocumentor/tutorial_tags.return.pkg.html)
Comment 1 Sebastian CLA 2007-04-30 05:57:00 EDT
I've found similar problem here:

http://209.85.129.104/search?q=cache:zd8rSEJ0I1UJ:framework.zend.com/issues/browse/ZF-1221+phpdoc+pdt+return+multiple+type+%40return&hl=pl&ct=clnk&cd=1&gl=pl&client=firefox-a

but using
/**
 * @return mixed A or B; some descrip here..
 */

is not a solution
Comment 2 Ian CLA 2007-08-09 22:40:09 EDT
I think a good solution would be somekind of comment typehinting for when the objects are returned.

for example:

$factory = new Factory();
//@hint Class_Name
$object = $factory->get("Class_Name");

Comment 3 Double Compile CLA 2007-08-10 07:50:48 EDT
(In reply to comment #2)
> I think a good solution would be somekind of comment typehinting for when the
> objects are returned.
> 
> for example:
> 
> $factory = new Factory();
> //@hint Class_Name
> $object = $factory->get("Class_Name");
> 

You can already do this.  Example:

$factory = new Factory();
$object = $factory->get("ClassName");
/* @var ClassName $object */

But this doesn't fix the main problem with not being able to specify multiple return types.  If anything, the editor should just recognize the first one.  A lot of times, people make a method return an object or false (or null).  As noted in the second comment, there are several methods in the Zend Framework whose @return doc tag has multiple values.
Comment 4 Double Compile CLA 2007-08-10 07:51:35 EDT
(In reply to comment #3)

> /* @var ClassName $object */


My mistake.  That should be:

/* @var $object ClassName */


Comment 5 Dominic Clifton CLA 2008-07-12 08:18:23 EDT
> $a = $factory->get("A");
> $a-> <-- should get autocompletition hint for class A or at least for all
> return types

ok, now pretend $sClassNameToGet was initialized from a command line parameter or via an xml config file:

$a = $factory->get($sClassNameToGet);
$a-> <-- what should appear in an auto-complete box here?

A better solution for the coder might to use interfaces and then set the @return of the the factory's get() method to @return myInterface

Obviously this solution is not appropriate for some situations where you're returning different classes that don't support the same interfaces.  e.g. a bean factory.  that's where the type hinting (see comment #4) should be used.

this is what I do and it's ok, but this is part of the problem with having a non-typed language.

in type'd languages you'd have to do something like this anyway:

Object = <ClassA>Factory->get('A');

so in terms of code readability:

$object = $factory->get('A');
/* @var $object ClassA */

it's not too bad, but then it is php :D

Always remember the fact that incorrect documentation is often worse than no documentation, if the editor's telling you you've got loads of methods available which are not suited to BOTH classes (in the case of using @return with multiple return types) then you might write some code which uses a non existent method without realizing, only to find out when you run the code and it dies with a fatal error.
Comment 6 Michael Spector CLA 2009-05-08 18:15:57 EDT
Fixed in CVS. Both ways are usable:

1.

/**
 * @return A|B
 */
function foo() {
}

foo()->| <--- list members of A and B

2.

/* @var $v A */
$v->| <--- list members of A

Comment 7 Vadim Punski CLA 2009-12-30 01:53:43 EST
Tested on 2.2.1SR1.
Closing.