Hi Barry,
Just a [Request]
Parse bootstrap files for defined Macros
Example Macro I have in Bootstrap/start.php
Html::macro('linkButton', function($url, $label, $buttonType = 'primary', $glyph = '', $size = 'md')
{
return sprintf('<button type="button" class="btn btn-%s btn-%s"
onclick="window.open(\'%s\',\'_self\')">
<span class="glyphicon glyphicon-%s"></span> %s
</button>',$buttonType,$size,$url,$glyph,$label);
});
Thanks,
Chris :-)
Hmm, I don't really think that is possible.
The HTML macros are store in a protected var $macros in the HTML class, but there is no function to get a list of the macros. And I can't just scan all the files. Or do you know a good way of finding them?
It would probably be possible to add them to the config file, but you would have to add them manually.
You can get protected and private members with reflection:
class Cat {
protected static $color = "blue";
}
$cat = new Cat();
$catReflection = new ReflectionObject($cat);
$catColorProperty = $catReflection->getProperty("color");
$catColorProperty->setAccessible(true);
print_r($catColorProperty->getValue());
This feature would be awesome!
Currently, I document my macros like this:
I just put pa fake class definition on top of my macros file, where I just phpdoc- every macro. IDE works fine like this, but it's painful to keep the documentation up to date, if something changes.
if(false){
/**
* Class HTML
* @method static string alertSuccess($title, $message = '', array $par = [])
* @method static string pageHeader($title = '')
*/
class HTML extends \Illuminate\Support\Facades\HTML{
}
}
+1 for this
I know this is over two years old, but has anyone been able to solve this issue? More and more things are implementing the Macroable trait and it is getting increasingly frustrating when my IDE cannot autocomplete or jump to the definition of a macro method.
@briedis I've a number of collection Macros that I'd love to get to autocomplete.
But I can't seem to get your trick to work in my IDE (phpstorm). How would I get my collection macros to autocomplete when they are currently defined in a CollectionMacroServiceProvider class?
Thanks!
@jonnywilliamson Can you show me what have you tried (like in my example)?
Sure.
So here's my CollectionServiceProvider where I register all my extra Macros I want to add. I'll reduce it to just one for ease of viewing.
<?php
namespace App\Providers;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
class CollectionServiceProvider extends ServiceProvider
{
public function register()
{
Collection::macro('toAssoc', function () {
return $this->reduce(function ($assoc, array $keyValuePair) {
list($key, $value) = $keyValuePair;
$assoc[$key] = $value;
return $assoc;
}, new static);
});
}
So having read your comment above where you added a fake Class Docblock, I thought this might work.
<?php
namespace App\Providers;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
/**
* Class CollectionServiceProvider
*
* @package App\Providers
* @method Collection toAssoc(callable $callback) To Assoc description and info here.
*/
class CollectionServiceProvider extends ServiceProvider
{
public function register()
{
Collection::macro('toAssoc', function () {
return $this->reduce(function ($assoc, array $keyValuePair) {
list($key, $value) = $keyValuePair;
$assoc[$key] = $value;
return $assoc;
}, new static);
});
}
Basically in any other class I create I want to be able to do this:
<?php
namespace App\Providers;
use Illuminate\Support\Collection;
class demo
{
public function start()
{
$demo = collect([1,2,3]);
$demo-> //At this point autocomplete should show `toAssoc` as a complete option. It does not.
}
}
I generated a new _ide_helper, but that still didn't give me the toAssoc option.
So then I tried changing the fake docbloc class name to match the Illuminate\Support\Collection class name:
/**
* Class \Illuminate\Support\Collection
*
* @package App\Providers
* @method Collection toAssoc(callable $callback) To Assoc description and info here.
*/
But that didn't work.
So I'm open to suggestions!
@jonnywilliamson
The trick is that you need to create another decalration of Collection class that IS NOT included in the actual code.
Create a file somewhere, and add this content:

You can see, that I try to redefine Collection class. When PhpStorm encounters multiple definitions of the class, it just merges together methods and properties from both definitions and offers them in code completion. This is a hack, and can result in IDE warnings about duplicate class definitions.
Ahhh.
Ok, So I actually just stuck this at the top of my CollectionsServiceProviders class and it's working! Thanks!
For anyone else following this is what it looks like now:
<?php
namespace Illuminate\Support;
/**
* @method Collection toAssoc(callable $callback) To Assoc description and info here.
*/
class Collection{};
?>
<?php
namespace App\Providers;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
class CollectionServiceProvider extends ServiceProvider
{
public function register()
{
Collection::macro('toAssoc', function () {
return $this->reduce(function ($assoc, array $keyValuePair) {
list($key, $value) = $keyValuePair;
$assoc[$key] = $value;
return $assoc;
}, new static);
});
}
It's ugly, but it works!
Will do nicely for a sticky plaster for a while. Thank you @briedis
Your solution will probably generate an error, because you cannot re-declare a class twice. You have to put this hack-code in a separate file, file which will be indexed by IDE, but it will never acutally be included in the running code.
@briedis Indeed it did. Thank you for the warning.
I've now done what you suggested and it is working well! Really appreciate that.
Just looking for solutions to this problem and came across this.
My issue (after creating a separate file with my definitions) is that PHP Storm now complains about "Multiple definitions exist for class XXX" in my code. Apart from turning that inspection off, is there a solution to this?
Currently I have _ide_helper_macros.php which contains:
<?php
namespace {
exit('This file should not be included, only analyzed by your IDE');
}
namespace Illuminate\Support {
/**
* @method static array split(array $array, int $pieces) Split an array into pieces, as equally as possible.
*/
class Arr {}
}
In my service provider, I have the code to define that macro:
Arr::macro('split', function(array $array, int $pieces = 2) {
...
}
And in my Controller, I use the macro, but PHPStorm is highlighting it:

You can disable this inspection.
Settings | Editor | Inspections | PHP | Undefined | Undefined class | Don't report multiple class declaration potential problems
Be aware that this disables the inspection everywhere and for everything. So it will also not notify you about really duplicated classes.
All other hacks/settings won't work because they prevent phpstorm from indexing the file and therefore you won't have method completion.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this issue is still present on the latest version of this library on supported Laravel versions, please let us know by replying to this issue so we can investigate further.
Thank you for your contribution! Apologies for any delayed response on our side.
I'm closing this in good faith, assuming the recent macro support fixes this => see the release https://github.com/barryvdh/laravel-ide-helper/releases/tag/v2.8.1
Since this is a _very old_ issue, I think if anyone here experiences issues with the new feature, please open a new issue with details. Thanks!
Most helpful comment
Just looking for solutions to this problem and came across this.
My issue (after creating a separate file with my definitions) is that PHP Storm now complains about "Multiple definitions exist for class XXX" in my code. Apart from turning that inspection off, is there a solution to this?
Currently I have
_ide_helper_macros.phpwhich contains:In my service provider, I have the code to define that macro:
And in my Controller, I use the macro, but PHPStorm is highlighting it:
