mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 18:13:11 +00:00
Add comments like table columns. (#1168)
* add comment with @method & @property-read & property-write in models * add readme comment * add readme comment * add comment tag unit-test * add comment tag unit-test * both a getter and a setter has a @comment test * Update README.md improve README Co-authored-by: Markus Podar <[email protected]> * Update CHANGELOG.md Co-authored-by: Markus Podar <[email protected]>
This commit is contained in:
co-authored by
Markus Podar
parent
c5e18beff1
commit
c5c9b2b951
@@ -527,7 +527,8 @@ class ModelsCommand extends Command
|
||||
$reflection = new \ReflectionMethod($model, $method);
|
||||
$type = $this->getReturnType($reflection);
|
||||
$type = $this->getTypeInModel($model, $type);
|
||||
$this->setProperty($name, $type, true, null);
|
||||
$comment = $this->getCommentFromDocBlock($reflection);
|
||||
$this->setProperty($name, $type, true, null, $comment);
|
||||
}
|
||||
} elseif (
|
||||
Str::startsWith($method, 'set') && Str::endsWith(
|
||||
@@ -538,13 +539,16 @@ class ModelsCommand extends Command
|
||||
//Magic set<name>Attribute
|
||||
$name = Str::snake(substr($method, 3, -9));
|
||||
if (!empty($name)) {
|
||||
$this->setProperty($name, null, null, true);
|
||||
$reflection = new \ReflectionMethod($model, $method);
|
||||
$comment = $this->getCommentFromDocBlock($reflection);
|
||||
$this->setProperty($name, null, null, true, $comment);
|
||||
}
|
||||
} elseif (Str::startsWith($method, 'scope') && $method !== 'scopeQuery') {
|
||||
//Magic set<name>Attribute
|
||||
$name = Str::camel(substr($method, 5));
|
||||
if (!empty($name)) {
|
||||
$reflection = new \ReflectionMethod($model, $method);
|
||||
$comment = $this->getCommentFromDocBlock($reflection);
|
||||
$args = $this->getParameters($reflection);
|
||||
//Remove the first ($query) argument
|
||||
array_shift($args);
|
||||
@@ -556,7 +560,7 @@ class ModelsCommand extends Command
|
||||
$reflection->getDeclaringClass(),
|
||||
$reflection->getDeclaringClass()->getName()
|
||||
);
|
||||
$this->setMethod($name, $builder . '|' . $modelName, $args);
|
||||
$this->setMethod($name, $builder . '|' . $modelName, $args, $comment);
|
||||
}
|
||||
} elseif (in_array($method, ['query', 'newQuery', 'newModelQuery'])) {
|
||||
$builder = $this->getClassNameInDestinationFile($model, get_class($model->newModelQuery()));
|
||||
@@ -608,6 +612,7 @@ class ModelsCommand extends Command
|
||||
continue;
|
||||
}
|
||||
|
||||
$comment = $this->getCommentFromDocBlock($reflection);
|
||||
// Adding constraints requires reading model properties which
|
||||
// can cause errors. Since we don't need constraints we can
|
||||
// disable them when we fetch the relation to avoid errors.
|
||||
@@ -639,7 +644,8 @@ class ModelsCommand extends Command
|
||||
$method,
|
||||
$collectionClassNameInModel . '|' . $relatedModel . '[]',
|
||||
true,
|
||||
null
|
||||
null,
|
||||
$comment
|
||||
);
|
||||
if ($this->write_model_relation_count_properties) {
|
||||
$this->setProperty(
|
||||
@@ -647,6 +653,7 @@ class ModelsCommand extends Command
|
||||
'int|null',
|
||||
true,
|
||||
false
|
||||
// What kind of comments should be added to the relation count here?
|
||||
);
|
||||
}
|
||||
} elseif ($relation === 'morphTo') {
|
||||
@@ -655,7 +662,8 @@ class ModelsCommand extends Command
|
||||
$method,
|
||||
$this->getClassNameInDestinationFile($model, Model::class) . '|\Eloquent',
|
||||
true,
|
||||
null
|
||||
null,
|
||||
$comment
|
||||
);
|
||||
} else {
|
||||
//Single model is returned
|
||||
@@ -664,7 +672,7 @@ class ModelsCommand extends Command
|
||||
$relatedModel,
|
||||
true,
|
||||
null,
|
||||
'',
|
||||
$comment,
|
||||
$this->isRelationNullable($relation, $relationObj)
|
||||
);
|
||||
}
|
||||
@@ -737,7 +745,7 @@ class ModelsCommand extends Command
|
||||
}
|
||||
}
|
||||
|
||||
protected function setMethod($name, $type = '', $arguments = [])
|
||||
protected function setMethod($name, $type = '', $arguments = [], $comment = '')
|
||||
{
|
||||
$methods = array_change_key_case($this->methods, CASE_LOWER);
|
||||
|
||||
@@ -745,6 +753,7 @@ class ModelsCommand extends Command
|
||||
$this->methods[$name] = [];
|
||||
$this->methods[$name]['type'] = $type;
|
||||
$this->methods[$name]['arguments'] = $arguments;
|
||||
$this->methods[$name]['comment'] = $comment;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -820,7 +829,11 @@ class ModelsCommand extends Command
|
||||
continue;
|
||||
}
|
||||
$arguments = implode(', ', $method['arguments']);
|
||||
$tag = Tag::createInstance("@method static {$method['type']} {$name}({$arguments})", $phpdoc);
|
||||
$tagLine = "@method static {$method['type']} {$name}({$arguments})";
|
||||
if ($method['comment'] !== '') {
|
||||
$tagLine .= " {$method['comment']}";
|
||||
}
|
||||
$tag = Tag::createInstance($tagLine, $phpdoc);
|
||||
$phpdoc->appendTag($tag);
|
||||
}
|
||||
|
||||
@@ -980,6 +993,30 @@ class ModelsCommand extends Command
|
||||
return $this->getReturnTypeFromReflection($reflection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get method comment based on it DocBlock comment
|
||||
*
|
||||
* @param \ReflectionMethod $reflection
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
protected function getCommentFromDocBlock(\ReflectionMethod $reflection)
|
||||
{
|
||||
$phpDocContext = (new ContextFactory())->createFromReflector($reflection);
|
||||
$context = new Context(
|
||||
$phpDocContext->getNamespace(),
|
||||
$phpDocContext->getNamespaceAliases()
|
||||
);
|
||||
$comment = '';
|
||||
$phpdoc = new DocBlock($reflection, $context);
|
||||
|
||||
if ($phpdoc->hasTag('comment')) {
|
||||
$comment = $phpdoc->getTagsByName('comment')[0]->getContent();
|
||||
}
|
||||
|
||||
return $comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get method return type based on it DocBlock comment
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user