mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 18:13:11 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b91b959364 | ||
|
|
9b0fbb4467 | ||
|
|
6c8e847430 | ||
|
|
25b61c56fe | ||
|
|
428d32cebd | ||
|
|
51a72d7766 | ||
|
|
d200f2284b | ||
|
|
8faae38655 | ||
|
|
8740a9a158 | ||
|
|
d344cdc842 | ||
|
|
5e411ef104 | ||
|
|
499a2113b3 | ||
|
|
f8a8cb18e0 | ||
|
|
16eb4f65ee | ||
|
|
0edf8f750e | ||
|
|
1b15d2930b | ||
|
|
3668e3821a | ||
|
|
7b4cef1ea4 | ||
|
|
cd2a71d43c | ||
|
|
c9132b28a6 | ||
|
|
8ab6e0536b | ||
|
|
87ebe458e9 | ||
|
|
9ef14f83be | ||
|
|
39c148ad42 | ||
|
|
2b3c9a2449 |
@@ -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']
|
||||
+8
-11
@@ -11,23 +11,20 @@
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7",
|
||||
"illuminate/support": "^5.5,<5.9",
|
||||
"illuminate/console": "^5.5,<5.9",
|
||||
"illuminate/filesystem": "^5.5,<5.9",
|
||||
"illuminate/support": "^5.5|^6",
|
||||
"illuminate/console": "^5.5|^6",
|
||||
"illuminate/filesystem": "^5.5|^6",
|
||||
"barryvdh/reflection-docblock": "^2.0.6",
|
||||
"composer/composer": "^1.6"
|
||||
"composer/composer": "^1.6",
|
||||
"doctrine/dbal": "~2.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"illuminate/config": "^5.1,<5.9",
|
||||
"illuminate/view": "^5.1,<5.9",
|
||||
"illuminate/config": "^5.5|^6",
|
||||
"illuminate/view": "^5.5|^6",
|
||||
"phpro/grumphp": "^0.14",
|
||||
"phpunit/phpunit" : "4.*",
|
||||
"scrutinizer/ocular": "~1.1",
|
||||
"squizlabs/php_codesniffer": "^3",
|
||||
"doctrine/dbal": "~2.3"
|
||||
},
|
||||
"suggest": {
|
||||
"doctrine/dbal": "Load information from the database about models for phpdocs (~2.3)"
|
||||
"squizlabs/php_codesniffer": "^3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
||||
+15
-14
@@ -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
|
||||
@@ -98,18 +110,7 @@ return array(
|
||||
'Session' => array('Illuminate\Session\Store'),
|
||||
),
|
||||
|
||||
'magic' => array(
|
||||
'Log' => array(
|
||||
'debug' => 'Monolog\Logger::addDebug',
|
||||
'info' => 'Monolog\Logger::addInfo',
|
||||
'notice' => 'Monolog\Logger::addNotice',
|
||||
'warning' => 'Monolog\Logger::addWarning',
|
||||
'error' => 'Monolog\Logger::addError',
|
||||
'critical' => 'Monolog\Logger::addCritical',
|
||||
'alert' => 'Monolog\Logger::addAlert',
|
||||
'emergency' => 'Monolog\Logger::addEmergency',
|
||||
)
|
||||
),
|
||||
'magic' => array(),
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
@@ -189,10 +190,10 @@ return array(
|
||||
| Cast the given "real type" to the given "type".
|
||||
|
|
||||
*/
|
||||
'type_overrides' => array(
|
||||
'type_overrides' => array(
|
||||
'integer' => 'int',
|
||||
'boolean' => 'bool',
|
||||
),
|
||||
),
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
## Laravel 5 IDE Helper Generator
|
||||
## Laravel IDE Helper Generator
|
||||
|
||||
[![Latest Version on Packagist][ico-version]][link-packagist]
|
||||
[![Software License][ico-license]](LICENSE.md)
|
||||
@@ -7,6 +7,19 @@
|
||||
|
||||
__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
|
||||
|
||||
_Check out [this Laracasts video](https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/15) for a quick introduction/explanation!_
|
||||
@@ -29,7 +42,7 @@ Note: You do need CodeComplice for Sublime Text: https://github.com/spectacles/C
|
||||
Require this package with composer using the following command:
|
||||
|
||||
```bash
|
||||
composer require barryvdh/laravel-ide-helper
|
||||
composer require --dev barryvdh/laravel-ide-helper
|
||||
```
|
||||
|
||||
After updating composer, add the service provider to the `providers` array in `config/app.php`
|
||||
@@ -39,12 +52,6 @@ Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
|
||||
```
|
||||
**Laravel 5.5** uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.
|
||||
|
||||
To install this package on only development systems, add the `--dev` flag to your composer command:
|
||||
|
||||
```bash
|
||||
composer require --dev barryvdh/laravel-ide-helper
|
||||
```
|
||||
|
||||
In Laravel, instead of adding the service provider in the `config/app.php` file, you can add the following code to your `app/Providers/AppServiceProvider.php` file, within the `register()` method:
|
||||
|
||||
```php
|
||||
@@ -75,8 +82,8 @@ You can configure your composer.json to do this after each commit:
|
||||
"scripts":{
|
||||
"post-update-cmd": [
|
||||
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
|
||||
"php artisan ide-helper:generate",
|
||||
"php artisan ide-helper:meta"
|
||||
"@php artisan ide-helper:generate",
|
||||
"@php artisan ide-helper:meta"
|
||||
]
|
||||
},
|
||||
```
|
||||
@@ -97,13 +104,6 @@ The `Illuminate/Support/helpers.php` is already set up, but you can add/remove y
|
||||
|
||||
### 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
|
||||
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.
|
||||
@@ -169,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.
|
||||
|
||||
|
||||
### 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.
|
||||
|
||||
@@ -105,3 +105,13 @@ namespace Illuminate\Support {
|
||||
class Fluent {}
|
||||
}
|
||||
<?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; ?>
|
||||
|
||||
@@ -20,6 +20,15 @@ namespace PHPSTORM_META {
|
||||
]));
|
||||
<?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::except(0), type(0));
|
||||
override(\Illuminate\Support\Arr::first(0), elementType(0));
|
||||
|
||||
+3
-2
@@ -10,6 +10,7 @@
|
||||
|
||||
namespace Barryvdh\LaravelIdeHelper;
|
||||
|
||||
use Closure;
|
||||
use ReflectionClass;
|
||||
use Barryvdh\Reflection\DocBlock;
|
||||
use Barryvdh\Reflection\DocBlock\Context;
|
||||
@@ -315,7 +316,7 @@ class Alias
|
||||
{
|
||||
foreach ($this->magicMethods as $magic => $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;
|
||||
}
|
||||
$method = new \ReflectionMethod($className, $name);
|
||||
@@ -395,7 +396,7 @@ class Alias
|
||||
return new \ReflectionMethod($macro_func[0], $macro_func[1]);
|
||||
}
|
||||
|
||||
if (is_object($macro_func) && is_callable($macro_func)) {
|
||||
if (is_object($macro_func) && is_callable($macro_func) && !$macro_func instanceof Closure) {
|
||||
return new \ReflectionMethod($macro_func, '__invoke');
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -47,6 +48,7 @@ class MetaCommand extends Command
|
||||
|
||||
protected $methods = [
|
||||
'new \Illuminate\Contracts\Container\Container',
|
||||
'\Illuminate\Container\Container::makeWith(0)',
|
||||
'\Illuminate\Contracts\Container\Container::make(0)',
|
||||
'\Illuminate\Contracts\Container\Container::makeWith(0)',
|
||||
'\App::make(0)',
|
||||
@@ -76,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();
|
||||
@@ -87,10 +92,11 @@ class MetaCommand extends Command
|
||||
|
||||
try {
|
||||
$concrete = $this->laravel->make($abstract);
|
||||
if (is_object($concrete)) {
|
||||
$reflectionClass = new \ReflectionClass($concrete);
|
||||
if (is_object($concrete) && !$reflectionClass->isAnonymous()) {
|
||||
$bindings[$abstract] = get_class($concrete);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
} catch (\Throwable $e) {
|
||||
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
|
||||
$this->comment("Cannot make '$abstract': ".$e->getMessage());
|
||||
}
|
||||
@@ -100,6 +106,7 @@ class MetaCommand extends Command
|
||||
$content = $this->view->make('meta', [
|
||||
'bindings' => $bindings,
|
||||
'methods' => $this->methods,
|
||||
'factories' => $factories,
|
||||
])->render();
|
||||
|
||||
$filename = $this->option('filename');
|
||||
|
||||
@@ -96,7 +96,7 @@ class ModelsCommand extends Command
|
||||
//If filename is default and Write is not specified, ask what to do
|
||||
if (!$this->write && $filename === $this->filename && !$this->option('nowrite')) {
|
||||
if ($this->confirm(
|
||||
"Do you want to overwrite the existing model files? Choose no to write to $filename instead?"
|
||||
"Do you want to overwrite the existing model files? Choose no to write to $filename instead"
|
||||
)
|
||||
) {
|
||||
$this->write = true;
|
||||
@@ -217,7 +217,7 @@ class ModelsCommand extends Command
|
||||
$output .= $this->createPhpDocs($name);
|
||||
$ignore[] = $name;
|
||||
$this->nullableColumns = [];
|
||||
} catch (\Exception $e) {
|
||||
} catch (\Throwable $e) {
|
||||
$this->error("Exception: " . $e->getMessage() . "\nCould not analyze class $name.");
|
||||
}
|
||||
}
|
||||
@@ -290,7 +290,7 @@ class ModelsCommand extends Command
|
||||
$realType = '\Illuminate\Support\Collection';
|
||||
break;
|
||||
default:
|
||||
$realType = 'mixed';
|
||||
$realType = class_exists($type) ? ('\\' . $type) : 'mixed';
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -446,15 +446,25 @@ class ModelsCommand extends Command
|
||||
}
|
||||
} elseif (in_array($method, ['query', 'newQuery', 'newModelQuery'])) {
|
||||
$reflection = new \ReflectionClass($model);
|
||||
$this->setMethod($method, '\Illuminate\Database\Eloquent\Builder|\\' . $reflection->getName());
|
||||
|
||||
$builder = get_class($model->newModelQuery());
|
||||
|
||||
$this->setMethod($method, "\\{$builder}|\\" . $reflection->getName());
|
||||
} elseif (!method_exists('Illuminate\Database\Eloquent\Model', $method)
|
||||
&& !Str::startsWith($method, 'get')
|
||||
) {
|
||||
//Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
|
||||
$reflection = new \ReflectionMethod($model, $method);
|
||||
// php 7.x type or fallback to docblock
|
||||
$type = (string) $reflection->getReturnType() ?: (string)$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->seek($reflection->getStartLine() - 1);
|
||||
|
||||
@@ -470,6 +480,7 @@ class ModelsCommand extends Command
|
||||
foreach (array(
|
||||
'hasMany' => '\Illuminate\Database\Eloquent\Relations\HasMany',
|
||||
'hasManyThrough' => '\Illuminate\Database\Eloquent\Relations\HasManyThrough',
|
||||
'hasOneThrough' => '\Illuminate\Database\Eloquent\Relations\HasOneThrough',
|
||||
'belongsToMany' => '\Illuminate\Database\Eloquent\Relations\BelongsToMany',
|
||||
'hasOne' => '\Illuminate\Database\Eloquent\Relations\HasOne',
|
||||
'belongsTo' => '\Illuminate\Database\Eloquent\Relations\BelongsTo',
|
||||
@@ -500,7 +511,7 @@ class ModelsCommand extends Command
|
||||
'morphToMany',
|
||||
'morphedByMany',
|
||||
];
|
||||
if (in_array($relation, $relations)) {
|
||||
if (strpos(get_class($relationObj), 'Many') !== false) {
|
||||
//Collection or array of models (because Collection is Arrayable)
|
||||
$this->setProperty(
|
||||
$method,
|
||||
@@ -508,6 +519,12 @@ class ModelsCommand extends Command
|
||||
true,
|
||||
null
|
||||
);
|
||||
$this->setProperty(
|
||||
Str::snake($method) . '_count',
|
||||
'int|null',
|
||||
true,
|
||||
false
|
||||
);
|
||||
} elseif ($relation === "morphTo") {
|
||||
// Model isn't specified because relation is polymorphic
|
||||
$this->setProperty(
|
||||
@@ -721,7 +738,7 @@ class ModelsCommand extends Command
|
||||
if (is_bool($default)) {
|
||||
$default = $default ? 'true' : 'false';
|
||||
} elseif (is_array($default)) {
|
||||
$default = 'array()';
|
||||
$default = '[]';
|
||||
} elseif (is_null($default)) {
|
||||
$default = 'null';
|
||||
} elseif (is_int($default)) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -321,7 +321,7 @@ class Method
|
||||
if (is_bool($default)) {
|
||||
$default = $default ? 'true' : 'false';
|
||||
} elseif (is_array($default)) {
|
||||
$default = 'array()';
|
||||
$default = '[]';
|
||||
} elseif (is_null($default)) {
|
||||
$default = 'null';
|
||||
} elseif (is_int($default)) {
|
||||
|
||||
Reference in New Issue
Block a user