Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro support doesn't work #531

Open
aledelgo opened this issue Jun 27, 2017 · 7 comments
Open

Macro support doesn't work #531

aledelgo opened this issue Jun 27, 2017 · 7 comments

Comments

@aledelgo
Copy link

@aledelgo aledelgo commented Jun 27, 2017

[Note i've previously posted this message as comment on the Macro feature PR but it seems i've got no visibility, hope this don't violate any rule ]

Unfortunately on my environment it doesn't works fine well... i'm on PhpStorm with laravel 5.3

i have this code in a custom registered provider (my code is working fine)

class ResponseMacroServiceProvider extends ServiceProvider {

    /**
     * Perform post-registration booting of services.
     *
     * @return void
     */
    public function boot()
    {



        /**
         * Create a response()->apiResponse(...) function
         */
        Response::macro('apiResponse', function($apiResultObj, $serializazionGroups=null, $includeDefaultGroups=true, $jsonPCallback=null)
        {

            /* custom code here...*/

            return $resp;


        });
    }

}

I see ide-helper correctly generate this in _ide_helper.php file

/**
         * Create a response()->apiResponse(.
         * 
         * ..) function
         *
         * @static 
         */ 
        public static function apiResponse($apiResultObj, $serializazionGroups = null, $includeDefaultGroups = true, $jsonPCallback = null)
        {
            return \Illuminate\Routing\ResponseFactory::apiResponse($apiResultObj, $serializazionGroups, $includeDefaultGroups, $jsonPCallback);
        }

but the IDE don't recognize it in the autocomplete list.
when i write:
return response()->apiResp
the method is not listed in the autocomplete list, and if i write it completely like
return response()->apiResponse($result, [], false);
the method is underlined and marked with a tooltip sayng the method is not found
image

Do you have any idea?

@mirbaagheri

This comment has been minimized.

Copy link

@mirbaagheri mirbaagheri commented Nov 17, 2017

I use appstract/laravel-response-macros for my laravel passport.
ide-helper can not handle new methods from this package.

public function run($factory)
{
    $factory->macro('error', function ($message, $status) use ($factory) {
        return $factory->make([
            'status' => 'error',
            'error' => $message,
        ], $status);
    });
}
@vpratfr

This comment has been minimized.

Copy link

@vpratfr vpratfr commented Mar 9, 2018

Got some macros defined in my TestCase base class and they do not get picked up either. What is the way to get our macros picked up by ide-helper? Any configuration parameter could help?

@crhg

This comment has been minimized.

Copy link
Contributor

@crhg crhg commented Sep 7, 2018

Currently, macro support is implemented as part of facade alias processing, so the target class must be registered as a facade with an alias. (Eg included in aliases in config/app.php)

Laravel has many classes that have the ability to define macro but not facade. (ex \Illuminate\Support\Arr, \Illuminate\Support\Collection, \Illuminate\Validation\Rule,...) For this reason they are not the target of macro support.

One workaround is to prepare appropriate aliases for those classes and add them to aliases in config/app.php. For example:

    'aliases' => [
        ......

        'Collection' => \Illuminate\Support\Collection::class,
    ],

Note:
I think this will work for most classes, but it doesn't work for \Illuminate\Database\Eloquent\Builder. This is because the current macro support judges whether or not the class has the macro function by judging whether or not the class uses the \Illuminate\Support\Traits\Macroable trait, \Illuminate\Database\Eloquent\Builder does not use the Macroable trait and has its own implementation.

@LastDragon-ru

This comment has been minimized.

Copy link

@LastDragon-ru LastDragon-ru commented Jun 3, 2019

but it doesn't work for \Illuminate\Database\Eloquent\Builder

vendor/barryvdh/laravel-ide-helper/src/Alias.php

Find:
if ($traits->contains('Illuminate\Support\Traits\Macroable')) {

Replace to
if ($traits->contains('Illuminate\Support\Traits\Macroable') || $reflection->getName() === Builder::class) {

And it will work :) (laravel 5.8)

@spencero91

This comment has been minimized.

Copy link

@spencero91 spencero91 commented Jun 20, 2019

It doesn't seem to find macros in custom service providers.... Or at least it isn't for the Cache facade.

@roni-estein

This comment has been minimized.

Copy link

@roni-estein roni-estein commented Sep 17, 2019

@spencero91 were you able to figure this out? I've run into this situation where I've been curating macros definitions into an AutoCompleteHelper.php. That all works beautifully, however, if some macros are coming from different packages that are being used internally, if the next project has more macros or uses doesn't use all the macro's of the first file, you wind up having to manually curate that helper file.

Extracting the helper file to the package seems like the obvious win, and it will work if you open the file up with phpstorm, and let it index it manually. However, if you make a change in the file in another IDE for example, to model something like a composer update where it changes behind the scenes, it won't update in PHPstorms index. If you do the same thing to the regular AutoCompleteHelper behind the scenes, it seems like it will update.

This is probably a horrible solution, but the first thing that crosses my mind, is to add a command to composer dump-autoload that reads all packages for an AutoCompleteHelper.php, and then basically merge them into some safe space like config/autocomplete.php

If anyone has solved this already and better please advise.

EDIT
Can't put it in config, but I'm hesitant to put it somewhere where it could affect the app, so I've settled on ide/AutoCompleteHelper.php

@roni-estein

This comment has been minimized.

Copy link

@roni-estein roni-estein commented Sep 19, 2019

I wrote a small package that uses some of the advice here to solve my own use case, which deals with TestCase Macros as well as macros in packages. It's not the coolest solution but it does work. you can find it at https://github.com/roni-estein/package-macro-autocomplete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants
You can’t perform that action at this time.