Jump to content
interrobang

PhpStorm autocompletion and typehinting of wire('xx')

Recommended Posts

In PhpStorm you can get autocompletion and typehinting for wire function calls if you add a file with the following content somewhere to your project. Jetbrains suggests naming this file '.phpstorm.meta.php' and adding it to the root of your project, but it should work anywhere else too. I had to re-open the project after adding the file.


<?php
/**
* ProcessWire PhpStorm Meta
*
* This file is not a CODE, it makes no sense and won't run or validate
* Its AST serves PhpStorm IDE as DATA source to make advanced type inference decisions.
*
* @see https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata
*/


namespace PHPSTORM_META {

$STATIC_METHOD_TYPES = [
\wire('') => [
'' == '@',
'config' instanceof Config,
'wire' instanceof ProcessWire,
'log' instanceof WireLog,
'notices' instanceof Notices,
'sanitizer' instanceof \Sanitizer,
'database' instanceof \WireDatabasePDO,
'db' instanceof \DatabaseMysqli,
'cache' instanceof \MarkupCache,
'modules' instanceof \Modules,
'procache' instanceof \ProCache,
'fieldtypes' instanceof \Fieldtypes,
'fields' instanceof \Fields,
'fieldgroups' instanceof \Fieldgroups,
'templates' instanceof \Templates,
'pages' instanceof \Pages,
'permissions' instanceof \ Permissions,
'roles' instanceof \Roles,
'users' instanceof \Users,
'user' instanceof \User,
'session' instanceof \Session,
'input' instanceof \WireInput,
'languages' instanceof \Languages,
'page' instanceof \Page,
]
];
}



  • Like 8

Share this post


Link to post
Share on other sites

Good find @interrobang. Thanks for posting. I like how this narrows it down because before I would get every method of every API variable possible, which worked just fine, but this makes it even simpler. A couple of questions that I wasn't able to figure out from the linked page, and thought you might know: 

1. How to make this apply to $this->wire() in the same way? (i.e. The wire() method from the Wire class). I tried to do it like this, duplicating the same properties, but no luck:

 \Wire::wire('') => [ all the same properties here ]

2. Also trying to figure out how to make it recognize a wire() or $this>wire() call with no arguments returns an instance of class ProcessWire. Any ideas? I'm not sure I fully follow what the '' == '@' means, the doc page is a little unclear, or maybe it's time to refill my coffee. 

  • Haha 1

Share this post


Link to post
Share on other sites

Unfortunately I can't help here - I did not understand much of the documentation. Getting to this point was mostly trial and error.

I am not even sure if this can be achieved with this file. I think the main purpose of the meta file is to enable type-hinting for static factory methods. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...