Compare commits

..
13 Commits
Author SHA1 Message Date
Barry vd. Heuvel b91b959364 Create FUNDING.yml 2019-10-30 21:53:27 +01:00
naneri 9b0fbb4467 Added table of contents to Readme.md (#833) 2019-10-02 17:32:49 +02:00
Chris Morrell 6c8e847430 Add support for factory()->create() return types (#758)
* Revert "Forked release"

* Initial factory builder support

* Oops — fixed tabs

* Revert "Forked release"

* Whitespace

* Reverted more code

* Whitespace

* More whitespace

* Fixed PHPCS line
2019-10-01 20:18:19 +02:00
cxlblm 25b61c56fe __toString function has been DEPRECATED (#801)
* __toString function has been DEPRECATED

__toString function has been DEPRECATED as of PHP 7.1.0

* remove code

remove code
2019-09-17 07:08:26 +02:00
Alex 428d32cebd Add hasOneThrough to ide-helper:models (#807)
Added missing relation type.
2019-09-16 14:33:30 +02:00
Matthew Hailwood 51a72d7766 [PSR-12] Update param type array to use short array syntax (#828)
* Update Param type array to use short array syntax (PSR-12)

* Update Param type array to use short array syntax (PSR-12)
2019-09-16 11:52:59 +02:00
Patompong Savaengsuk d200f2284b Update readme.md (#825)
* Update readme.md

In version 2.6.5, this commit https://github.com/barryvdh/laravel-ide-helper/commit/f8a8cb18e0a42ae4131b7ed865fe792f9b34630c made it so library's consumer no longer need to pull in `doctrine/dbal` manually. I think we should mention that in the readme file as well. Although I'm not sure if it adds any value to the file itself or not.

* Remove note about doctrine/dbal from readme.md
2019-09-11 08:25:44 +02:00
Давид 8faae38655 Update readme.md (#824) 2019-09-10 15:46:31 +02:00
Barry vd. Heuvel 8740a9a158 Fix CS 2019-09-08 11:56:38 +02:00
liquid207 d344cdc842 Fix phpcs (#803) 2019-09-08 11:55:17 +02:00
Mandy Schoep 5e411ef104 Do not added instances of a anonymous class to the phpstorm_meta file (#806) 2019-09-08 11:46:25 +02:00
Barry vd. Heuvel 499a2113b3 Check if method exists 2019-09-08 11:45:23 +02:00
Barry vd. Heuvel f8a8cb18e0 Always require doctrine 2019-09-08 11:36:40 +02:00
12 changed files with 124 additions and 21 deletions
+12
View File
@@ -0,0 +1,12 @@
# These are supported funding model platforms
github: barryvdh
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
+3 -6
View File
@@ -15,7 +15,8 @@
"illuminate/console": "^5.5|^6", "illuminate/console": "^5.5|^6",
"illuminate/filesystem": "^5.5|^6", "illuminate/filesystem": "^5.5|^6",
"barryvdh/reflection-docblock": "^2.0.6", "barryvdh/reflection-docblock": "^2.0.6",
"composer/composer": "^1.6" "composer/composer": "^1.6",
"doctrine/dbal": "~2.3"
}, },
"require-dev": { "require-dev": {
"illuminate/config": "^5.5|^6", "illuminate/config": "^5.5|^6",
@@ -23,11 +24,7 @@
"phpro/grumphp": "^0.14", "phpro/grumphp": "^0.14",
"phpunit/phpunit" : "4.*", "phpunit/phpunit" : "4.*",
"scrutinizer/ocular": "~1.1", "scrutinizer/ocular": "~1.1",
"squizlabs/php_codesniffer": "^3", "squizlabs/php_codesniffer": "^3"
"doctrine/dbal": "~2.3"
},
"suggest": {
"doctrine/dbal": "Load information from the database about models for phpdocs (~2.3)"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
+12
View File
@@ -27,6 +27,18 @@ return array(
'include_fluent' => false, '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 | Write Model Magic methods
+27 -9
View File
@@ -7,6 +7,19 @@
__For Laravel 4.x, check [version 1.11](https://github.com/barryvdh/laravel-ide-helper/tree/1.11)__ __For Laravel 4.x, check [version 1.11](https://github.com/barryvdh/laravel-ide-helper/tree/1.11)__
* [Complete phpDocs, directly from the source](#complete-phpdocs--directly-from-the-source)
* [Install](#install)
* [Automatic phpDoc generation for Laravel Facades](#automatic-phpdoc-generation-for-laravel-facades)
* [Automatic phpDocs for models](#automatic-phpdocs-for-models)
* [Automatic phpDocs generation for Laravel Fluent methods](#automatic-phpdocs-generation-for-laravel-fluent-methods)
* [Auto-completion for factory builders](#auto-completion-for-factory-builders)
- [PhpStorm Meta for Container instances](#phpstorm-meta-for-container-instances)
- [Lumen Install](#lumen-install)
+ [Enabling Facades](#enabling-facades)
+ [Adding the Service Provider](#adding-the-service-provider)
+ [Adding Additional Facades](#adding-additional-facades)
* [License](#license)
### Complete phpDocs, directly from the source ### Complete phpDocs, directly from the source
_Check out [this Laracasts video](https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/15) for a quick introduction/explanation!_ _Check out [this Laracasts video](https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/15) for a quick introduction/explanation!_
@@ -69,8 +82,8 @@ You can configure your composer.json to do this after each commit:
"scripts":{ "scripts":{
"post-update-cmd": [ "post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate", "Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan ide-helper:generate", "@php artisan ide-helper:generate",
"php artisan ide-helper:meta" "@php artisan ide-helper:meta"
] ]
}, },
``` ```
@@ -91,13 +104,6 @@ The `Illuminate/Support/helpers.php` is already set up, but you can add/remove y
### Automatic phpDocs for models ### Automatic phpDocs for models
> You need to add `doctrine/dbal: ~2.3` to require-dev in your own composer.json to get database columns.
```bash
composer require --dev doctrine/dbal
```
If you don't want to write your properties yourself, you can use the command `php artisan ide-helper:models` to generate If you don't want to write your properties yourself, you can use the command `php artisan ide-helper:models` to generate
phpDocs, based on table columns, relations and getters/setters. You can write the comments directly to your Model file, using the `--write (-W)` option. By default, you are asked to overwrite or write to a separate file (`_ide_helper_models.php`). You can force No with `--nowrite (-N)`. phpDocs, based on table columns, relations and getters/setters. You can write the comments directly to your Model file, using the `--write (-W)` option. By default, you are asked to overwrite or write to a separate file (`_ide_helper_models.php`). You can force No with `--nowrite (-N)`.
Please make sure to backup your models, before writing the info. Please make sure to backup your models, before writing the info.
@@ -163,6 +169,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. 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 ## 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. 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.
+10
View File
@@ -105,3 +105,13 @@ namespace Illuminate\Support {
class Fluent {} class Fluent {}
} }
<?php endif ?> <?php endif ?>
<?php foreach ($factories as $factory): ?>
namespace <?=$factory->getNamespaceName()?> {
/**
* @method \Illuminate\Database\Eloquent\Collection|<?=$factory->getShortName()?>[]|<?=$factory->getShortName()?> create($attributes = [])
* @method \Illuminate\Database\Eloquent\Collection|<?=$factory->getShortName()?>[]|<?=$factory->getShortName()?> make($attributes = [])
*/
class <?=$factory->getShortName()?>FactoryBuilder extends \Illuminate\Database\Eloquent\FactoryBuilder {}
}
<?php endforeach; ?>
+9
View File
@@ -20,6 +20,15 @@ namespace PHPSTORM_META {
])); ]));
<?php endforeach; ?> <?php endforeach; ?>
<?php if (count($factories)): ?>
override(\factory(0), map([
'' => '@FactoryBuilder',
<?php foreach($factories as $factory): ?>
'<?= $factory->getName() ?>' => \<?= $factory->getName() ?>FactoryBuilder::class,
<?php endforeach; ?>
]));
<?php endif; ?>
override(\Illuminate\Support\Arr::add(0), type(0)); override(\Illuminate\Support\Arr::add(0), type(0));
override(\Illuminate\Support\Arr::except(0), type(0)); override(\Illuminate\Support\Arr::except(0), type(0));
override(\Illuminate\Support\Arr::first(0), elementType(0)); override(\Illuminate\Support\Arr::first(0), elementType(0));
+1 -1
View File
@@ -316,7 +316,7 @@ class Alias
{ {
foreach ($this->magicMethods as $magic => $real) { foreach ($this->magicMethods as $magic => $real) {
list($className, $name) = explode('::', $real); list($className, $name) = explode('::', $real);
if (!class_exists($className) && !interface_exists($className)) { if ((!class_exists($className) && !interface_exists($className)) || !method_exists($className, $name)) {
continue; continue;
} }
$method = new \ReflectionMethod($className, $name); $method = new \ReflectionMethod($className, $name);
+7 -1
View File
@@ -10,6 +10,7 @@
namespace Barryvdh\LaravelIdeHelper\Console; namespace Barryvdh\LaravelIdeHelper\Console;
use Barryvdh\LaravelIdeHelper\Factories;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@@ -77,6 +78,9 @@ class MetaCommand extends Command
*/ */
public function handle() public function handle()
{ {
// Needs to run before exception handler is registered
$factories = $this->config->get('ide-helper.include_factory_builders') ? Factories::all() : [];
$this->registerClassAutoloadExceptions(); $this->registerClassAutoloadExceptions();
$bindings = array(); $bindings = array();
@@ -88,7 +92,8 @@ class MetaCommand extends Command
try { try {
$concrete = $this->laravel->make($abstract); $concrete = $this->laravel->make($abstract);
if (is_object($concrete)) { $reflectionClass = new \ReflectionClass($concrete);
if (is_object($concrete) && !$reflectionClass->isAnonymous()) {
$bindings[$abstract] = get_class($concrete); $bindings[$abstract] = get_class($concrete);
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
@@ -101,6 +106,7 @@ class MetaCommand extends Command
$content = $this->view->make('meta', [ $content = $this->view->make('meta', [
'bindings' => $bindings, 'bindings' => $bindings,
'methods' => $this->methods, 'methods' => $this->methods,
'factories' => $factories,
])->render(); ])->render();
$filename = $this->option('filename'); $filename = $this->option('filename');
+11 -3
View File
@@ -455,9 +455,16 @@ class ModelsCommand extends Command
) { ) {
//Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
$reflection = new \ReflectionMethod($model, $method); $reflection = new \ReflectionMethod($model, $method);
// php 7.x type or fallback to docblock
$type = (string)($reflection->getReturnType() ?: $this->getReturnTypeFromDocBlock($reflection));
if ($returnType = $reflection->getReturnType()) {
$type = $returnType instanceof \ReflectionNamedType
? $returnType->getName()
: (string)$returnType;
} else {
// php 7.x type or fallback to docblock
$type = (string)$this->getReturnTypeFromDocBlock($reflection);
}
$file = new \SplFileObject($reflection->getFileName()); $file = new \SplFileObject($reflection->getFileName());
$file->seek($reflection->getStartLine() - 1); $file->seek($reflection->getStartLine() - 1);
@@ -473,6 +480,7 @@ class ModelsCommand extends Command
foreach (array( foreach (array(
'hasMany' => '\Illuminate\Database\Eloquent\Relations\HasMany', 'hasMany' => '\Illuminate\Database\Eloquent\Relations\HasMany',
'hasManyThrough' => '\Illuminate\Database\Eloquent\Relations\HasManyThrough', 'hasManyThrough' => '\Illuminate\Database\Eloquent\Relations\HasManyThrough',
'hasOneThrough' => '\Illuminate\Database\Eloquent\Relations\HasOneThrough',
'belongsToMany' => '\Illuminate\Database\Eloquent\Relations\BelongsToMany', 'belongsToMany' => '\Illuminate\Database\Eloquent\Relations\BelongsToMany',
'hasOne' => '\Illuminate\Database\Eloquent\Relations\HasOne', 'hasOne' => '\Illuminate\Database\Eloquent\Relations\HasOne',
'belongsTo' => '\Illuminate\Database\Eloquent\Relations\BelongsTo', 'belongsTo' => '\Illuminate\Database\Eloquent\Relations\BelongsTo',
@@ -730,7 +738,7 @@ class ModelsCommand extends Command
if (is_bool($default)) { if (is_bool($default)) {
$default = $default ? 'true' : 'false'; $default = $default ? 'true' : 'false';
} elseif (is_array($default)) { } elseif (is_array($default)) {
$default = 'array()'; $default = '[]';
} elseif (is_null($default)) { } elseif (is_null($default)) {
$default = 'null'; $default = 'null';
} elseif (is_int($default)) { } elseif (is_int($default)) {
+30
View File
@@ -0,0 +1,30 @@
<?php
namespace Barryvdh\LaravelIdeHelper;
use Exception;
use Illuminate\Database\Eloquent\Factory;
use ReflectionClass;
class Factories
{
public static function all()
{
$factories = [];
$factory = app(Factory::class);
$definitions = (new ReflectionClass(Factory::class))->getProperty('definitions');
$definitions->setAccessible(true);
foreach ($definitions->getValue($factory) as $factory_target => $config) {
try {
$factories[] = new ReflectionClass($factory_target);
} catch (Exception $exception) {
}
}
return $factories;
}
}
+1
View File
@@ -86,6 +86,7 @@ class Generator
->with('helpers', $this->helpers) ->with('helpers', $this->helpers)
->with('version', $app->version()) ->with('version', $app->version())
->with('include_fluent', $this->config->get('ide-helper.include_fluent', true)) ->with('include_fluent', $this->config->get('ide-helper.include_fluent', true))
->with('factories', $this->config->get('ide-helper.include_factory_builders') ? Factories::all() : [])
->render(); ->render();
} }
+1 -1
View File
@@ -321,7 +321,7 @@ class Method
if (is_bool($default)) { if (is_bool($default)) {
$default = $default ? 'true' : 'false'; $default = $default ? 'true' : 'false';
} elseif (is_array($default)) { } elseif (is_array($default)) {
$default = 'array()'; $default = '[]';
} elseif (is_null($default)) { } elseif (is_null($default)) {
$default = 'null'; $default = 'null';
} elseif (is_int($default)) { } elseif (is_int($default)) {