PHPDoc Type Hinting for variables
Hi,
Is there a way I can get tupe hinting for variables? Take this code for example:
function readAll_array() {
$query = $this->db->get('gk_blog_posts');
if ($query->num_rows > 1) {
return $query->result_array();
}
}
What I would like to is to be able to hit Crtl-Space at $query so that it display the options available. At the moment, no suggestions are displayed what so ever.... can anyone help... working with CodeIgniter btw...
thanks
请先登录再写评论。
Well I am using this on top of my contoller and it works (almost all completion)
/**
* @property CI_Loader $load
* @property CI_Form_validation $form_validation
* @property CI_Input $input
* @property CI_Email $email
* @property CI_DB_active_record $db
* @property CI_DB_forge $dbforge
* @property CI_Table $table
* @property CI_Session $session
* @property CI_FTP $ftp
* @property CI_Encrypt $encrypt
*/
I have all those added to my code already...
but in my example, when you use Ctrl-Space with the $query variable, it should also display things like num_rows, affected_rows, etc... which it isnt doing... so there must be a way to get that to work
For completion type for variable $query need to be deduced. You can check type via Quick doc popup (Ctrl+Q) - if deduced it will be displayed in first line.
Type inference relies on code introspection (assignments, new object creation) and function/method phpdoc annotations. In this case you need correct @return TypeName annotation for get('..') method.
Alexey, you can kill me if I understood anything you said mate
:|
Yeah, that definitely threw me off a bit... any examples that you have would be very helpful..
Okay.. I'm not familiar with CI framework, but it looks like it uses ActiveRecord pattern.
CI_DB_active_record->get() is annotated with @return object - this effectively prevents member completion.
There's currently no way to annotate @return type based on 1st argument (however this advanced annotations are planned for next release of PS)
Thus to get completion now you need to resort to @var annotation after each declaration which is not practical, i.e:
function readAll_array() {
$query = $this->db->get('gk_blog_posts');
/** @var $query gk_blog_posts_class */
if ($query->num_rows > 1) {
return $query->result_array();
}
}
Even more, gk_blog_posts_class now need to actually exist and have either actual properties or @property annotations.