mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-20 02:53:11 +00:00
* Fix https://github.com/barryvdh/laravel-ide-helper/issues/627 * Fix codestyle * Init * composer fix-style * Actualize * Fix * normalize composer.json * Fix * composer fix-style * Replace NamespaceUses with UsesResolver * Add UsesResolver tests * Add MethodTest::testClassAliases * Fix code style * Get rid of laravel support helpers * Make class UsesResolver stateless, update tests for it * composer fix-style Co-authored-by: Andrii Savluk <[email protected]> Co-authored-by: laravel-ide-helper <[email protected]> Co-authored-by: SavKS <[email protected]> Co-authored-by: Taras Fomin <[email protected]>
100 lines
3.3 KiB
PHP
100 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace Barryvdh\LaravelIdeHelper;
|
|
|
|
use Barryvdh\Reflection\DocBlock;
|
|
use Barryvdh\Reflection\DocBlock\Tag;
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class Macro extends Method
|
|
{
|
|
/**
|
|
* Macro constructor.
|
|
*
|
|
* @param \ReflectionFunctionAbstract $method
|
|
* @param string $alias
|
|
* @param \ReflectionClass $class
|
|
* @param null $methodName
|
|
* @param array $interfaces
|
|
* @param array $classAliases
|
|
*/
|
|
public function __construct(
|
|
$method,
|
|
$alias,
|
|
$class,
|
|
$methodName = null,
|
|
$interfaces = [],
|
|
$classAliases = []
|
|
) {
|
|
parent::__construct($method, $alias, $class, $methodName, $interfaces, $classAliases);
|
|
}
|
|
|
|
/**
|
|
* @param \ReflectionFunctionAbstract $method
|
|
*/
|
|
protected function initPhpDoc($method)
|
|
{
|
|
$this->phpdoc = new DocBlock($method);
|
|
|
|
$this->addLocationToPhpDoc();
|
|
|
|
// Add macro parameters if they are missed in original docblock
|
|
if (!$this->phpdoc->hasTag('param')) {
|
|
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 it missed in original docblock
|
|
if ($method->hasReturnType() && !$this->phpdoc->hasTag('return')) {
|
|
$builder = EloquentBuilder::class;
|
|
$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}"));
|
|
}
|
|
}
|
|
|
|
protected function addLocationToPhpDoc()
|
|
{
|
|
if ($this->method->name === '__invoke') {
|
|
$enclosingClass = $this->method->getDeclaringClass();
|
|
} else {
|
|
$enclosingClass = $this->method->getClosureScopeClass();
|
|
}
|
|
|
|
/** @var \ReflectionMethod $enclosingMethod */
|
|
$enclosingMethod = Collection::make($enclosingClass->getMethods())
|
|
->first(function (\ReflectionMethod $method) {
|
|
return $method->getStartLine() <= $this->method->getStartLine()
|
|
&& $method->getEndLine() >= $this->method->getEndLine();
|
|
});
|
|
|
|
if ($enclosingMethod) {
|
|
$this->phpdoc->appendTag(Tag::createInstance(
|
|
'@see \\' . $enclosingClass->getName() . '::' . $enclosingMethod->getName() . '()'
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param \ReflectionFunctionAbstract $method
|
|
* @param \ReflectionClass $class
|
|
*/
|
|
protected function initClassDefinedProperties($method, \ReflectionClass $class)
|
|
{
|
|
$this->namespace = $class->getNamespaceName();
|
|
$this->declaringClassName = '\\' . ltrim($class->name, '\\');
|
|
}
|
|
}
|