Skip to content

Commit 4f13ba8

Browse files
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
1 parent 0fee1c4 commit 4f13ba8

File tree

6 files changed

+150
-0
lines changed

6 files changed

+150
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ All notable changes to this project will be documented in this file.
2121
- Created a possibility to add custom relation type [\#987 / efinder2](https://github.com/barryvdh/laravel-ide-helper/pull/987)
2222
- Added `@see` with macro/mixin definition location to PhpDoc [\#1054 / riesjart](https://github.com/barryvdh/laravel-ide-helper/pull/1054)
2323
- Initial compatibility for PHP8 [\#1106 / mfn](https://github.com/barryvdh/laravel-ide-helper/pull/1106)
24+
- Generate PHPDoc for laravel 8.x factories [\#1074 / ahmed-aliraqi](https://github.com/barryvdh/laravel-ide-helper/pull/1074)
2425

2526
### Changed
2627
- Implement DeferrableProvider [\#914 / kon-shou](https://github.com/barryvdh/laravel-ide-helper/pull/914)

src/Console/ModelsCommand.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Composer\Autoload\ClassMapGenerator;
1919
use Illuminate\Console\Command;
2020
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
21+
use Illuminate\Database\Eloquent\Factories\Factory;
2122
use Illuminate\Database\Eloquent\Model;
2223
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2324
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
@@ -277,6 +278,7 @@ protected function generateDocs($loadModels, $ignore = '')
277278
$this->getPropertiesFromMethods($model);
278279
$this->getSoftDeleteMethods($model);
279280
$this->getCollectionMethods($model);
281+
$this->getFactoryMethods($model);
280282
$output .= $this->createPhpDocs($name);
281283
$ignore[] = $name;
282284
$this->nullableColumns = [];
@@ -1043,6 +1045,33 @@ protected function getSoftDeleteMethods($model)
10431045
}
10441046
}
10451047

1048+
/**
1049+
* Generate factory method from "HasFactory" trait.
1050+
*
1051+
* @param \Illuminate\Database\Eloquent\Model $model
1052+
*/
1053+
protected function getFactoryMethods($model)
1054+
{
1055+
if(!class_exists(Factory::class)) {
1056+
return;
1057+
}
1058+
1059+
$traits = class_uses(get_class($model), true);
1060+
if (! in_array('Illuminate\\Database\\Eloquent\\Factories\\HasFactory', $traits)) {
1061+
return;
1062+
}
1063+
1064+
$modelName = get_class($model);
1065+
$factory = get_class($modelName::factory());
1066+
$factory = '\\' . trim($factory, '\\');
1067+
1068+
if (! class_exists($factory)) {
1069+
return;
1070+
}
1071+
1072+
$this->setMethod('factory', $factory, ['...$parameters']);
1073+
}
1074+
10461075
/**
10471076
* Generates methods that return collections
10481077
* @param \Illuminate\Database\Eloquent\Model $model
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Factories;
6+
7+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Getter\Models\Simple;
8+
use Illuminate\Database\Eloquent\Factories\Factory;
9+
10+
class SimpleFactory extends Factory
11+
{
12+
/**
13+
* The name of the factory's corresponding model.
14+
*
15+
* @var string
16+
*/
17+
protected $model = Simple::class;
18+
19+
/**
20+
* Define the model's default state.
21+
*
22+
* @return array
23+
*/
24+
public function definition()
25+
{
26+
return [
27+
//
28+
];
29+
}
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
6+
7+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Factories\SimpleFactory;
8+
use Illuminate\Database\Eloquent\Factories\HasFactory;
9+
use Illuminate\Database\Eloquent\Model;
10+
11+
class Simple extends Model
12+
{
13+
use HasFactory;
14+
15+
/**
16+
* Create a new factory instance for the model.
17+
*
18+
* @return \Illuminate\Database\Eloquent\Factories\Factory
19+
*/
20+
protected static function newFactory()
21+
{
22+
return SimpleFactory::new();
23+
}
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories;
6+
7+
use Illuminate\Foundation\Application;
8+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
9+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
10+
11+
class Test extends AbstractModelsCommand
12+
{
13+
public function test(): void
14+
{
15+
if (! version_compare(Application::VERSION, '8.2', '>=')) {
16+
$this->markTestSkipped(
17+
'This test only works in Laravel >= 8.2'
18+
);
19+
}
20+
21+
$command = $this->app->make(ModelsCommand::class);
22+
23+
$tester = $this->runCommand($command, [
24+
'--write' => true,
25+
]);
26+
27+
$this->assertSame(0, $tester->getStatusCode());
28+
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
29+
$this->assertMatchesMockedSnapshot();
30+
}
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
6+
7+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Factories\SimpleFactory;
8+
use Illuminate\Database\Eloquent\Factories\HasFactory;
9+
use Illuminate\Database\Eloquent\Model;
10+
11+
/**
12+
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\Simple
13+
*
14+
* @property integer $id
15+
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Factories\SimpleFactory factory(...$parameters)
16+
* @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery()
17+
* @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
18+
* @method static \Illuminate\Database\Eloquent\Builder|Simple query()
19+
* @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value)
20+
* @mixin \Eloquent
21+
*/
22+
class Simple extends Model
23+
{
24+
use HasFactory;
25+
26+
/**
27+
* Create a new factory instance for the model.
28+
*
29+
* @return \Illuminate\Database\Eloquent\Factories\Factory
30+
*/
31+
protected static function newFactory()
32+
{
33+
return SimpleFactory::new();
34+
}
35+
}

0 commit comments

Comments
 (0)