Generate PHPDoc for laravel 8.x factories (#1074)

* Generate PHPDoc for laravel 8.x factories

* formatting

* sorting imports

* fix code style

* fix tests in laravel 7.x and lower

* use markTestSkipped

* fix test failing ~_~

* add  argument

* determine the laravel vertion is 8.2.x or upper

* use version_compare instead of artisan command

* use correct namespace

* formatting

* formatting

* formatting

* update next release notes
This commit is contained in:
Ahmed Fathy
2021-01-06 13:38:26 +01:00
committed by GitHub
parent 0fee1c47d3
commit 4f13ba85bd
6 changed files with 150 additions and 0 deletions
+29
View File
@@ -18,6 +18,7 @@ use Barryvdh\Reflection\DocBlock\Tag;
use Composer\Autoload\ClassMapGenerator;
use Illuminate\Console\Command;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
@@ -277,6 +278,7 @@ class ModelsCommand extends Command
$this->getPropertiesFromMethods($model);
$this->getSoftDeleteMethods($model);
$this->getCollectionMethods($model);
$this->getFactoryMethods($model);
$output .= $this->createPhpDocs($name);
$ignore[] = $name;
$this->nullableColumns = [];
@@ -1043,6 +1045,33 @@ class ModelsCommand extends Command
}
}
/**
* Generate factory method from "HasFactory" trait.
*
* @param \Illuminate\Database\Eloquent\Model $model
*/
protected function getFactoryMethods($model)
{
if(!class_exists(Factory::class)) {
return;
}
$traits = class_uses(get_class($model), true);
if (! in_array('Illuminate\\Database\\Eloquent\\Factories\\HasFactory', $traits)) {
return;
}
$modelName = get_class($model);
$factory = get_class($modelName::factory());
$factory = '\\' . trim($factory, '\\');
if (! class_exists($factory)) {
return;
}
$this->setMethod('factory', $factory, ['...$parameters']);
}
/**
* Generates methods that return collections
* @param \Illuminate\Database\Eloquent\Model $model