mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
Alias will grab macros from \Illuminate\Database\Eloquent\Builder too (#1118)
* `Alias` will grab macros from `\Illuminate\Database\Eloquent\Builder` too. * `@return` will contains `|static` for Eloquent Builder macros which return Eloquent Builder instance. * `Macro` tests. * `Alias::detectMethods()` tests. * Mock classes rename.
This commit is contained in:
+3
-1
@@ -17,6 +17,7 @@ use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
|||||||
use Barryvdh\Reflection\DocBlock\Tag\MethodTag;
|
use Barryvdh\Reflection\DocBlock\Tag\MethodTag;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Config\Repository as ConfigRepository;
|
use Illuminate\Config\Repository as ConfigRepository;
|
||||||
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||||
use Illuminate\Support\Facades\Facade;
|
use Illuminate\Support\Facades\Facade;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
|
|
||||||
@@ -368,8 +369,9 @@ class Alias
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if the class is macroable
|
// Check if the class is macroable
|
||||||
|
// (Eloquent\Builder is also macroable but doesn't use Macroable trait)
|
||||||
$traits = collect($reflection->getTraitNames());
|
$traits = collect($reflection->getTraitNames());
|
||||||
if ($traits->contains('Illuminate\Support\Traits\Macroable')) {
|
if ($traits->contains('Illuminate\Support\Traits\Macroable') || $class === EloquentBuilder::class) {
|
||||||
$properties = $reflection->getStaticProperties();
|
$properties = $reflection->getStaticProperties();
|
||||||
$macros = isset($properties['macros']) ? $properties['macros'] : [];
|
$macros = isset($properties['macros']) ? $properties['macros'] : [];
|
||||||
foreach ($macros as $macro_name => $macro_func) {
|
foreach ($macros as $macro_name => $macro_func) {
|
||||||
|
|||||||
+7
-2
@@ -4,6 +4,7 @@ namespace Barryvdh\LaravelIdeHelper;
|
|||||||
|
|
||||||
use Barryvdh\Reflection\DocBlock;
|
use Barryvdh\Reflection\DocBlock;
|
||||||
use Barryvdh\Reflection\DocBlock\Tag;
|
use Barryvdh\Reflection\DocBlock\Tag;
|
||||||
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
class Macro extends Method
|
class Macro extends Method
|
||||||
@@ -49,8 +50,12 @@ class Macro extends Method
|
|||||||
|
|
||||||
// Add macro return type
|
// Add macro return type
|
||||||
if ($method->hasReturnType()) {
|
if ($method->hasReturnType()) {
|
||||||
$type = $method->getReturnType()->getName();
|
$builder = EloquentBuilder::class;
|
||||||
$type .= $method->getReturnType()->allowsNull() ? '|null' : '';
|
$return = $method->getReturnType();
|
||||||
|
|
||||||
|
$type = $return->getName();
|
||||||
|
$type .= $this->root === "\\{$builder}" && $return->getName() === $builder ? '|static' : '';
|
||||||
|
$type .= $return->allowsNull() ? '|null' : '';
|
||||||
|
|
||||||
$this->phpdoc->appendTag(Tag::createInstance("@return {$type}"));
|
$this->phpdoc->appendTag(Tag::createInstance("@return {$type}"));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,105 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Alias;
|
||||||
|
use Barryvdh\LaravelIdeHelper\Macro;
|
||||||
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||||
|
use Illuminate\Database\Query\Builder;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @coversDefaultClass \Barryvdh\LaravelIdeHelper\Alias
|
||||||
|
*/
|
||||||
|
class AliasTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers ::detectMethods
|
||||||
|
*/
|
||||||
|
public function testDetectMethodsMacroableMacros(): void
|
||||||
|
{
|
||||||
|
// Mock
|
||||||
|
$macro = __FUNCTION__;
|
||||||
|
$alias = new AliasMock();
|
||||||
|
|
||||||
|
// Macros
|
||||||
|
Builder::macro(
|
||||||
|
$macro,
|
||||||
|
function () {
|
||||||
|
// empty
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Prepare
|
||||||
|
$alias->setClasses([Builder::class]);
|
||||||
|
$alias->detectMethods();
|
||||||
|
|
||||||
|
// Test
|
||||||
|
$this->assertNotNull($this->getAliasMacro($alias, Builder::class, $macro));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::detectMethods
|
||||||
|
*/
|
||||||
|
public function testDetectMethodsEloquentBuilderMacros(): void
|
||||||
|
{
|
||||||
|
// Mock
|
||||||
|
$macro = __FUNCTION__;
|
||||||
|
$alias = new AliasMock();
|
||||||
|
|
||||||
|
// Macros
|
||||||
|
EloquentBuilder::macro(
|
||||||
|
$macro,
|
||||||
|
function () {
|
||||||
|
// empty
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Prepare
|
||||||
|
$alias->setClasses([EloquentBuilder::class]);
|
||||||
|
$alias->detectMethods();
|
||||||
|
|
||||||
|
// Test
|
||||||
|
$this->assertNotNull($this->getAliasMacro($alias, EloquentBuilder::class, $macro));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getAliasMacro(Alias $alias, string $class, string $method): ?Macro
|
||||||
|
{
|
||||||
|
return Arr::first(
|
||||||
|
$alias->getMethods(),
|
||||||
|
function ($macro) use ($class, $method) {
|
||||||
|
return $macro instanceof Macro
|
||||||
|
&& $macro->getDeclaringClass() === "\\{$class}"
|
||||||
|
&& $macro->getName() === $method;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @noinspection PhpMultipleClassesDeclarationsInOneFile
|
||||||
|
*/
|
||||||
|
class AliasMock extends Alias
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
// no need to call parent
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string[] $classes
|
||||||
|
*/
|
||||||
|
public function setClasses(array $classes)
|
||||||
|
{
|
||||||
|
$this->classes = $classes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function detectMethods()
|
||||||
|
{
|
||||||
|
parent::detectMethods();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Macro;
|
||||||
|
use Barryvdh\Reflection\DocBlock;
|
||||||
|
use Barryvdh\Reflection\DocBlock\Tag;
|
||||||
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionFunction;
|
||||||
|
use ReflectionFunctionAbstract;
|
||||||
|
|
||||||
|
use function array_map;
|
||||||
|
use function implode;
|
||||||
|
|
||||||
|
use const PHP_EOL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @coversDefaultClass \Barryvdh\LaravelIdeHelper\Macro
|
||||||
|
*/
|
||||||
|
class MacroTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers ::initPhpDoc
|
||||||
|
* @throws \ReflectionException
|
||||||
|
*/
|
||||||
|
public function testInitPhpDocEloquentBuilderHasStaticInReturnType(): void
|
||||||
|
{
|
||||||
|
$class = new ReflectionClass(EloquentBuilder::class);
|
||||||
|
$phpdoc = (new MacroMock())->getPhpDoc(
|
||||||
|
new ReflectionFunction(
|
||||||
|
function (): EloquentBuilder {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
),
|
||||||
|
$class
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertNotNull($phpdoc);
|
||||||
|
$this->assertEquals(
|
||||||
|
'@return \Illuminate\Database\Eloquent\Builder|static',
|
||||||
|
$this->tagsToString($phpdoc, 'return')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tagsToString(DocBlock $docBlock, string $name)
|
||||||
|
{
|
||||||
|
$tags = $docBlock->getTagsByName($name);
|
||||||
|
$tags = array_map(
|
||||||
|
function (Tag $tag) {
|
||||||
|
return trim((string)$tag);
|
||||||
|
},
|
||||||
|
$tags
|
||||||
|
);
|
||||||
|
$tags = implode(PHP_EOL, $tags);
|
||||||
|
|
||||||
|
return $tags;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @noinspection PhpMultipleClassesDeclarationsInOneFile
|
||||||
|
*/
|
||||||
|
class MacroMock extends Macro
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
// no need to call parent
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPhpDoc(ReflectionFunctionAbstract $method, ReflectionClass $class = null): DocBlock
|
||||||
|
{
|
||||||
|
return (new Macro($method, '', $class ?? $method->getClosureScopeClass()))->phpdoc;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user