diff --git a/config/ide-helper.php b/config/ide-helper.php index 115d61e..374eacb 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -27,6 +27,18 @@ return array( 'include_fluent' => false, + /* + |-------------------------------------------------------------------------- + | Factory Builders + |-------------------------------------------------------------------------- + | + | Set to true to generate factory generators for better factory() + | method auto-completion. + | + */ + + 'include_factory_builders' => false, + /* |-------------------------------------------------------------------------- | Write Model Magic methods diff --git a/readme.md b/readme.md index d2fc6e2..396b448 100644 --- a/readme.md +++ b/readme.md @@ -156,6 +156,18 @@ After publishing vendor, simply change the `include_fluent` line your `config/id And then run `php artisan ide-helper:generate` , you will now see all of the Fluent methods are recognized by your IDE. +### Auto-completion for factory builders + +If you would like the `factory()->create()` and `factory()->make()` methods to return the correct model class, +you can enable custom factory builders with the `include_factory_builders` line your `config/ide-helper.php` file. + +```php +'include_factory_builders' => true, +``` + +For this to work, you must also publish the PhpStorm Meta file (see below). + + ## PhpStorm Meta for Container instances It's possible to generate a PhpStorm meta file to [add support for factory design pattern](https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata). For Laravel, this means we can make PhpStorm understand what kind of object we are resolving from the IoC Container. For example, `events` will return an `Illuminate\Events\Dispatcher` object, so with the meta file you can call `app('events')` and it will autocomplete the Dispatcher methods. diff --git a/resources/views/helper.php b/resources/views/helper.php index 04f9071..3e0359e 100644 --- a/resources/views/helper.php +++ b/resources/views/helper.php @@ -105,3 +105,13 @@ namespace Illuminate\Support { class Fluent {} } + + +namespace getNamespaceName()?> { + /** + * @method \Illuminate\Database\Eloquent\Collection|getShortName()?>[]|getShortName()?> create($attributes = []) + * @method \Illuminate\Database\Eloquent\Collection|getShortName()?>[]|getShortName()?> make($attributes = []) + */ + class getShortName()?>FactoryBuilder extends \Illuminate\Database\Eloquent\FactoryBuilder {} +} + diff --git a/resources/views/meta.php b/resources/views/meta.php index df53ebd..3e6fb6b 100644 --- a/resources/views/meta.php +++ b/resources/views/meta.php @@ -20,6 +20,15 @@ namespace PHPSTORM_META { ])); + + override(\factory(0), map([ + '' => '@FactoryBuilder', + + 'getName() ?>' => \getName() ?>FactoryBuilder::class, + + ])); + + override(\Illuminate\Support\Arr::add(0), type(0)); override(\Illuminate\Support\Arr::except(0), type(0)); override(\Illuminate\Support\Arr::first(0), elementType(0)); diff --git a/src/Console/MetaCommand.php b/src/Console/MetaCommand.php index 00d97c4..21bc343 100644 --- a/src/Console/MetaCommand.php +++ b/src/Console/MetaCommand.php @@ -10,6 +10,7 @@ namespace Barryvdh\LaravelIdeHelper\Console; +use Barryvdh\LaravelIdeHelper\Factories; use Illuminate\Console\Command; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -77,6 +78,9 @@ class MetaCommand extends Command */ public function handle() { + // Needs to run before exception handler is registered + $factories = $this->config->get('ide-helper.include_factory_builders') ? Factories::all() : []; + $this->registerClassAutoloadExceptions(); $bindings = array(); @@ -102,6 +106,7 @@ class MetaCommand extends Command $content = $this->view->make('meta', [ 'bindings' => $bindings, 'methods' => $this->methods, + 'factories' => $factories, ])->render(); $filename = $this->option('filename'); diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 34f38cc..65e9007 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -457,7 +457,9 @@ class ModelsCommand extends Command $reflection = new \ReflectionMethod($model, $method); if ($returnType = $reflection->getReturnType()) { - $type = $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string)$returnType; + $type = $returnType instanceof \ReflectionNamedType + ? $returnType->getName() + : (string)$returnType; } else { // php 7.x type or fallback to docblock $type = (string)$this->getReturnTypeFromDocBlock($reflection); diff --git a/src/Factories.php b/src/Factories.php new file mode 100644 index 0000000..fffded8 --- /dev/null +++ b/src/Factories.php @@ -0,0 +1,30 @@ +getProperty('definitions'); + $definitions->setAccessible(true); + + foreach ($definitions->getValue($factory) as $factory_target => $config) { + try { + $factories[] = new ReflectionClass($factory_target); + } catch (Exception $exception) { + } + } + + return $factories; + } +} diff --git a/src/Generator.php b/src/Generator.php index e1afc53..5916c9f 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -86,6 +86,7 @@ class Generator ->with('helpers', $this->helpers) ->with('version', $app->version()) ->with('include_fluent', $this->config->get('ide-helper.include_fluent', true)) + ->with('factories', $this->config->get('ide-helper.include_factory_builders') ? Factories::all() : []) ->render(); }