mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 10:07:12 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aedd2ee127 | ||
|
|
007a96ef3f | ||
|
|
63b1026fc3 | ||
|
|
d042c6ea8e | ||
|
|
ae0dce2abd | ||
|
|
7f17da57b2 | ||
|
|
0f22941e51 | ||
|
|
f7f0e025f3 |
@@ -54,19 +54,30 @@ If you don't want to write your properties yourself, you can use the command `id
|
|||||||
phpDocs, based on table columns, relations and getters/setters. Still in beta, so please provide feedback if you want.
|
phpDocs, based on table columns, relations and getters/setters. Still in beta, so please provide feedback if you want.
|
||||||
Docs are written to a phpfile (_ide_helper_models.php) in the root of the project, so you can move the docs to the real model.
|
Docs are written to a phpfile (_ide_helper_models.php) in the root of the project, so you can move the docs to the real model.
|
||||||
|
|
||||||
You can now also write the comments directly to your Model file, using the -W argument. Please make sure to backup your models, before writing the info.
|
You can now also write the comments directly to your Model file, using the `--write (-W)` option. Please make sure to backup your models, before writing the info.
|
||||||
It should keep the existing comments and only append new properties/methods. The existing phpdoc is replaced, or added if not found.
|
It should keep the existing comments and only append new properties/methods. The existing phpdoc is replaced, or added if not found.
|
||||||
|
With the `--reset (-R)` option, the existing phpdocs are ignored, only the newly found columns/relations are saved as phpdocs.
|
||||||
|
|
||||||
|
php artisan ide-helper:models Post
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An Eloquent Model: 'Post'
|
||||||
|
*
|
||||||
|
* @property integer $id
|
||||||
|
* @property integer $author_id
|
||||||
|
* @property string $title
|
||||||
|
* @property string $text
|
||||||
|
* @property \Carbon\Carbon $created_at
|
||||||
|
* @property \Carbon\Carbon $updated_at
|
||||||
|
* @property-read \User $author
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Comment[] $comments
|
||||||
|
*/
|
||||||
|
|
||||||
For now, only models in app/models are scanned. The optional argument tells what models to use (also outside app/models).
|
For now, only models in app/models are scanned. The optional argument tells what models to use (also outside app/models).
|
||||||
`php artisan ide-helper:models Post,User`
|
|
||||||
|
|
||||||
Note: With namespaces, uses \\ instead of \
|
php artisan ide-helper:models Post,User
|
||||||
`php artisan ide-helper:models API\\User`
|
|
||||||
|
|
||||||
This creates a file with the phpDocs for each Model. You should check and change them to be more accurate.
|
|
||||||
Also, all relations are Eloquent|Eloquent[] by default, you can change them to the actual Model.
|
|
||||||
After copying the phpdocs to your model, you can clear the file, so your IDE only uses the real source.
|
|
||||||
|
|
||||||
|
Note: With namespaces, uses `\\` instead of `\`: `php artisan ide-helper:models API\\User`
|
||||||
|
|
||||||
### License
|
### License
|
||||||
|
|
||||||
|
|||||||
@@ -157,6 +157,12 @@ exit('Only to be used as an helper for your IDE');\n\n";
|
|||||||
$addOutput = ($root !== $facade);
|
$addOutput = ($root !== $facade);
|
||||||
$output .= $this->getMethods($root, $alias, $usedMethods, $addOutput);
|
$output .= $this->getMethods($root, $alias, $usedMethods, $addOutput);
|
||||||
|
|
||||||
|
|
||||||
|
$driver = $this->getDriver($alias);
|
||||||
|
if($driver){
|
||||||
|
$output .= $this->getMethods($driver, $alias, $usedMethods);
|
||||||
|
}
|
||||||
|
|
||||||
//Add extra methods, from other classes (magic static calls)
|
//Add extra methods, from other classes (magic static calls)
|
||||||
if(array_key_exists($alias, $this->extra)){
|
if(array_key_exists($alias, $this->extra)){
|
||||||
$output .= $this->getMethods($this->extra[$alias], $alias, $usedMethods);
|
$output .= $this->getMethods($this->extra[$alias], $alias, $usedMethods);
|
||||||
@@ -183,6 +189,12 @@ exit('Only to be used as an helper for your IDE');\n\n";
|
|||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the real root of a facade
|
||||||
|
*
|
||||||
|
* @param $facade
|
||||||
|
* @return bool|string
|
||||||
|
*/
|
||||||
protected function getRoot($facade){
|
protected function getRoot($facade){
|
||||||
try{
|
try{
|
||||||
//If possible, get the facade root
|
//If possible, get the facade root
|
||||||
@@ -210,6 +222,32 @@ exit('Only to be used as an helper for your IDE');\n\n";
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getDriver($alias){
|
||||||
|
if($alias == "Auth"){
|
||||||
|
$driver = \Auth::driver();
|
||||||
|
}elseif($alias == "DB"){
|
||||||
|
$driver = \DB::connection();
|
||||||
|
}elseif($alias == "Cache"){
|
||||||
|
$driver = \Cache::driver();
|
||||||
|
}elseif($alias == "Queue"){
|
||||||
|
$driver = \Queue::connection();
|
||||||
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return get_class($driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the methods for one or multiple classes.
|
||||||
|
*
|
||||||
|
* @param $classes
|
||||||
|
* @param $alias
|
||||||
|
* @param $usedMethods
|
||||||
|
* @param bool $addOutput
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
protected function getMethods($classes, $alias, &$usedMethods, $addOutput = true){
|
protected function getMethods($classes, $alias, &$usedMethods, $addOutput = true){
|
||||||
if(!is_array($classes)){
|
if(!is_array($classes)){
|
||||||
$classes = array($classes);
|
$classes = array($classes);
|
||||||
@@ -221,14 +259,14 @@ exit('Only to be used as an helper for your IDE');\n\n";
|
|||||||
}
|
}
|
||||||
$reflection = new \ReflectionClass($class);
|
$reflection = new \ReflectionClass($class);
|
||||||
|
|
||||||
$methods = $reflection->getMethods();
|
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
|
||||||
if($methods)
|
if($methods)
|
||||||
{
|
{
|
||||||
foreach ($methods as $method)
|
foreach ($methods as $method)
|
||||||
{
|
{
|
||||||
if(!in_array($method->name, $usedMethods)){
|
if(!in_array($method->name, $usedMethods)){
|
||||||
if($method->isPublic() && $addOutput){
|
if( $addOutput){
|
||||||
$output .= $this->parseMethod($method, $alias);
|
$output .= $this->parseMethod($method, $alias, $reflection);
|
||||||
}
|
}
|
||||||
$usedMethods[] = $method->name;
|
$usedMethods[] = $method->name;
|
||||||
}
|
}
|
||||||
@@ -237,15 +275,13 @@ exit('Only to be used as an helper for your IDE');\n\n";
|
|||||||
}
|
}
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \ReflectionMethod $method
|
* @param \ReflectionMethod $method
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
* @param string $root
|
|
||||||
* @param bool $static
|
|
||||||
* @param string $rootParam
|
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function parseMethod($method, $alias){
|
protected function parseMethod($method, $alias, $class){
|
||||||
$output = '';
|
$output = '';
|
||||||
|
|
||||||
// Don't add the __clone() functions
|
// Don't add the __clone() functions
|
||||||
@@ -278,11 +314,29 @@ exit('Only to be used as an helper for your IDE');\n\n";
|
|||||||
$output .= "){\r\n";
|
$output .= "){\r\n";
|
||||||
|
|
||||||
//Only return when not a constructor and not void.
|
//Only return when not a constructor and not void.
|
||||||
$return = ($returnValue && $returnValue !== "void" && $method->name !== "__construct") ? 'return' : '';
|
$return = ($returnValue && $returnValue !== "void" && $method->name !== "__construct") ? 'return ' : '';
|
||||||
|
|
||||||
//Reference the 'real' function in the declaringclass
|
//Reference the 'real' function in the declaringclass
|
||||||
$root = $method->getDeclaringClass()->getName();
|
$declaringClass = $method->getDeclaringClass();
|
||||||
$output .= "\t\t$return \\$root::";
|
if($declaringClass->isAbstract()){
|
||||||
|
$declaringClass = $class;
|
||||||
|
}
|
||||||
|
|
||||||
|
$constructor = $declaringClass->getConstructor();
|
||||||
|
$root = $declaringClass->getName();
|
||||||
|
|
||||||
|
|
||||||
|
$output .= "\t\t\$root = new \\$root(";
|
||||||
|
if(!is_null($constructor)){
|
||||||
|
$constructorParams = array();
|
||||||
|
for($i=0;$i<$constructor->getNumberOfRequiredParameters();$i++){
|
||||||
|
$constructorParams[] = 'null';
|
||||||
|
}
|
||||||
|
$output .= implode($constructorParams, ',');
|
||||||
|
}
|
||||||
|
$output .= ");\n";
|
||||||
|
|
||||||
|
$output .= "\t\t$return\$root->";
|
||||||
|
|
||||||
//Write the default parameters in the function call
|
//Write the default parameters in the function call
|
||||||
$output .= $method->name."(".implode($params, ", ").");\r\n";
|
$output .= $method->name."(".implode($params, ", ").");\r\n";
|
||||||
@@ -291,6 +345,12 @@ exit('Only to be used as an helper for your IDE');\n\n";
|
|||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the description and get the inherited docs.
|
||||||
|
*
|
||||||
|
* @param $phpdoc
|
||||||
|
* @param $method
|
||||||
|
*/
|
||||||
protected function normalizeDescription(&$phpdoc, $method){
|
protected function normalizeDescription(&$phpdoc, $method){
|
||||||
//Get the short + long description from the DocBlock
|
//Get the short + long description from the DocBlock
|
||||||
$description = $phpdoc->getText();
|
$description = $phpdoc->getText();
|
||||||
@@ -314,6 +374,14 @@ exit('Only to be used as an helper for your IDE');\n\n";
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make some changes to the return types, if needed.
|
||||||
|
*
|
||||||
|
* @param $phpdoc
|
||||||
|
* @param $method
|
||||||
|
* @param $alias
|
||||||
|
* @return null
|
||||||
|
*/
|
||||||
protected function normalizeReturn(&$phpdoc, $method, $alias){
|
protected function normalizeReturn(&$phpdoc, $method, $alias){
|
||||||
//Get the return type and adjust them for beter autocomplete
|
//Get the return type and adjust them for beter autocomplete
|
||||||
$returnTags = $phpdoc->getTagsByName('return');
|
$returnTags = $phpdoc->getTagsByName('return');
|
||||||
@@ -324,14 +392,6 @@ exit('Only to be used as an helper for your IDE');\n\n";
|
|||||||
|
|
||||||
if($method->name == "__construct"){
|
if($method->name == "__construct"){
|
||||||
$tag->setType('self');
|
$tag->setType('self');
|
||||||
}elseif($alias == 'Eloquent' and
|
|
||||||
(in_array($method->name, array('pluck', 'first', 'fill', 'newInstance', 'newFromBuilder', 'create', 'find', 'findOrFail'))
|
|
||||||
or $returnValue === '\Illuminate\Database\Query\Builder')){
|
|
||||||
//Reference the calling class, to provide more accurate auto-complete
|
|
||||||
$tag->addType('static');
|
|
||||||
}elseif($alias == 'Eloquent' and in_array($method->name, array('all', 'get'))){
|
|
||||||
$tag->addType('\Eloquent[]');
|
|
||||||
$tag->addType('static[]');
|
|
||||||
}elseif($alias == 'Eloquent' and in_array($method->name, array('hasOne', 'hasMany', 'belongsTo', 'belongsToMany', 'morphOne', 'morphTo', 'morphMany'))){
|
}elseif($alias == 'Eloquent' and in_array($method->name, array('hasOne', 'hasMany', 'belongsTo', 'belongsToMany', 'morphOne', 'morphTo', 'morphMany'))){
|
||||||
$tag->addType('\Eloquent');
|
$tag->addType('\Eloquent');
|
||||||
}
|
}
|
||||||
@@ -341,6 +401,12 @@ exit('Only to be used as an helper for your IDE');\n\n";
|
|||||||
return $returnValue;
|
return $returnValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the parameters and format them correctly
|
||||||
|
*
|
||||||
|
* @param $method
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
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 paremeters, and make the correct output string
|
||||||
$params = array();
|
$params = array();
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ class ModelsCommand extends Command {
|
|||||||
protected $methods = array();
|
protected $methods = array();
|
||||||
protected $write = false;
|
protected $write = false;
|
||||||
protected $dir;
|
protected $dir;
|
||||||
|
protected $reset;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -56,6 +57,7 @@ class ModelsCommand extends Command {
|
|||||||
$this->write = $this->option('write');
|
$this->write = $this->option('write');
|
||||||
$this->dir = $this->option('dir');
|
$this->dir = $this->option('dir');
|
||||||
$model = $this->argument('model');
|
$model = $this->argument('model');
|
||||||
|
$this->reset = $this->option('reset');
|
||||||
|
|
||||||
$content = $this->generateDocs($model);
|
$content = $this->generateDocs($model);
|
||||||
|
|
||||||
@@ -95,6 +97,7 @@ class ModelsCommand extends Command {
|
|||||||
array('filename', 'F', InputOption::VALUE_OPTIONAL, 'The path to the helper file', '_ide_helper_models.php'),
|
array('filename', 'F', InputOption::VALUE_OPTIONAL, 'The path to the helper file', '_ide_helper_models.php'),
|
||||||
array('dir', 'D', InputOption::VALUE_OPTIONAL, 'The model dir','app/models'),
|
array('dir', 'D', InputOption::VALUE_OPTIONAL, 'The model dir','app/models'),
|
||||||
array('write', 'W', InputOption::VALUE_NONE, 'Write to Model file'),
|
array('write', 'W', InputOption::VALUE_NONE, 'Write to Model file'),
|
||||||
|
array('reset', 'R', InputOption::VALUE_NONE, 'Remove the original phpdocs instead of appending'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -310,7 +313,12 @@ class ModelsCommand extends Command {
|
|||||||
$namespace = $reflection->getNamespaceName();
|
$namespace = $reflection->getNamespaceName();
|
||||||
$classname = $reflection->getShortName();
|
$classname = $reflection->getShortName();
|
||||||
$originalDoc = $reflection->getDocComment();
|
$originalDoc = $reflection->getDocComment();
|
||||||
$phpdoc = new DocBlock($reflection, new Context($namespace));
|
|
||||||
|
if($this->reset){
|
||||||
|
$phpdoc = new DocBlock('', new Context($namespace));
|
||||||
|
}else{
|
||||||
|
$phpdoc = new DocBlock($reflection, new Context($namespace));
|
||||||
|
}
|
||||||
|
|
||||||
if(!$phpdoc->getText()){
|
if(!$phpdoc->getText()){
|
||||||
$phpdoc->setText("An Eloquent Model: '$class'");
|
$phpdoc->setText("An Eloquent Model: '$class'");
|
||||||
|
|||||||
@@ -40,10 +40,6 @@ return array(
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
'extra' => array(
|
'extra' => array(
|
||||||
'Auth' => array('Illuminate\Auth\Guard'),
|
|
||||||
'Cache' => array('Illuminate\Cache\StoreInterface', 'Illuminate\Cache\Repository'),
|
|
||||||
'DB' => array('Illuminate\Database\Connection'),
|
|
||||||
'Queue' => array('Illuminate\Queue\QueueInterface'),
|
|
||||||
'Eloquent' => array('Illuminate\Database\Eloquent\Builder', 'Illuminate\Database\Query\Builder'),
|
'Eloquent' => array('Illuminate\Database\Eloquent\Builder', 'Illuminate\Database\Query\Builder'),
|
||||||
),
|
),
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user