I have created a standard Registry class. The registry can then return a new or already created instance of a class by using:
$classObject = $registry->getInstance(Namespace\subdirectories\ClassName);
My project is set up with namespaces, so that is the typical syntax.
All works well, except IDEs like Eclipse luna will no longer have Content Assist, meaning pressing ctrl+space will no longer pop up a screen with the classes public methods.
Has anyone run into this situation before? How did you restore content assist functionality as best as you could? Alternatively, if you could not restore the functionality, what did you do so programming remained somewhat still practical within the project?
I have fixed this issue by using PHPDoc type hinting for my IDE
Example
/**
/* #var \FullClassNamespace\
*/
$private $someVar;
public function __construct() {
$this->someVar = registry->getClassinstance(fullnamespace);
$someVar-> (autocomplete works)
}
It is worth noticing that in some IDEs doing an inline type hinting won't work, as in /** #var namespace */. The initial /** and ending */ must be in separated lines
Related
I am new using PhpStorm. Previously I used Visual Studio Code and Sublime Text 3/4, however with PhpStorm it is much more convenient to work with PHP and I am happy with the IDE.
I have customized the style of the code according to my needs and the convention of the Framework I am working with. The problem arises when doing a cleanup and/or reformatting of the code: when this happens, the protected and public methods get mixed up. I will try to explain it as best as possible.
For example: if I have a protected method named getPath that is called in one or more public methods at the beginning of the class, by convention this method should be at the end of the class along with the private methods, but then if I set another public method that does not use the getPath method, getPath is placed on top of the new public method. I KNOW! It doesn't seem to be a problem, but when you follow strict convention rules of a framework or a personal convention, this ends up being very important.
In my case, I'm extremely clean and tidy in my code, but I haven't been able to configure the IDE to keep methods private and protected at the end of the class.
I would like to know if someone can help me configure the IDE to solve the problem and if possible I would like the magic methods (__call, __callStatic, __sleep, __get) to be kept at the end of everything.
I am using PhpStorm 2021.3.2.
Hello i have a bootloader where i define some basis services like
/** #var $serviceManager Zend\ServiceManager\ServiceManager */
$serviceManager = new ServiceManagerComposite(new ConfigurationComposite($configuration['service_manager']));
But this type of documentation is not available trough my legacy code for hinting, as i found out this will define the variable only on tht one php file. But because i use it as a legacy acess point for my code i need it for the whole php project...
Any sugestions what type of documentation would work with the IDE hinting from Netbeans?
You'd probably need to duplicate at least the first use of $serviceManager with the same #var docblock in each file. If NetBeans is not already resolving scope for you via requires and includes, then this would be the only way to put a context on that "local" variable in the file you're in.
Sangoku is perfectly right!
/* #var $serviceManager Zend\ServiceManager\ServiceManager */
Should do the trick
Does anyone know any way to specify #return type based on the input parameter?
My project has a lot of method calls such as
...->getComponent('name')->someMethod()
where getComponent is a service locator and IDE doesn't understand which value it returns.
For the PhpStorm IDE, you can create one or more files called .phpstorm.meta.php in your project. Here is an example content of such a file:
<?php
namespace PHPSTORM_META {
override(\Factory::getComponent(), map([
"name" => \some\namespace\Name::class,
"exception" => \Exception::class,
]));
}
You can find the documentation here:
https://www.jetbrains.com/help/phpstorm/ide-advanced-metadata.html#map
I recommend committing this file to make auto-completion available to other PhpStorm users.
The only way would be to have getComponent()'s #return tag actually list out all possible data types that could return. This kind of dynamic, loose typing behavior that PHP allows for is not conducive to such static resolution as done by an IDE autocomplete. It has always been a tough case to try to document, because your API doc is just as "static" as the "object tree" that an IDE has to construct in order to provide autocompletion.
If IDE autocompletion is urgent enough to warrant adjusting the code to make it happen, then aside from my earlier
#return ClassOne|ClassTwo|...|ClassInfinitum option,
you could split the two calls rather than chaining them, and use an #var "local docblock" that some IDEs know how to recognize:
// this litte docblock establishes the data type for the local variable $nameComponent
// note however that some IDEs want "datatype varname",
// some want "varname datatype",
// and some don't recognize the docblock at all
/** #var \NameClass $myComponent */
$nameComponent = $service->getComponent('name');
// here, the known methods of NameClass should pop up after typing ->
$nameComponent->
I am making usage of
/* #var $var Type */
really often for NetBeans can then autocomplete methods and stuff in code.
Still I think its a very useful feature but sometimes I got objects of
classes extending one more class and implementing multiple interfaces.
Or I even got a transitive class hierarchy.
I don't know a way to tell NetBeans that it shall be using autocomplete for
all these interfaces and upper-layer parent classes.
I would like to do so for of course every of these interfaces / classes got
dedicated methods (which are defined somewhere in case of interfaces...)
I tried something like this:
/* #var $var TypeA|\TypeB|\TypeC */
because I saw NetBeans will generate a similar documentation for methods returning
different class objects due a switch/case. But this seems to work only for the
#return notation.
I also tried
/* #var $var TypeA|TypeB */
Also not working...
NetBeans will autocomplete the last told Type in this case but not a combination of both/all told classes.
How can I document so my autocomplete works as desired (a summary of methods of all classes /interfaces I listed)?
regards!
If I understand you correctly, you are asking to chain hint through your PHP code.
The problem is netbeans has no way of knowing what an object actually is; unless you tell it. The solution is to use the #property command in your object decleration to forward type define the objects members , be it a class or interface.
/**
#property classMyClass1 $clsMyClass1
#property classMyClass2 $clsMyClass2
*/
class baseClass{
public $clsMyclass1;
public $clsMyClass2;
public function __construct() {
$this->clsMyClass1 = new classMyClass1();
$this->clsMyClass2 = new classMyClass2();
}
}
$foo = new baseClass();
Now when you type your code in netbeans it will know what hinting to display on your last typed object
$foo->clsMyClass1->
So long as each class has a forward property decleration you can chain as long as you want
$foo->class1->class2->class3->...
The above code would need the autoload() function to load the correct class files....
Hope this helps you!
How might I hint at what $this is in the context of a Magento template, with a DocBlock?
As an example, head.phtml within the template/page/html/ directory is an object of Mage_Page_Block_Html_Head. The method I tried was:
<?php
/**
* #var $this Mage_Page_Block_Html_Head
*/
?>
Unfortunately, it doesn't seem my IDE (NetBeans) is picking up on the hint. So my question is if there is a correct way of doing this so that NetBeans recognizes the hint.
Netbeans should accept it, at least my version 6.9 for
/* #var $this Bar */
does this:
and that is an example with the include scenario (no class around).
Take care to use the single-line - single-asterisk - type-of - commment.
Also take care Netbeans updated the index. As in your case:
NetBeans had to scan for changes in this case, which I would have assumed it would do automatically. [...]. In my case it only periodically scans.
Docblock variable hinting related:
How do I make my PHP IDE understand Dependency Injection Containers?