Compare commits

...
22 Commits
Author SHA1 Message Date
Barry vd. Heuvel 16eb4f65ee Update readme.md 2019-09-03 19:51:13 +02:00
Kevin Bond 0edf8f750e remove monolog configuration (fixes #818) (#820) 2019-09-03 19:49:46 +02:00
Johan van Helden 1b15d2930b fix the identation in the config (#817) 2019-08-31 12:53:08 +02:00
Matthew Hailwood 3668e3821a Add support for custom cast classes (#816)
* Add support for custom cast classes

* Fix unrelated code style warning.
2019-08-30 07:32:01 +02:00
Barry vd. Heuvel 7b4cef1ea4 Update composer.json 2019-08-23 14:21:37 +02:00
Stanislas cd2a71d43c ModelsCommand: Remove question mark (#812)
Sorry this is a bit nitpicky 😅
2019-08-06 07:54:53 +02:00
edvordo c9132b28a6 Chages to x2Many relationships detection & adds relationship_count docBlock comment (#783)
* x to Many relationships are detected via a `Many` keyword in the relationship object class name
   * reason: example: `belongsTo` was incorrectly recognized as `belongsToMany`
 * adds `@property-read int|null $x2Many_relationship_method_name_count` model class docBlock comment
   * when model makes use of `withCount` or `loadCount` there is a property on the resource `$instance->relationship_method_count` that states the count of related models
   * can be `NULL` when `withCount` or `loadCount` aren't used
2019-06-18 16:38:42 +02:00
Guilherme Pressutto 8ab6e0536b Fixed meta not working with app()->makeWith() (#791) 2019-05-29 17:09:57 +02:00
Theodore R. Smith 87ebe458e9 Removed confusing README installation instructions (#786)
While installing this for my project, I didn't see the `--dev` instructions until I was literally editing this README for a pull request.

Since laravel-ide-helper is only designed for development purposes, let's just remove the superfluous entry altogether. Smart devs who for some crazy reason need it on prod will know to remove the `--dev`, while less knowledgeable devs won't shoot themselves in the foot accidentally.
2019-04-17 22:07:56 +02:00
leo108 9ef14f83be use Throwable instead of Exception (#779) 2019-03-27 05:56:31 +01:00
Brent Roose 39c148ad42 Support for custom builder classes (#782) 2019-03-26 11:38:22 +01:00
Francis Lavoie 2b3c9a2449 Fix BC introduced in #765 (#778)
See my comment https://github.com/barryvdh/laravel-ide-helper/pull/765#issuecomment-471014271:

The change in #765 made anonymous functions get reflected with `ReflectionMethod` instead of `ReflectionFunction`, which makes it lose the doc comment. This means that the generated helpers are missing their return types. This is because Closure is also an object and callable.

To fix it, Closure should be excluded from that new condition.
2019-03-12 11:38:49 +01:00
Matthew Brown 725711022b Make autoload exception more specific (#777)
This is a nice-to-have ([as I'm currently overriding it](https://github.com/psalm/laravel-psalm-plugin/commit/a700c89061d151d1c08851abd93d834f9183534d)), but might help other packages.
2019-03-05 10:24:51 +01:00
neoteknic 881f77156c Fix php 7.3 bug (#772) 2019-03-02 15:15:16 +01:00
neoteknic 0fad511954 Fix Bug PHP 7.3 & L5.8 (#774)
add cast (string) to disable bug ($type can be null)
2019-03-02 15:14:46 +01:00
Stan Daniëls 754bb4d075 Add support for invokable classes as macro function (#765)
Macros can be used with invokable classes, like so.

```
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Response;

class ResponseMacroServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Response::macro('foo', new Foo());
    }
}

class Foo
{
    public function __invoke()
    {
        return 'foobar';
    }
}
```

When running `ide-helper:generate` the following fatal error was thrown.

```  Symfony\Component\Debug\Exception\FatalThrowableError  : ReflectionFunction::__construct() expects parameter 1 to be string, object given```

This commit fixes this error.
2019-02-18 20:54:27 +01:00
edcoreweb bcb881b20d Nested relations detection. (#763) 2019-02-03 16:59:09 +01:00
Tim Philips d00600d769 Include DocBloc’s from classes to allow deeper code inspection (#745)
* Add support to include DocBloc’s from classes to allow further code inspection

* Adjust for PSR2

* Additional fix for PSR2
2019-01-28 21:38:57 +01:00
Ahmed Fathy 858dd40844 Install doctrine/dbal on development (#753) 2019-01-28 21:25:55 +01:00
Kyle 4cd11a4d33 Allow Laravel 5.8 (#754) 2019-01-28 21:24:50 +01:00
Barry vd. Heuvel 5439781e7c Update composer.json 2018-12-19 13:13:32 +01:00
+pikari 190bf07c05 fix: warning when call the instance method (#735) 2018-12-19 13:12:44 +01:00
9 changed files with 123 additions and 64 deletions
+6 -6
View File
@@ -11,15 +11,15 @@
],
"require": {
"php": ">=7",
"illuminate/support": "^5.5,<5.8",
"illuminate/console": "^5.5,<5.8",
"illuminate/filesystem": "^5.5,<5.8",
"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"
},
"require-dev": {
"illuminate/config": "^5.1,<5.8",
"illuminate/view": "^5.1,<5.8",
"illuminate/config": "^5.5|^6",
"illuminate/view": "^5.5|^6",
"phpro/grumphp": "^0.14",
"phpunit/phpunit" : "4.*",
"scrutinizer/ocular": "~1.1",
@@ -46,7 +46,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.5-dev"
"dev-master": "2.6-dev"
},
"laravel": {
"providers": [
+15 -14
View File
@@ -98,18 +98,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,8 +178,20 @@ return array(
| Cast the given "real type" to the given "type".
|
*/
'type_overrides' => array(
'type_overrides' => array(
'integer' => 'int',
'boolean' => 'bool',
),
),
/*
|--------------------------------------------------------------------------
| Include DocBlocks from classes
|--------------------------------------------------------------------------
|
| Include DocBlocks from classes to allow additional code inspection for
| magic methods and properties.
|
*/
'include_class_docblocks' => false,
);
+4 -10
View File
@@ -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)
@@ -29,7 +29,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 +39,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
@@ -97,11 +91,11 @@ The `Illuminate/Support/helpers.php` is already set up, but you can add/remove y
### Automatic phpDocs for models
> You need to require `doctrine/dbal: ~2.3` in your own composer.json to get database columns.
> You need to add `doctrine/dbal: ~2.3` to require-dev in your own composer.json to get database columns.
```bash
composer require doctrine/dbal
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
+9 -3
View File
@@ -36,7 +36,10 @@ namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
//Method inherited from <?= $method->getDeclaringClass() ?>
<?php endif; ?>
<?= $method->shouldReturn() ? 'return ': '' ?><?= $method->getRoot() ?>::<?= $method->getRealName() ?>(<?= $method->getParams() ?>);
<?php if($method->isInstanceCall()):?>
/** @var <?=$method->getRoot()?> $instance */
<?php endif?>
<?= $method->shouldReturn() ? 'return ': '' ?><?= $method->getRootMethodCall() ?>;
}
<?php endforeach; ?>
}
@@ -57,8 +60,11 @@ namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
//Method inherited from <?= $method->getDeclaringClass() ?>
<?php endif; ?>
<?= $method->shouldReturn() ? 'return ': '' ?><?= $method->getRoot() ?>::<?= $method->getRealName() ?>(<?= $method->getParams() ?>);
<?php if($method->isInstanceCall()):?>
/** @var <?=$method->getRoot()?> $instance */
<?php endif?>
<?= $method->shouldReturn() ? 'return ': '' ?><?= $method->getRootMethodCall() ?>;
}
<?php endforeach; ?>
<?php endif; ?>}
+29 -8
View File
@@ -10,11 +10,13 @@
namespace Barryvdh\LaravelIdeHelper;
use Closure;
use ReflectionClass;
use Barryvdh\Reflection\DocBlock;
use Barryvdh\Reflection\DocBlock\Context;
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
use Barryvdh\Reflection\DocBlock\Tag\MethodTag;
use ReflectionClass;
use Illuminate\Config\Repository as ConfigRepository;
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
class Alias
{
@@ -35,17 +37,22 @@ class Alias
protected $interfaces = array();
protected $phpdoc = null;
/** @var ConfigRepository */
protected $config;
/**
* @param string $alias
* @param string $facade
* @param array $magicMethods
* @param array $interfaces
* @param ConfigRepository $config
* @param string $alias
* @param string $facade
* @param array $magicMethods
* @param array $interfaces
*/
public function __construct($alias, $facade, $magicMethods = array(), $interfaces = array())
public function __construct($config, $alias, $facade, $magicMethods = array(), $interfaces = array())
{
$this->alias = $alias;
$this->magicMethods = $magicMethods;
$this->interfaces = $interfaces;
$this->config = $config;
// Make the class absolute
$facade = '\\' . ltrim($facade, '\\');
@@ -389,6 +396,10 @@ class Alias
return new \ReflectionMethod($macro_func[0], $macro_func[1]);
}
if (is_object($macro_func) && is_callable($macro_func) && !$macro_func instanceof Closure) {
return new \ReflectionMethod($macro_func, '__invoke');
}
return new \ReflectionFunction($macro_func);
}
@@ -403,10 +414,20 @@ class Alias
$serializer = new DocBlockSerializer(1, $prefix);
if ($this->phpdoc) {
if ($this->config->get('ide-helper.include_class_docblocks')) {
// if a class doesn't expose any DocBlock tags
// we can perform reflection on the class and
// add in the original class DocBlock
if (count($this->phpdoc->getTags()) === 0) {
$class = new ReflectionClass($this->root);
$this->phpdoc = new DocBlock($class->getDocComment());
}
}
$this->removeDuplicateMethodsFromPhpDoc();
return $serializer->getDocComment($this->phpdoc);
}
return '';
}
+3 -2
View File
@@ -47,6 +47,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)',
@@ -90,7 +91,7 @@ class MetaCommand extends Command
if (is_object($concrete)) {
$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());
}
@@ -135,7 +136,7 @@ class MetaCommand extends Command
protected function registerClassAutoloadExceptions()
{
spl_autoload_register(function ($class) {
throw new \Exception("Class '$class' not found.");
throw new \ReflectionException("Class '$class' not found.");
});
}
+28 -17
View File
@@ -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,12 +446,17 @@ 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() ?: $this->getReturnTypeFromDocBlock($reflection));
$file = new \SplFileObject($reflection->getFileName());
$file->seek($reflection->getStartLine() - 1);
@@ -466,19 +471,19 @@ class ModelsCommand extends Command
$code = substr($code, $begin, strrpos($code, '}') - $begin + 1);
foreach (array(
'hasMany',
'hasManyThrough',
'belongsToMany',
'hasOne',
'belongsTo',
'morphOne',
'morphTo',
'morphMany',
'morphToMany',
'morphedByMany'
) as $relation) {
'hasMany' => '\Illuminate\Database\Eloquent\Relations\HasMany',
'hasManyThrough' => '\Illuminate\Database\Eloquent\Relations\HasManyThrough',
'belongsToMany' => '\Illuminate\Database\Eloquent\Relations\BelongsToMany',
'hasOne' => '\Illuminate\Database\Eloquent\Relations\HasOne',
'belongsTo' => '\Illuminate\Database\Eloquent\Relations\BelongsTo',
'morphOne' => '\Illuminate\Database\Eloquent\Relations\MorphOne',
'morphTo' => '\Illuminate\Database\Eloquent\Relations\MorphTo',
'morphMany' => '\Illuminate\Database\Eloquent\Relations\MorphMany',
'morphToMany' => '\Illuminate\Database\Eloquent\Relations\MorphToMany',
'morphedByMany' => '\Illuminate\Database\Eloquent\Relations\MorphToMany'
) as $relation => $impl) {
$search = '$this->' . $relation . '(';
if ($pos = stripos($code, $search)) {
if (stripos($code, $search) || stripos($impl, (string)$type) !== false) {
//Resolve the relation's model to a Relation object.
$methodReflection = new \ReflectionMethod($model, $method);
if ($methodReflection->getNumberOfParameters()) {
@@ -498,7 +503,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,
@@ -506,6 +511,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(
+1 -1
View File
@@ -203,7 +203,7 @@ class Generator
}
$magicMethods = array_key_exists($name, $this->magic) ? $this->magic[$name] : array();
$alias = new Alias($name, $facade, $magicMethods, $this->interfaces);
$alias = new Alias($this->config, $name, $facade, $magicMethods, $this->interfaces);
if ($alias->isValid()) {
//Add extra methods, from other classes (magic static calls)
if (array_key_exists($name, $this->extra)) {
+28 -3
View File
@@ -34,6 +34,7 @@ class Method
protected $interfaces = array();
protected $real_name;
protected $return = null;
protected $root;
/**
* @param \ReflectionMethod|\ReflectionFunctionAbstract $method
@@ -50,6 +51,9 @@ class Method
$this->real_name = $method->isClosure() ? $this->name : $method->name;
$this->initClassDefinedProperties($method, $class);
//Reference the 'real' function in the declaring class
$this->root = '\\' . ltrim($class->getName(), '\\');
//Create a DocBlock and serializer instance
$this->initPhpDoc($method);
@@ -66,9 +70,6 @@ class Method
//Make the method static
$this->phpdoc->appendTag(Tag::createInstance('@static', $this->phpdoc));
//Reference the 'real' function in the declaring class
$this->root = '\\' . ltrim($class->getName(), '\\');
}
/**
@@ -110,6 +111,26 @@ class Method
return $this->root;
}
/**
* @return bool
*/
public function isInstanceCall()
{
return ! ($this->method->isClosure() || $this->method->isStatic());
}
/**
* @return string
*/
public function getRootMethodCall()
{
if ($this->isInstanceCall()) {
return "\$instance->{$this->getRealName()}({$this->getParams()})";
} else {
return "{$this->getRoot()}::{$this->getRealName()}({$this->getParams()})";
}
}
/**
* Get the docblock for this method
*
@@ -243,6 +264,10 @@ class Method
// Set the changed content
$tag->setContent($returnValue . ' ' . $tag->getDescription());
$this->return = $returnValue;
if ($tag->getType() === '$this') {
$tag->setType($this->root);
}
} else {
$this->return = null;
}