Add better support for macros and mixins (#1006)

Co-authored-by: Dominik Krämer <[email protected]>
This commit is contained in:
domkrm
2020-09-06 07:58:13 +02:00
committed by GitHub
co-authored by Dominik Krämer
parent 24596b67f1
commit 3a22b8daf6
4 changed files with 93 additions and 2 deletions
+59 -1
View File
@@ -13,6 +13,9 @@ namespace Barryvdh\LaravelIdeHelper;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use ReflectionClass;
use Symfony\Component\Console\Output\OutputInterface;
class Generator
@@ -220,7 +223,11 @@ class Generator
*/
protected function getAliasesByExtendsNamespace()
{
return $this->getValidAliases()->groupBy(function (Alias $alias) {
$aliases = $this->getValidAliases();
$this->addMacroableClasses($aliases);
return $aliases->groupBy(function (Alias $alias) {
return $alias->getExtendsNamespace();
});
}
@@ -291,4 +298,55 @@ class Generator
echo $string . "\r\n";
}
}
/**
* Add all macroable classes which are not already loaded as an alias and have defined macros.
*
* @param Collection $aliases
*/
protected function addMacroableClasses(Collection $aliases)
{
$macroable = $this->getMacroableClasses($aliases);
foreach ($macroable as $class) {
$reflection = new ReflectionClass($class);
if (!$reflection->getStaticProperties()['macros']) {
continue;
}
$aliases[] = new Alias($this->config, $class, $class, [], $this->interfaces);
}
}
/**
* Get all loaded macroable classes which are not loaded as an alias.
*
* @param Collection $aliases
* @return Collection
*/
protected function getMacroableClasses(Collection $aliases)
{
return (new Collection(get_declared_classes()))
->filter(function ($class) {
$reflection = new ReflectionClass($class);
// Filter out internal classes and class aliases
return !$reflection->isInternal() && $reflection->getName() === $class;
})
->filter(function ($class) {
$traits = class_uses($class);
// Filter only classes with the macroable trait
return isset($traits[Macroable::class]);
})
->filter(function ($class) use ($aliases) {
$class = Str::start($class, '\\');
// Filter out aliases
return !$aliases->first(function (Alias $alias) use ($class) {
return $alias->getExtends() === $class;
});
});
}
}
+21 -1
View File
@@ -3,6 +3,7 @@
namespace Barryvdh\LaravelIdeHelper;
use Barryvdh\Reflection\DocBlock;
use Barryvdh\Reflection\DocBlock\Tag;
class Macro extends Method
{
@@ -30,7 +31,26 @@ class Macro extends Method
*/
protected function initPhpDoc($method)
{
$this->phpdoc = new DocBlock($method);
$this->phpdoc = new DocBlock('/** */');
// Add macro parameters
foreach ($method->getParameters() as $parameter) {
$type = $parameter->hasType() ? $parameter->getType()->getName() : 'mixed';
$type .= $parameter->hasType() && $parameter->getType()->allowsNull() ? '|null' : '';
$name = $parameter->isVariadic() ? '...' : '';
$name .= '$' . $parameter->getName();
$this->phpdoc->appendTag(Tag::createInstance("@param {$type} {$name}"));
}
// Add macro return type
if ($method->hasReturnType()) {
$type = $method->getReturnType()->getName();
$type .= $method->getReturnType()->allowsNull() ? '|null' : '';
$this->phpdoc->appendTag(Tag::createInstance("@return {$type}"));
}
}
/**