Open andersondanilo opened 5 years ago
I'm not familiar with Larvel - why is the helper file necessary - are the methods "magic" ? (also not sure why the methods have bodies, if this is a stub they would never be executed?)
As an experiment (with no idea if it will work) you might try including the helper file by setting:
"worse_reflection.stub_dir": "%application_root%\/vendor\/jetbrains\/phpstorm-stubs"
in phpactor.yml
(or phpactor.json
) to the directory of the helper (instead of the php(storm stubs) (and then clearing the cache - phpactor cache:clear
) this will break standard PHP function/class completion, but i it fixes the laravel issue, it would be simple to allow multiple files to be specified here.
Otherwise if you have a link to a recent open laravel project on Github, I can try and have a look and see what the problems are.
cc @Einenlum will the above fix help, or do we need to support the Laravel autoloading mechanism (or do we need to support both stubs and the autoloading?)
TBH, I was so pissed off by Laravel and its aliases (man, I miss Sf so much), I took a week to remove aliases everywhere on every project I could find, with a search and replace. Took me a week because even PHPStorm is not good to replace these aliases with the real classes…
Now, this said, I can try on a previous version that have the aliases. Will try today.
@dantleech The file is generated through an artisan command: artisan ide-helper:generate
.
It generates a _ide_helper.php
file in the root of the app.
ls -1
app
artisan
bootstrap
composer.json
composer.lock
config
database
deploy
docker
docker-compose.yml
_ide_helper.php
public
README.md
storage
tests
vendor
I added the stub dir in my phpactor configuration:
phpactor config:dump | grep worse
"completion_worse.completor.class.limit": 100,
"worse_reflection.enable_cache": true,
"worse_reflection.cache_dir": "%cache%\/worse-reflection",
"worse_reflection.stub_dir": "%application_root%\/_ide_helper.php",
"worse_reference_finder.plain_text_break_chars": [
Given the following file:
<?php
namespace App\Http\Controllers;
class TestController extends BaseController
{
/**
* @return \Illuminate\View\View
*/
public function test()
{
// Get the data
$foo = \C<>onfig::get('foo');
// ...
}
}
When I try to jump on definition, I get: Error from Phpactor: Unable to locate class "Config", Word "\Config" could not be resolved to a class
.
Edit: I forgot to clear the cache. Otherwise it would have said _ide_helper.php
is not a directory.
Oh! My bad @dantleech ! It works!
I moved the _ide_helper.php
file to vendor/ide-test
. Then updated my config:
➜ ~ phpactor config:dump | grep worse
"completion_worse.completor.class.limit": 100,
"worse_reflection.enable_cache": true,
"worse_reflection.cache_dir": "%cache%\/worse-reflection",
"worse_reflection.stub_dir": "\/tmp\/somefolder\/vendor\/ide-test",
"worse_reference_finder.plain_text_break_chars": [
➜ ~
Updated the cache, and it works!
It just does not work with a relative path. It seems %application_root%
is transformed to the path of phpactor and not where the application lives (it's probably wanted?).
Edit: of course it's wanted cause phpactor can be run outside vim :man_facepalming:
I tried to copy the file _ide_helper.php to ~/.vim/bundle/phpactor/vendor/jetbrains/phpstorm-stubs and now its working (after cache:clear)
It just does not work with a relative path.
Maybe %project_root%
if you want the root of the project?
... and now its working (after cache:clear)
Great news :) I will keep this ticket open in order to change the code to accept an array of locations for stubs (and maybe add a Laravel integration page in the docs
Now that phpactor uses an index, _ide_helper.php
and _ide_helper_model.php
seem to be picked up by the indexer. There is currently a looming issue with the @mixin
tag not being supported yet, so technically this doesn't work, but it's a closer than I previously realized! The stubs path isn't necessary!
It seems the helper is "overriding" some vendor files in phpactor too. For example it doesn't see $request->user()
as valid because the request it is using is from the _ide_helper.php
file only. I think it's meant to "magically" use that along with the file from vendor.
The linked example above has all methods defined but mine has just magic ones, so I'm going to see if there is a way to just do all methods again like it used to.
I was pleasantly surprised to see that phpactor picked up on the _ide_helper.php
and _ide_helper_models.php
files out-of-the-box (using Neovim and the phpactor language server), however, there's two issues:
_ide_helper*
files.@mixin
tag, but it phpactor doesn't pick up on this.Also, it doesn't look like the PhpStorm meta file (.phpstorm.meta.php
) is picked up, is support for that on the roadmap? I saw some other issues mention it.
Running into this now as well. IDE helper generates a same class names with docs in a separate file. This files is being indexed but it loses the scope of the original class.
While mixin works, ideally this would not be required.
I might try to tackle this, but some pointers on where to start digging would be welcome :).
I'm not sure what needs to be done exactly. Would we want the analysis and docs to use the "helper" while goto definition / find references ignores the stub file? Without actually working on a Laravel project I'm not sure.
So ide helper generates for example _ide_models.php with something like this:
namespace App\Models{
/**
* App\Models\Board
*
* @property int $id
* @property string $name
* @property bool $available
* @property int $event_id
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \App\Models\Event|null $event
* @property-read \App\Models\Game|null $game
* @method static \Illuminate\Database\Eloquent\Builder|Board newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Board newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Board query()
* @method static \Illuminate\Database\Eloquent\Builder|Board whereAvailable($value)
* @method static \Illuminate\Database\Eloquent\Builder|Board whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Board whereEventId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Board whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Board whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|Board whereUpdatedAt($value)
* @mixin \Eloquent
*/
class Board extends Model {}
}
The helper only documents things that are not part of the model itself.
Now what happens (I think) is because the class Board is the same as my actual Board class, it ignores that one and only indexes the helper file.
This results in actually defined methods to be ignored.
It is perfectly fine for goto-definition to go to the helper file if it is a documented property/method/.., however, the original class should continue to work.
Sorry if this is not explained well.
So intelephense solves this issues by populating the quickfix with the different references (however, it only seems to do this on the Laravel class through the _ide_helper.php
file but it takes you directly to the models):
or using telescope:
That's kinda what I was trying to explain. I think no more that 1 instance of a class can exist in phpactor.
hmm ... we index both, it would be possible to change Phpactors behavior to offer both files on goto definition - would that solve it?
On the left is the source, on the right the "models.php" file that has the generated data.
When not using a mixin, it does not complete from there.
If you generate the model "fixes" with artisan ide-helper:models -W
which writes the docblock onto the actual model file instead of a separate file. This means for those things it works correctly.
The Eloquent
mixin and other areas of _ide_helper.php
completely breaks things though, to the point I have just disabled it.
docblock onto the actual model file instead of a separate file
My team doesn't want our models littered with those docblocks.
The IDE Helper package has the --mixin
option which creates the helper file with unique model names, (e.g., IdeHelperUser
), the user model would then have a /** @mixin IdeHelperModel */
phpdoc. However, I have found that the @mixin
doesn't work in phpactor, although I have seen some issues about it so I'm not sure if it's on the roadmap.
as per this test case (and its siblings) @mixin
should work: https://github.com/phpactor/phpactor/blob/42f88bfcee0dbae23bbb95db0c4809fd3315fbd9/lib/WorseReflection/Tests/Inference/reflection/mixin_class.test#L12 so maybe worth figuring out what's wrong there.
In my tests mixins work 👍
Does anyone have a ide-helper config as to what are the best options to set? I'm new to this and kinda lost as to which options should I enable, and whether to use mixins or not.
I would also like to know how people are using phpactor with laravel-ide-helper.
Currently, (1) if I use laravel-ide-helper, things like $request->user()
are regarded as undefined methods because of the overriding behavior described above. (2) On the other hand, if I don't use laravel-ide-helper, I can't handle Laravel's (problematic) magic methods like User::create(...)
.
Both of the options aren't desirable. Did anyone find any workarounds?
As an experiment (with no idea if it will work) you might try including the helper file by setting:
"worse_reflection.stub_dir": "%application_root%\/vendor\/jetbrains\/phpstorm-stubs"
in
phpactor.yml
(orphpactor.json
) to the directory of the helper (instead of the php(storm stubs) (and then clearing the cache -phpactor cache:clear
) this will break standard PHP function/class completion, but i it fixes the laravel issue, it would be simple to allow multiple files to be specified here.
is it already possible to specifiy multiple files here?
No, but you should be able to do it here: https://phpactor.readthedocs.io/en/master/reference/configuration.html#indexer-stub-paths but I don't know if related to this issue?
Well, the thing is that in Laravel there is an _ide_helper.php
and _id_helper_models.php
file, and both need to be loaded. And in addition I also want the plain PHP functions to work. So I think this is related. I tried the indexer.stub_paths
option, but it seems like I cannot set files here, directories must be passed instead. Wouldn't it make sense to be able to pass files as well to this option?
However, putting both files in the same folder and add this folder to this indexer.stub_paths
option works fine.
I tried again and everything works out of the box with _ide_helper.php
and _ide_helper_models.php
. Not sure this issue still needs to stay open.
@Einenlum, it does not work out of the box, https://github.com/phpactor/phpactor/issues/2420 is still an issue.
Oh my bad :+1:
Hi all,
Using the laravel ide helper here with vim and some laravel projects.
Generating _ide_helper.php
and _ide_helper_models.php
into a folder (.ide_helpers
) at the root of the project.
Adding to .phpactor.json
the line "indexer.stub_paths": ["%project_root%\/.ide_helpers\/"]
The above resolve for me the facades (models mostly) functions to be known.
Now something I came across is the reflection getting only interfaces function definition instead of concrete class.
Example is for $this->app->isLocal()
for instance does underline isLocal()
not to be part of the interface (which is true).
but isLocal()
can be found in _ide_helper.php
Any news on this front?
Coming to this issue since I upgrade my neovim version and config (my bad lol?) and I can't manage to get this to work (it was working previously). Could there be any issue with the new phpactor version?
Previously I only generated the ide helper in my project root, and phpactor would work out of the box (no config needed). But now, I tried all config options here, the indexer says it is loading them, and I still get issues reported for Model::query()->join
for example.
Anyone know how to address this?
I think somebody needs to investigate it, as I probably won't have time and as it doesn't affect my work directly there isn't much motivation for me to look into it. I'm happy to provide pointers in a discussion and help out.
Can we support laravel ide helper: https://github.com/barryvdh/laravel-ide-helper?
Example file: https://gist.github.com/barryvdh/5227822