mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
Expand types to use FQN
This commit is contained in:
+73
-42
@@ -13,6 +13,8 @@ namespace Barryvdh\LaravelIdeHelper;
|
|||||||
use phpDocumentor\Reflection\DocBlock;
|
use phpDocumentor\Reflection\DocBlock;
|
||||||
use phpDocumentor\Reflection\DocBlock\Context;
|
use phpDocumentor\Reflection\DocBlock\Context;
|
||||||
use phpDocumentor\Reflection\DocBlock\Tag;
|
use phpDocumentor\Reflection\DocBlock\Tag;
|
||||||
|
use phpDocumentor\Reflection\DocBlock\Tag\ReturnTag;
|
||||||
|
use phpDocumentor\Reflection\DocBlock\Tag\ParamTag;
|
||||||
use phpDocumentor\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
use phpDocumentor\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
||||||
|
|
||||||
class Method
|
class Method
|
||||||
@@ -20,12 +22,16 @@ class Method
|
|||||||
/** @var \phpDocumentor\Reflection\DocBlock */
|
/** @var \phpDocumentor\Reflection\DocBlock */
|
||||||
protected $phpdoc;
|
protected $phpdoc;
|
||||||
|
|
||||||
|
/** @var \ReflectionMethod */
|
||||||
|
protected $method;
|
||||||
|
|
||||||
protected $output = '';
|
protected $output = '';
|
||||||
protected $name;
|
protected $name;
|
||||||
protected $namespace;
|
protected $namespace;
|
||||||
protected $params = array();
|
protected $params = array();
|
||||||
protected $params_with_default = array();
|
protected $params_with_default = array();
|
||||||
protected $interfaces = array();
|
protected $interfaces = array();
|
||||||
|
protected $return = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \ReflectionMethod $method
|
* @param \ReflectionMethod $method
|
||||||
@@ -36,6 +42,7 @@ class Method
|
|||||||
*/
|
*/
|
||||||
public function __construct(\ReflectionMethod $method, $alias, $class, $methodName = null, $interfaces = array())
|
public function __construct(\ReflectionMethod $method, $alias, $class, $methodName = null, $interfaces = array())
|
||||||
{
|
{
|
||||||
|
$this->method = $method;
|
||||||
$this->interfaces = $interfaces;
|
$this->interfaces = $interfaces;
|
||||||
$this->name = $methodName ?: $method->name;
|
$this->name = $methodName ?: $method->name;
|
||||||
$this->namespace = $method->getDeclaringClass()->getNamespaceName();
|
$this->namespace = $method->getDeclaringClass()->getNamespaceName();
|
||||||
@@ -45,10 +52,10 @@ class Method
|
|||||||
|
|
||||||
//Normalize the description and inherit the docs from parents/interfaces
|
//Normalize the description and inherit the docs from parents/interfaces
|
||||||
try {
|
try {
|
||||||
$this->normalizeDescription($method);
|
$this->normalizeDescription();
|
||||||
} catch (\Exception $e) {
|
$this->normalizeParams();
|
||||||
$this->error("Cannot normalize method $alias::{$this->name}..");
|
$this->normalizeReturn();
|
||||||
}
|
} catch (\Exception $e) {}
|
||||||
|
|
||||||
//Get the parameters, including formatted default values
|
//Get the parameters, including formatted default values
|
||||||
$this->getParameters($method);
|
$this->getParameters($method);
|
||||||
@@ -56,11 +63,6 @@ class Method
|
|||||||
//Make the method static
|
//Make the method static
|
||||||
$this->phpdoc->appendTag(Tag::createInstance('@static', $this->phpdoc));
|
$this->phpdoc->appendTag(Tag::createInstance('@static', $this->phpdoc));
|
||||||
|
|
||||||
//Correct the return values
|
|
||||||
$returnValue = $this->getReturn();
|
|
||||||
//Only return when not a constructor and not void.
|
|
||||||
$this->return = ($returnValue && $returnValue !== "void" && $method->name !== "__construct");
|
|
||||||
|
|
||||||
//Reference the 'real' function in the declaringclass
|
//Reference the 'real' function in the declaringclass
|
||||||
$declaringClass = $method->getDeclaringClass();
|
$declaringClass = $method->getDeclaringClass();
|
||||||
$this->declaringClassName = '\\' . ltrim($declaringClass->name, '\\');
|
$this->declaringClassName = '\\' . ltrim($declaringClass->name, '\\');
|
||||||
@@ -77,16 +79,6 @@ class Method
|
|||||||
return $this->declaringClassName;
|
return $this->declaringClassName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Should the function return a value?
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function shouldReturn()
|
|
||||||
{
|
|
||||||
return $this->return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the class from which this function would be called
|
* Return the class from which this function would be called
|
||||||
*
|
*
|
||||||
@@ -144,16 +136,15 @@ class Method
|
|||||||
/**
|
/**
|
||||||
* Get the description and get the inherited docs.
|
* Get the description and get the inherited docs.
|
||||||
*
|
*
|
||||||
* @param $method
|
|
||||||
*/
|
*/
|
||||||
protected function normalizeDescription($method)
|
protected function normalizeDescription()
|
||||||
{
|
{
|
||||||
//Get the short + long description from the DocBlock
|
//Get the short + long description from the DocBlock
|
||||||
$description = $this->phpdoc->getText();
|
$description = $this->phpdoc->getText();
|
||||||
|
|
||||||
//Loop through parents/interfaces, to fill in {@inheritdoc}
|
//Loop through parents/interfaces, to fill in {@inheritdoc}
|
||||||
if (strpos($description, '{@inheritdoc}') !== false) {
|
if (strpos($description, '{@inheritdoc}') !== false) {
|
||||||
$inheritdoc = $this->getInheritDoc($method);
|
$inheritdoc = $this->getInheritDoc($this->method);
|
||||||
$inheritDescription = $inheritdoc->getText();
|
$inheritDescription = $inheritdoc->getText();
|
||||||
|
|
||||||
$description = str_replace('{@inheritdoc}', $inheritDescription, $description);
|
$description = str_replace('{@inheritdoc}', $inheritDescription, $description);
|
||||||
@@ -171,28 +162,79 @@ class Method
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make some changes to the return types, if needed.
|
* Normalize the parameters
|
||||||
*
|
|
||||||
* @return string|null
|
|
||||||
*/
|
*/
|
||||||
protected function getReturn()
|
protected function normalizeParams()
|
||||||
|
{
|
||||||
|
//Get the return type and adjust them for beter autocomplete
|
||||||
|
$paramTags = $this->phpdoc->getTagsByName('param');
|
||||||
|
if ($paramTags) {
|
||||||
|
/** @var ParamTag $tag */
|
||||||
|
foreach($paramTags as $tag){
|
||||||
|
// Convert the keywords
|
||||||
|
$content = $this->convertKeywords($tag->getContent());
|
||||||
|
$tag->setContent($content);
|
||||||
|
|
||||||
|
// Get the expanded type and re-set the content
|
||||||
|
$content = $tag->getType() . ' ' . $tag->getVariableName() . ' ' . $tag->getDescription();
|
||||||
|
$tag->setContent(trim($content));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize the return tag (make full namespace, replace interfaces)
|
||||||
|
*/
|
||||||
|
protected function normalizeReturn()
|
||||||
{
|
{
|
||||||
//Get the return type and adjust them for beter autocomplete
|
//Get the return type and adjust them for beter autocomplete
|
||||||
$returnTags = $this->phpdoc->getTagsByName('return');
|
$returnTags = $this->phpdoc->getTagsByName('return');
|
||||||
if ($returnTags) {
|
if ($returnTags) {
|
||||||
/** @var Tag $tag */
|
/** @var ReturnTag $tag */
|
||||||
$tag = reset($returnTags);
|
$tag = reset($returnTags);
|
||||||
|
// Get the expanded type
|
||||||
$returnValue = $tag->getType();
|
$returnValue = $tag->getType();
|
||||||
|
|
||||||
|
// Replace the interfaces
|
||||||
foreach($this->interfaces as $interface => $real){
|
foreach($this->interfaces as $interface => $real){
|
||||||
$returnValue = str_replace($interface, $real, $returnValue);
|
$returnValue = str_replace($interface, $real, $returnValue);
|
||||||
}
|
}
|
||||||
$tag->setContent($returnValue);
|
|
||||||
} else {
|
// Set the changed content
|
||||||
$returnValue = null;
|
$tag->setContent($returnValue . ' ' . $tag->getDescription());
|
||||||
|
$this->return = $returnValue;
|
||||||
|
}else{
|
||||||
|
$this->return = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert keywwords that are incorrect.
|
||||||
|
*
|
||||||
|
* @param string $string
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function convertKeywords($string)
|
||||||
|
{
|
||||||
|
$string = str_replace('\Closure', 'Closure', $string);
|
||||||
|
$string = str_replace('Closure', '\Closure', $string);
|
||||||
|
$string = str_replace('dynamic', 'mixed', $string);
|
||||||
|
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should the function return a value?
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function shouldReturn()
|
||||||
|
{
|
||||||
|
if($this->return !== "void" && $this->method->name !== "__construct"){
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $returnValue;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -255,15 +297,4 @@ class Method
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Output an error.
|
|
||||||
*
|
|
||||||
* @param string $string
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
protected function error($string)
|
|
||||||
{
|
|
||||||
echo $string . "\r\n";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user