PHP Open API abilities/limitations
This is probably an Alexey question or at least a reference to an Alexey answer.
I have been posed with 2 questions from a few people about my plugin that I have an inkling the answer is no but I do not want to say it is not possible unless it is actually not. Very easy to say no, more beer time. Plus I am genuinely interested.
1. Is it possible to return a list of objects via a signature eg. #K#C\DomDocument.[]|? (If that is right and resolves to DomDocument.class except for the [] I have spent way to long in psi viewer :) )
Eg,
class mockTest { function foo() { } } function mockObjects($classname) { $objects = array( new $classname(), new $classname(), new $classname() ); return $objects; } $objects = mockObjects('mockTest');
So a signature formockTest[] is desired.
2. Is there a signature format for multiple types eg.
/** @var Foo|PHPUnit_Framework_MockObject_MockObject */
$foo = $this->getMock("DomDocument");
The following does not work for a DomDocument|SimpleXmlElement combination
phpIndex.getBySignature( "#K#C\\DomDocument.|#K#C\\SimpleXmlElement.", null, 0 ) I am also unsured what the null/0 parameters do. Many thanks (I like the completion contributor example as I have not noticed that before)
Please sign in to leave a comment.
#K is for ClassConstant and every dot requires a member name after it and there can't be any [] so neither of signatures in post makes sense.
Perhaps you can elaborate?
As to phpIndex.getBySignature( "#K#C\\DomDocument.|#K#C\\SimpleXmlElement.", - you have to split on | before calling it. And supply valid signatures of course.
Okay I will try to elaborate, signatures just confuse/over complicate things and this should be discussable just using phpIndex.getAnyByFQN in my examples..
1.
If I write the following
@Override
public Collection<? extends PhpNamedElement> getBySignature( String signature, Project project ) {
PhpIndex phpIndex = PhpIndex.getInstance( project );
Collection<PhpClass> domDocument = phpIndex.getAnyByFQN( "\\DOMDocument" );
return domDocument;
}
everything then returns DOMDocument if getType returns a signature for that call. Is it possible for getBySignature to return something that is interpreted like the
@return \DOMDocument[]
PHPDoc equivalent
Unfortunately the psi viewer shows \DOMDocument[] as its signature when using PHPDoc which is why I brought up the [] bit.
2. I want to simulate "@return DomDocument|SimpleXmlElement"
@Override
public Collection<? extends PhpNamedElement> getBySignature( String signature, Project project ) {
PhpIndex phpIndex = PhpIndex.getInstance( project );
Collection<PhpClass> domDocument = phpIndex.getAnyByFQN( "\\DOMDocument" );
Collection<PhpClass> simpleXmlElement = phpIndex.getAnyByFQN( "\\SimpleXMLElement" );
return //????? - If it is at all possible how can I conjoin them into the @return \DOMDocument|\SimpleXMLElement equivalent so auto complete o offered for both classes in the PHPStorm side?
}
Hopefully this makes it clearer what is trying to be achieved.
Many thanks for getting back to me on this.
Sorry, this does not make any sense to me – and I'm the author of the PhpIndex and signatures and stuff.
Please start with original task you trying to accomplish.
I can say now only that you shoud not work with types as strings. And, typically, with any signatures directly.
And that "FOO|BAR" is only a representation. And not a signature those always start with "#". Use PhpType class.
Thanks for you feedback..
I found this http://devnet.jetbrains.com/thread/443959 which reminded me I had to do something similar elsewhere.
so new PhpType().add("Class[]") caters for point 1
and new PhpType.add("\\DomDocument[]").add("\\SimpleXMLElement[]"); caters for point 2
On a curious side point the method getBySignature returns a collection of the abstract PhpNamedElement what would be the preferred implementation of PhpNamedElement to use to add the type to?
Creating a class implementing PhpNamedElement and then returning it from the implemented
public PhpType getType() {
return type;
}
works fine and dandy but it creates a lot of generated boiler plate code so is maybe less than ideal from your perspective.
I hope that makes sense. Anyway I can hunt through other plugin source for lighter altenatives.
Have a nice easter.
I'm sorry, but It utterly does not.
We're unable to provide you further guidance on API unless you describe the original task you trying to accomplish by doing all this.
1. provide isolated yet complete PHP code example
2. describe how IDE treats it now
3. describe how you want it work - complete, navigate, anything else.
You can class this as closed as the PhpType reference you gave earlier really answers everything I need.
Thanks for your help.