Improve Method class to reduce generated filesize, and correct FQN types of phpDoc, and support case-insensitive @inheritdoc

This commit is contained in:
Thach Nguyen
2018-01-27 03:27:07 +07:00
parent e6f7ca7433
commit a44572676a
2 changed files with 367 additions and 350 deletions
+87 -77
View File
@@ -16,6 +16,7 @@ use Barryvdh\Reflection\DocBlock\Tag;
use Barryvdh\Reflection\DocBlock\Tag\ReturnTag; use Barryvdh\Reflection\DocBlock\Tag\ReturnTag;
use Barryvdh\Reflection\DocBlock\Tag\ParamTag; use Barryvdh\Reflection\DocBlock\Tag\ParamTag;
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer; use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
use Kdyby\ParseUseStatements\UseStatements;
class Method class Method
{ {
@@ -36,7 +37,7 @@ class Method
/** /**
* @param \ReflectionMethod $method * @param \ReflectionMethod $method
* @param string $alias * @param string $alias
* @param string $class * @param \ReflectionClass $class
* @param string|null $methodName * @param string|null $methodName
* @param array $interfaces * @param array $interfaces
*/ */
@@ -45,15 +46,15 @@ class Method
$this->method = $method; $this->method = $method;
$this->interfaces = $interfaces; $this->interfaces = $interfaces;
$this->name = $methodName ?: $method->name; $this->name = $methodName ?: $method->name;
$this->namespace = $method->getDeclaringClass()->getNamespaceName(); $declaringClass = $method->getDeclaringClass();
$this->namespace = $declaringClass->getNamespaceName();
//Create a DocBlock and serializer instance //Create a DocBlock and serializer instance
$this->phpdoc = new DocBlock($method, new Context($this->namespace)); $this->phpdoc = new DocBlock($method, new Context($this->namespace, static::getUseStatements($declaringClass)));
//Normalize the description and inherit the docs from parents/interfaces //Normalize the description and inherit the docs from parents/interfaces
try { try {
$this->normalizeParams($this->phpdoc); $this->normalizeReturnTags($this->phpdoc);
$this->normalizeReturn($this->phpdoc);
$this->normalizeDescription($this->phpdoc); $this->normalizeDescription($this->phpdoc);
} catch (\Exception $e) {} } catch (\Exception $e) {}
@@ -61,10 +62,9 @@ class Method
$this->getParameters($method); $this->getParameters($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));
//Reference the 'real' function in the declaringclass //Reference the 'real' function in the declaringClass
$declaringClass = $method->getDeclaringClass();
$this->declaringClassName = '\\' . ltrim($declaringClass->name, '\\'); $this->declaringClassName = '\\' . ltrim($declaringClass->name, '\\');
$this->root = '\\' . ltrim($class->getName(), '\\'); $this->root = '\\' . ltrim($class->getName(), '\\');
} }
@@ -93,12 +93,17 @@ class Method
* Get the docblock for this method * Get the docblock for this method
* *
* @param string $prefix * @param string $prefix
* @return mixed * @param bool $trim
* @return string
*/ */
public function getDocComment($prefix = "\t\t") public function getDocComment($prefix = "\t\t", $trim = false)
{ {
$serializer = new DocBlockSerializer(1, $prefix); $serializer = new DocBlockSerializer(1, $prefix);
return $serializer->getDocComment($this->phpdoc); $str = $serializer->getDocComment($this->phpdoc);
if ($trim) {
$str = preg_replace(array('/\s+$/m', '#^(\s*/\*\*[\r\n])(?:\s*\*[\r\n])+#u', '#(?:[\r\n]\s*\*)+([\r\n]\s*\*/)$#u'), array('', '$1', '$1'), $str);
}
return $str;
} }
/** /**
@@ -111,11 +116,31 @@ class Method
return $this->name; return $this->name;
} }
/**
* Checks whether the method is deprecated
*
* @return bool
*/
public function isDeprecated()
{
return $this->phpdoc->hasTag('deprecated');
}
/**
* Get the declared parameters for this method
*
* @return array
*/
public function getDocParams()
{
return $this->phpdoc->getTagsByName('param');
}
/** /**
* Get the parameters for this method * Get the parameters for this method
* *
* @param bool $implode Wether to implode the array or not * @param bool $implode Whether to implode the array or not
* @return string * @return string|array
*/ */
public function getParams($implode = true) public function getParams($implode = true)
{ {
@@ -125,8 +150,8 @@ class Method
/** /**
* Get the parameters for this method including default values * Get the parameters for this method including default values
* *
* @param bool $implode Wether to implode the array or not * @param bool $implode Whether to implode the array or not
* @return string * @return string|array
*/ */
public function getParamsWithDefault($implode = true) public function getParamsWithDefault($implode = true)
{ {
@@ -144,103 +169,75 @@ class Method
$description = $phpdoc->getText(); $description = $phpdoc->getText();
//Loop through parents/interfaces, to fill in {@inheritdoc} //Loop through parents/interfaces, to fill in {@inheritdoc}
if (strpos($description, '{@inheritdoc}') !== false) { if (stripos($description, '{@inheritdoc}') !== false && ($inheritdoc = $this->getInheritDoc($this->method))) {
$inheritdoc = $this->getInheritDoc($this->method);
$inheritDescription = $inheritdoc->getText(); $inheritDescription = $inheritdoc->getText();
$description = str_replace('{@inheritdoc}', $inheritDescription, $description); $description = str_ireplace('{@inheritdoc}', $inheritDescription, $description);
$phpdoc->setText($description); $phpdoc->setText($description);
$this->normalizeParams($inheritdoc); $this->normalizeReturnTags($inheritdoc);
$this->normalizeReturn($inheritdoc);
//Add the tags that are inherited //Add the tags that are inherited
$inheritTags = $inheritdoc->getTags(); foreach ($inheritdoc->getTags() as $tag) {
if ($inheritTags) {
/** @var Tag $tag */
foreach ($inheritTags as $tag) {
$tag->setDocBlock(); $tag->setDocBlock();
$phpdoc->appendTag($tag); $phpdoc->appendTag($tag);
} }
} }
} }
}
/**
* Normalize the parameters
*
* @param DocBlock $phpdoc
*/
protected function normalizeParams(DocBlock $phpdoc)
{
//Get the return type and adjust them for beter autocomplete
$paramTags = $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) * Normalize the return tag (make full namespace, replace interfaces)
* *
* @param DocBlock $phpdoc * @param DocBlock $phpdoc
*/ */
protected function normalizeReturn(DocBlock $phpdoc) protected function normalizeReturnTags(DocBlock $phpdoc)
{ {
//Get the return type and adjust them for beter autocomplete $this->return = null;
$returnTags = $phpdoc->getTagsByName('return');
if ($returnTags) { //Get the return type and adjust them for better autocomplete
/** @var ReturnTag $tag */ foreach ($phpdoc->getTags() as $tag) {
$tag = reset($returnTags); if ($tag instanceof ReturnTag) {
// Convert the keywords
$typeValue = static::convertKeywords($tag->getType(false));
$tag->setType($typeValue);
// Get the expanded type // Get the expanded type
$returnValue = $tag->getType(); $typeValue = $tag->getType();
// Replace the interfaces // Replace the interfaces
if (get_class($tag) === ReturnTag::class) {
foreach ($this->interfaces as $interface => $real) { foreach ($this->interfaces as $interface => $real) {
$returnValue = str_replace($interface, $real, $returnValue); $typeValue = preg_replace('/(^|\|)' . preg_quote($interface, '/') . '\b/', $real, $typeValue);
}
$this->return = $typeValue;
} }
// Set the changed content // Re-set the type
$tag->setContent($returnValue . ' ' . $tag->getDescription()); $tag->setType($typeValue);
$this->return = $returnValue; }
}else{
$this->return = null;
} }
} }
/** /**
* Convert keywwords that are incorrect. * Convert keywords that are incorrect.
* *
* @param string $string * @param string $string
* @return string * @return string
*/ */
protected function convertKeywords($string) protected static function convertKeywords($string)
{ {
$string = str_replace('\Closure', 'Closure', $string); return preg_replace(array('/(^|\|)Closure(\||$)/', '/(^|\|)dynamic(\||$)/'), array('$1\Closure$2', '$1mixed$2'), $string);
$string = str_replace('Closure', '\Closure', $string);
$string = str_replace('dynamic', 'mixed', $string);
return $string;
} }
/** /**
* Should the function return a value? * Should the function return a value?
* *
* @return bool * @return bool|int
*/ */
public function shouldReturn() public function shouldReturn()
{ {
if($this->return !== "void" && $this->method->name !== "__construct"){ if ($this->return !== 'void' && $this->method->name !== '__construct') {
return true; return isset($this->return) ? true : 1;
} }
return false; return false;
@@ -249,17 +246,19 @@ class Method
/** /**
* Get the parameters and format them correctly * Get the parameters and format them correctly
* *
* @param $method * @param \ReflectionMethod $method
* @return array * @return void
*/ */
public function getParameters($method) public function getParameters($method)
{ {
//Loop through the default values for paremeters, and make the correct output string //Loop through the default values for parameters, and make the correct output string
$params = array(); $params = array();
$paramsWithDefault = array(); $paramsWithDefault = array();
foreach ($method->getParameters() as $param) { foreach ($method->getParameters() as $param) {
$paramStr = '$' . $param->getName(); $paramStr = '$' . $param->getName();
$params[] = $paramStr; $params[] = $paramStr;
if ($param->isOptional()) { if ($param->isOptional()) {
$default = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null; $default = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null;
if (is_bool($default)) { if (is_bool($default)) {
@@ -275,6 +274,7 @@ class Method
} }
$paramStr .= " = $default"; $paramStr .= " = $default";
} }
$paramsWithDefault[] = $paramStr; $paramsWithDefault[] = $paramStr;
} }
@@ -284,7 +284,7 @@ class Method
/** /**
* @param \ReflectionMethod $reflectionMethod * @param \ReflectionMethod $reflectionMethod
* @return DocBlock * @return DocBlock|null
*/ */
protected function getInheritDoc($reflectionMethod) protected function getInheritDoc($reflectionMethod)
{ {
@@ -298,14 +298,24 @@ class Method
} }
if ($method) { if ($method) {
$namespace = $method->getDeclaringClass()->getNamespaceName(); $namespace = $method->getDeclaringClass()->getNamespaceName();
$phpdoc = new DocBlock($method, new Context($namespace)); $phpdoc = new DocBlock($method, new Context($namespace, static::getUseStatements($method->getDeclaringClass())));
if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) { if (stripos($phpdoc->getText(), '{@inheritdoc}') !== false) {
//Not at the end yet, try another parent/interface.. //Not at the end yet, try another parent/interface..
return $this->getInheritDoc($method); return $this->getInheritDoc($method);
} else { } else {
return $phpdoc; return $phpdoc;
} }
} }
return null;
}
protected static function getUseStatements(\ReflectionClass $class)
{
try {
return UseStatements::getUseStatements($class);
} catch (\Exception $e) {
return array();
}
} }
} }
+17 -10
View File
@@ -2,38 +2,45 @@
/** /**
* An helper file for Laravel 4, to provide autocomplete information to your IDE * An helper file for Laravel 4, to provide autocomplete information to your IDE
* Generated for Laravel <?= $version ?> on <?= date("Y-m-d") ?>. * Generated for Laravel <?= $version ?> on <?= date('Y-m-d') ?>.
* *
* @author Barry vd. Heuvel <[email protected]> * @author Barry vd. Heuvel <[email protected]>
* @see https://github.com/barryvdh/laravel-ide-helper * @see https://github.com/barryvdh/laravel-ide-helper
*/ */
<?php foreach($namespaces as $namespace => $aliases): ?> <?php foreach ($namespaces as $namespace => $aliases/* @var \Barryvdh\LaravelIdeHelper\Alias[] $aliases */): ?>
namespace <?= $namespace == '__root' ? '' : $namespace ?>{ namespace <?= $namespace == '__root' ? '' : $namespace ?>{
<?php if ($namespace == '__root'): ?> <?php if ($namespace == '__root'): ?>
exit("This file should not be included, only analyzed by your IDE"); exit('This file should not be included, only analyzed by your IDE');
<?= $helpers ?> <?= $helpers ?>
<?php endif; ?> <?php endif; ?>
<?php foreach ($aliases as $alias): ?> <?php foreach ($aliases as $alias): ?>
<?php if (($cBase = class_basename($alias->getExtends())) != $alias->getExtends() && $cBase != $alias->getShortName()): ?>
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
<?php endif; ?>
<?= $alias->getClassType() ?> <?= $alias->getShortName() ?> <?= $alias->getExtends() ? 'extends ' . $alias->getExtends() : '' ?>{ <?= $alias->getClassType() ?> <?= $alias->getShortName() ?> <?= $alias->getExtends() ? 'extends ' . $alias->getExtends() : '' ?>{
<?php foreach ($alias->getMethods() as $method): ?> <?php foreach ($alias->getMethods() as $method): ?>
<?= trim($method->getDocComment(' ')) ?> <?php if (($nParams = count($method->getDocParams())) > count($mParams = $method->getParamsWithDefault(false))): ?>
/** @noinspection PhpDocSignatureInspection */
public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>){<?php if($method->getDeclaringClass() !== $method->getRoot()): ?>
//Method inherited from <?= $method->getDeclaringClass() ?>
<?php endif; ?> <?php endif; ?>
<?php if ($docComment = str_replace("/**\n\t\t */", '', trim($method->getDocComment("\t\t", true)))): ?>
<?= $docComment ?>
<?php endif; ?>
public static function <?= $method->getName() ?>(<?= $nParams < count($mParams) ? '/** @noinspection PhpDocSignatureInspection */' . implode(', /** @noinspection PhpDocSignatureInspection */', $mParams) : $method->getParamsWithDefault() ?>){
<?php if ($method->getDeclaringClass() !== $method->getRoot()): ?>
//Method inherited from <?= $method->getDeclaringClass() ?>
<?php endif; ?>
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection,PhpDynamicAsStaticMethodCallInspection<?= ($method->shouldReturn() === 1 ? ',PhpVoidFunctionResultUsedInspection' : '') . ($method->isDeprecated() ? ',PhpDeprecationInspection' : '') ?> */
<?= $method->shouldReturn() ? 'return ' : '' ?><?= $method->getRoot() ?>::<?= $method->getName() ?>(<?= $method->getParams() ?>); <?= $method->shouldReturn() ? 'return ' : '' ?><?= $method->getRoot() ?>::<?= $method->getName() ?>(<?= $method->getParams() ?>);
} }
<?php endforeach; ?> <?php endforeach; ?>
} }
<?php endforeach; ?> <?php endforeach; ?>
} }
<?php endforeach; ?> <?php endforeach; ?>