mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 18:13:11 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f61602e7a | ||
|
|
c8ebf563a0 | ||
|
|
843067b907 | ||
|
|
f0f7051012 | ||
|
|
e1af93f43b | ||
|
|
b19b1d69ba | ||
|
|
92d7f2e706 | ||
|
|
f0c138c4fd | ||
|
|
b8d5fc3663 | ||
|
|
df994eb767 | ||
|
|
0cae143470 | ||
|
|
c2dec69d64 | ||
|
|
dedd69e8b4 | ||
|
|
40c02c524d | ||
|
|
04c1139223 | ||
|
|
172e430c12 | ||
|
|
244e936b74 | ||
|
|
f7f7e9e65a | ||
|
|
1a813703fe | ||
|
|
d58731cd9b | ||
|
|
b246d9ab23 | ||
|
|
8d8302ff6a | ||
|
|
202395b50f | ||
|
|
c5c9b2b951 | ||
|
|
c5e18beff1 | ||
|
|
6c4dcd9049 | ||
|
|
aaade4eab9 | ||
|
|
b21324b20e | ||
|
|
db83fb8b6b | ||
|
|
4f42af1fa1 | ||
|
|
f0959c1184 | ||
|
|
73c502d5c5 | ||
|
|
3376522d68 | ||
|
|
a83df51ad4 | ||
|
|
fd30d1190d | ||
|
|
4f13ba85bd | ||
|
|
0fee1c47d3 | ||
|
|
64a6b90258 | ||
|
|
25cc68c75f | ||
|
|
36c4406b3a | ||
|
|
9ba2f5c557 |
@@ -18,15 +18,8 @@ jobs:
|
|||||||
COMPOSER_NO_INTERACTION: 1
|
COMPOSER_NO_INTERACTION: 1
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
php: [8.0, 7.4, 7.3, 7.2]
|
php: [8.0, 7.4, 7.3]
|
||||||
lumen: [8.*, 7.*, 6.*]
|
lumen: [8.*]
|
||||||
exclude:
|
|
||||||
- lumen: 6.*
|
|
||||||
php: 8.0
|
|
||||||
- lumen: 7.*
|
|
||||||
php: 8.0
|
|
||||||
- lumen: 8.*
|
|
||||||
php: 7.2
|
|
||||||
name: P${{ matrix.php }} - Lumen${{ matrix.lumen }}
|
name: P${{ matrix.php }} - Lumen${{ matrix.lumen }}
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
@@ -82,11 +75,8 @@ jobs:
|
|||||||
COMPOSER_NO_INTERACTION: 1
|
COMPOSER_NO_INTERACTION: 1
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
php: [7.4, 7.3, 7.2]
|
php: [8.0, 7.4, 7.3]
|
||||||
laravel: [8.*, 7.*, 6.*]
|
laravel: [8.*]
|
||||||
exclude:
|
|
||||||
- laravel: 8.*
|
|
||||||
php: 7.2
|
|
||||||
name: P${{ matrix.php }} - Laravel${{ matrix.laravel }}
|
name: P${{ matrix.php }} - Laravel${{ matrix.laravel }}
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
|
|||||||
@@ -13,6 +13,13 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Setup PHP
|
||||||
|
uses: shivammathur/setup-php@v2
|
||||||
|
with:
|
||||||
|
php-version: 7.4
|
||||||
|
coverage: none
|
||||||
|
extensions: pdo_sqlite
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: composer install --prefer-dist --no-progress --no-suggest
|
run: composer install --prefer-dist --no-progress --no-suggest
|
||||||
|
|||||||
@@ -19,12 +19,10 @@ jobs:
|
|||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
php: [8.0, 7.4, 7.3, 7.2]
|
php: [8.0, 7.4, 7.3]
|
||||||
laravel: [8.*, 7.*, 6.*]
|
laravel: [8.*]
|
||||||
dependency-version: [prefer-lowest, prefer-stable]
|
dependency-version: [prefer-lowest, prefer-stable]
|
||||||
exclude:
|
exclude:
|
||||||
- laravel: 8.*
|
|
||||||
php: 7.2
|
|
||||||
- php: 8.0
|
- php: 8.0
|
||||||
dependency-version: prefer-lowest
|
dependency-version: prefer-lowest
|
||||||
|
|
||||||
|
|||||||
+2
-4
@@ -2,12 +2,10 @@
|
|||||||
|
|
||||||
// Share common rules between non-test and test files
|
// Share common rules between non-test and test files
|
||||||
return [
|
return [
|
||||||
// PSR12 from https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/4943
|
'@PSR12' => true,
|
||||||
'@PSR2' => true,
|
|
||||||
'blank_line_after_opening_tag' => true,
|
'blank_line_after_opening_tag' => true,
|
||||||
'braces' => [
|
'braces' => [
|
||||||
// Not-yet-implemented
|
'allow_single_line_anonymous_class_with_empty_body' => true,
|
||||||
// 'allow_single_line_anonymous_class_with_empty_body' => true,
|
|
||||||
],
|
],
|
||||||
'compact_nullable_typehint' => true,
|
'compact_nullable_typehint' => true,
|
||||||
'declare_equal_normalize' => true,
|
'declare_equal_normalize' => true,
|
||||||
|
|||||||
+34
-1
@@ -2,9 +2,42 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.8.1...master)
|
[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.9.3...master)
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
|
2021-04-01, 2.9.2
|
||||||
|
-----------------
|
||||||
|
### Added
|
||||||
|
- Model hooks for adding custom information from external sources to model classes through the ModelsCommand [\#945 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/945)
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- Fix ide-helper:models exception if model doesn't have factory [\#1196 / ahmed-aliraqi](https://github.com/barryvdh/laravel-ide-helper/pull/1196)
|
||||||
|
- Running tests triggering post_migrate hooks [\#1193 / netpok](https://github.com/barryvdh/laravel-ide-helper/pull/1193)
|
||||||
|
- Array_merge error when config is cached prior to package install [\#1184 / netpok](https://github.com/barryvdh/laravel-ide-helper/pull/1184)
|
||||||
|
|
||||||
|
2021-03-15, 2.9.1
|
||||||
|
-----------------
|
||||||
|
### Added
|
||||||
|
- Generate PHPDoc for Laravel 8.x factories [\#1074 / ahmed-aliraqi](https://github.com/barryvdh/laravel-ide-helper/pull/1074)
|
||||||
|
- Add a comment to a property like table columns [\#1168 / biiiiiigmonster](https://github.com/barryvdh/laravel-ide-helper/pull/1168)
|
||||||
|
- Added `post_migrate` hook to run commands after a migration [\#1163 / netpok](https://github.com/barryvdh/laravel-ide-helper/pull/1163)
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- Error when generating helper for invokable classes [\#1124 / standaniels](https://github.com/barryvdh/laravel-ide-helper/pull/1124)
|
||||||
|
- Fix broken ReflectionUnionTypes [\#1132 / def-studio](https://github.com/barryvdh/laravel-ide-helper/pull/1132)
|
||||||
|
- Relative class names are not converted to fully-qualified class names [\#1005 / SavKS](https://github.com/barryvdh/laravel-ide-helper/pull/1005)
|
||||||
|
|
||||||
|
2020-12-30, 2.9.0
|
||||||
|
-----------------
|
||||||
|
### Changed
|
||||||
|
- Dropped support for Laravel 6 and Laravel 7, as well as support for PHP 7.2 and added support for doctrine/dbal:^3 [\#1114 / mfn](https://github.com/barryvdh/laravel-ide-helper/pull/1114)
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- `Macro::initPhpDoc()` will save original docblock if present [\#1116 / LastDragon-ru](https://github.com/barryvdh/laravel-ide-helper/pull/1116)
|
||||||
|
- `Alias` will grab macros from `\Illuminate\Database\Eloquent\Builder` too [\#1118 / LastDragon-ru](https://github.com/barryvdh/laravel-ide-helper/pull/1118)
|
||||||
|
|
||||||
|
2020-12-08, 2.8.2
|
||||||
|
-----------------
|
||||||
### Added
|
### Added
|
||||||
- Fix phpdoc generate for custom cast with parameter [\#986 / artelkr](https://github.com/barryvdh/laravel-ide-helper/pull/986)
|
- Fix phpdoc generate for custom cast with parameter [\#986 / artelkr](https://github.com/barryvdh/laravel-ide-helper/pull/986)
|
||||||
- Created a possibility to add custom relation type [\#987 / efinder2](https://github.com/barryvdh/laravel-ide-helper/pull/987)
|
- Created a possibility to add custom relation type [\#987 / efinder2](https://github.com/barryvdh/laravel-ide-helper/pull/987)
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ Generation is done based on the files in your project, so they are always up-to-
|
|||||||
- [Usage](#usage)
|
- [Usage](#usage)
|
||||||
- [Automatic PHPDoc generation for Laravel Facades](#automatic-phpdoc-generation-for-laravel-facades)
|
- [Automatic PHPDoc generation for Laravel Facades](#automatic-phpdoc-generation-for-laravel-facades)
|
||||||
- [Automatic PHPDocs for models](#automatic-phpdocs-for-models)
|
- [Automatic PHPDocs for models](#automatic-phpdocs-for-models)
|
||||||
|
- [Model Directories](#model-directories)
|
||||||
|
- [Ignore Models](#ignore-models)
|
||||||
|
- [Model Hooks](#model-hooks)
|
||||||
- [Automatic PHPDocs generation for Laravel Fluent methods](#automatic-phpdocs-generation-for-laravel-fluent-methods)
|
- [Automatic PHPDocs generation for Laravel Fluent methods](#automatic-phpdocs-generation-for-laravel-fluent-methods)
|
||||||
- [Auto-completion for factory builders](#auto-completion-for-factory-builders)
|
- [Auto-completion for factory builders](#auto-completion-for-factory-builders)
|
||||||
- [PhpStorm Meta for Container instances](#phpstorm-meta-for-container-instances)
|
- [PhpStorm Meta for Container instances](#phpstorm-meta-for-container-instances)
|
||||||
@@ -175,6 +178,8 @@ With the `--write-mixin (-M)` option
|
|||||||
*/
|
*/
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Model Directories
|
||||||
|
|
||||||
By default, models in `app/models` are scanned. The optional argument tells what models to use (also outside app/models).
|
By default, models in `app/models` are scanned. The optional argument tells what models to use (also outside app/models).
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -189,6 +194,8 @@ php artisan ide-helper:models --dir="path/to/models" --dir="app/src/Model"
|
|||||||
|
|
||||||
You can publish the config file (`php artisan vendor:publish`) and set the default directories.
|
You can publish the config file (`php artisan vendor:publish`) and set the default directories.
|
||||||
|
|
||||||
|
#### Ignore Models
|
||||||
|
|
||||||
Models can be ignored using the `--ignore (-I)` option
|
Models can be ignored using the `--ignore (-I)` option
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -206,7 +213,7 @@ Or can be ignored by setting the `ignored_models` config
|
|||||||
|
|
||||||
#### Magic `where*` methods
|
#### Magic `where*` methods
|
||||||
|
|
||||||
Eloquent allows calling `where<Attribute>` on your modes, e.g. `Post::whereTitle(…)` and automatically translates this to e.g. `Post::where('title', '=', '…')`.
|
Eloquent allows calling `where<Attribute>` on your models, e.g. `Post::whereTitle(…)` and automatically translates this to e.g. `Post::where('title', '=', '…')`.
|
||||||
|
|
||||||
If for some reason it's undesired to have them generated (one for each column), you can disable this via config `write_model_magic_where` and setting it to `false`.
|
If for some reason it's undesired to have them generated (one for each column), you can disable this via config `write_model_magic_where` and setting it to `false`.
|
||||||
|
|
||||||
@@ -216,6 +223,33 @@ You may use the [`::withCount`](https://laravel.com/docs/master/eloquent-relatio
|
|||||||
|
|
||||||
By default, these attributes are generated in the phpdoc. You can turn them off by setting the config `write_model_relation_count_properties` to `false`.
|
By default, these attributes are generated in the phpdoc. You can turn them off by setting the config `write_model_relation_count_properties` to `false`.
|
||||||
|
|
||||||
|
#### Support `@comment` based on DocBlock
|
||||||
|
|
||||||
|
In order to better support IDEs, relations and getters/setters can also add a comment to a property like table columns. Therefore a custom docblock `@comment` is used:
|
||||||
|
```php
|
||||||
|
class Users extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @comment Get User's full name
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getFullNameAttribute(): string
|
||||||
|
{
|
||||||
|
return $this->first_name . ' ' .$this->last_name ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// => after generate models
|
||||||
|
|
||||||
|
/**
|
||||||
|
* App\Models\Users
|
||||||
|
*
|
||||||
|
* @property-read string $full_name Get User's full name
|
||||||
|
* …
|
||||||
|
*/
|
||||||
|
```
|
||||||
|
|
||||||
#### Dedicated Eloquent Builder methods
|
#### Dedicated Eloquent Builder methods
|
||||||
|
|
||||||
A new method to the eloquent models was added called `newEloquentBuilder` [Reference](https://timacdonald.me/dedicated-eloquent-model-query-builders/) where we can
|
A new method to the eloquent models was added called `newEloquentBuilder` [Reference](https://timacdonald.me/dedicated-eloquent-model-query-builders/) where we can
|
||||||
@@ -243,6 +277,42 @@ For those special cases, you can map them via the config `custom_db_types`. Exam
|
|||||||
],
|
],
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Model Hooks
|
||||||
|
|
||||||
|
If you need additional information on your model from sources that are not handled by default, you can hook in to the
|
||||||
|
generation process with model hooks to add extra information on the fly.
|
||||||
|
Simply create a class that implements `ModelHookInterface` and add it to the `model_hooks` array in the config:
|
||||||
|
|
||||||
|
```php
|
||||||
|
'model_hooks' => [
|
||||||
|
MyCustomHook::class,
|
||||||
|
],
|
||||||
|
```
|
||||||
|
|
||||||
|
The `run` method will be called during generation for every model and receives the current running `ModelsCommand` and the current `Model`, e.g.:
|
||||||
|
|
||||||
|
```php
|
||||||
|
class MyCustomHook implements ModelHookInterface
|
||||||
|
{
|
||||||
|
public function run(ModelsCommand $command, Model $model): void
|
||||||
|
{
|
||||||
|
if (! $model instanceof MyModel) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$command->setProperty('custom', 'string', true, false, 'My custom property');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```php
|
||||||
|
/**
|
||||||
|
* MyModel
|
||||||
|
*
|
||||||
|
* @property integer $id
|
||||||
|
* @property-read string $custom
|
||||||
|
```
|
||||||
|
|
||||||
### Automatic PHPDocs generation for Laravel Fluent methods
|
### Automatic PHPDocs generation for Laravel Fluent methods
|
||||||
|
|
||||||
If you need PHPDocs support for Fluent methods in migration, for example
|
If you need PHPDocs support for Fluent methods in migration, for example
|
||||||
|
|||||||
+15
-11
@@ -20,33 +20,37 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.2",
|
"php": "^7.3 || ^8.0",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"barryvdh/reflection-docblock": "^2.0.6",
|
"barryvdh/reflection-docblock": "^2.0.6",
|
||||||
"composer/composer": "^1.6 || ^2",
|
"composer/composer": "^1.6 || ^2",
|
||||||
"doctrine/dbal": "~2.3",
|
"doctrine/dbal": "^2.6 || ^3",
|
||||||
"illuminate/console": "^6 || ^7 || ^8",
|
"illuminate/console": "^8",
|
||||||
"illuminate/filesystem": "^6 || ^7 || ^8",
|
"illuminate/filesystem": "^8",
|
||||||
"illuminate/support": "^6 || ^7 || ^8",
|
"illuminate/support": "^8",
|
||||||
|
"nikic/php-parser": "^4.7",
|
||||||
"phpdocumentor/type-resolver": "^1.1.0"
|
"phpdocumentor/type-resolver": "^1.1.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"ext-pdo_sqlite": "*",
|
"ext-pdo_sqlite": "*",
|
||||||
"friendsofphp/php-cs-fixer": "^2",
|
"friendsofphp/php-cs-fixer": "^2",
|
||||||
"illuminate/config": "^6 || ^7 || ^8",
|
"illuminate/config": "^8",
|
||||||
"illuminate/view": "^6 || ^7 || ^8",
|
"illuminate/view": "^8",
|
||||||
"mockery/mockery": "^1.3.3",
|
"mockery/mockery": "^1.4",
|
||||||
"orchestra/testbench": "^4 || ^5 || ^6",
|
"orchestra/testbench": "^6",
|
||||||
"phpunit/phpunit": "^8.5 || ^9",
|
"phpunit/phpunit": "^8.5 || ^9",
|
||||||
"spatie/phpunit-snapshot-assertions": "^1.4 || ^2.2 || ^3 || ^4",
|
"spatie/phpunit-snapshot-assertions": "^3 || ^4",
|
||||||
"vimeo/psalm": "^3.12"
|
"vimeo/psalm": "^3.12"
|
||||||
},
|
},
|
||||||
|
"suggest": {
|
||||||
|
"illuminate/events": "Required for automatic helper generation (^6|^7|^8)."
|
||||||
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"sort-packages": true
|
"sort-packages": true
|
||||||
},
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
"dev-master": "2.8-dev"
|
"dev-master": "2.9-dev"
|
||||||
},
|
},
|
||||||
"laravel": {
|
"laravel": {
|
||||||
"providers": [
|
"providers": [
|
||||||
|
|||||||
@@ -144,6 +144,21 @@ return [
|
|||||||
|
|
||||||
],
|
],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Models hooks
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| Define which hook classes you want to run for models to add custom information
|
||||||
|
|
|
||||||
|
| Hooks should implement Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
'model_hooks' => [
|
||||||
|
// App\Support\IdeHelper\MyModelHook::class
|
||||||
|
],
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Extra classes
|
| Extra classes
|
||||||
@@ -278,4 +293,16 @@ return [
|
|||||||
*/
|
*/
|
||||||
'additional_relation_types' => [],
|
'additional_relation_types' => [],
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Run artisan commands after migrations to generate model helpers
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| The specified commands should run after migrations are finished running.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
'post_migrate' => [
|
||||||
|
// 'ide-helper:models --nowrite',
|
||||||
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -17,9 +17,4 @@
|
|||||||
<code>\Storage</code>
|
<code>\Storage</code>
|
||||||
</UndefinedClass>
|
</UndefinedClass>
|
||||||
</file>
|
</file>
|
||||||
<file src="src/IdeHelperServiceProvider.php">
|
|
||||||
<TooFewArguments occurrences="1">
|
|
||||||
<code>new PhpEngine()</code>
|
|
||||||
</TooFewArguments>
|
|
||||||
</file>
|
|
||||||
</files>
|
</files>
|
||||||
|
|||||||
+13
-8
@@ -17,6 +17,7 @@ use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
|||||||
use Barryvdh\Reflection\DocBlock\Tag\MethodTag;
|
use Barryvdh\Reflection\DocBlock\Tag\MethodTag;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Config\Repository as ConfigRepository;
|
use Illuminate\Config\Repository as ConfigRepository;
|
||||||
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||||
use Illuminate\Support\Facades\Facade;
|
use Illuminate\Support\Facades\Facade;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
|
|
||||||
@@ -39,6 +40,7 @@ class Alias
|
|||||||
protected $magicMethods = [];
|
protected $magicMethods = [];
|
||||||
protected $interfaces = [];
|
protected $interfaces = [];
|
||||||
protected $phpdoc = null;
|
protected $phpdoc = null;
|
||||||
|
protected $classAliases = [];
|
||||||
|
|
||||||
/** @var ConfigRepository */
|
/** @var ConfigRepository */
|
||||||
protected $config;
|
protected $config;
|
||||||
@@ -77,10 +79,11 @@ class Alias
|
|||||||
$this->detectExtendsNamespace();
|
$this->detectExtendsNamespace();
|
||||||
|
|
||||||
if (!empty($this->namespace)) {
|
if (!empty($this->namespace)) {
|
||||||
//Create a DocBlock and serializer instance
|
$this->classAliases = (new UsesResolver())->loadFromClass($this->root);
|
||||||
$this->phpdoc = new DocBlock(new ReflectionClass($alias), new Context($this->namespace));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
//Create a DocBlock and serializer instance
|
||||||
|
$this->phpdoc = new DocBlock(new ReflectionClass($alias), new Context($this->namespace, $this->classAliases));
|
||||||
|
}
|
||||||
|
|
||||||
if ($facade === '\Illuminate\Database\Eloquent\Model') {
|
if ($facade === '\Illuminate\Database\Eloquent\Model') {
|
||||||
$this->usedMethods = ['decrement', 'increment'];
|
$this->usedMethods = ['decrement', 'increment'];
|
||||||
@@ -329,7 +332,7 @@ class Alias
|
|||||||
|
|
||||||
if (!in_array($magic, $this->usedMethods)) {
|
if (!in_array($magic, $this->usedMethods)) {
|
||||||
if ($class !== $this->root) {
|
if ($class !== $this->root) {
|
||||||
$this->methods[] = new Method($method, $this->alias, $class, $magic, $this->interfaces);
|
$this->methods[] = new Method($method, $this->alias, $class, $magic, $this->interfaces, $this->classAliases);
|
||||||
}
|
}
|
||||||
$this->usedMethods[] = $magic;
|
$this->usedMethods[] = $magic;
|
||||||
}
|
}
|
||||||
@@ -343,7 +346,6 @@ class Alias
|
|||||||
*/
|
*/
|
||||||
protected function detectMethods()
|
protected function detectMethods()
|
||||||
{
|
{
|
||||||
|
|
||||||
foreach ($this->classes as $class) {
|
foreach ($this->classes as $class) {
|
||||||
$reflection = new \ReflectionClass($class);
|
$reflection = new \ReflectionClass($class);
|
||||||
|
|
||||||
@@ -359,7 +361,8 @@ class Alias
|
|||||||
$this->alias,
|
$this->alias,
|
||||||
$reflection,
|
$reflection,
|
||||||
$method->name,
|
$method->name,
|
||||||
$this->interfaces
|
$this->interfaces,
|
||||||
|
$this->classAliases
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$this->usedMethods[] = $method->name;
|
$this->usedMethods[] = $method->name;
|
||||||
@@ -368,8 +371,9 @@ class Alias
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if the class is macroable
|
// Check if the class is macroable
|
||||||
|
// (Eloquent\Builder is also macroable but doesn't use Macroable trait)
|
||||||
$traits = collect($reflection->getTraitNames());
|
$traits = collect($reflection->getTraitNames());
|
||||||
if ($traits->contains('Illuminate\Support\Traits\Macroable')) {
|
if ($traits->contains('Illuminate\Support\Traits\Macroable') || $class === EloquentBuilder::class) {
|
||||||
$properties = $reflection->getStaticProperties();
|
$properties = $reflection->getStaticProperties();
|
||||||
$macros = isset($properties['macros']) ? $properties['macros'] : [];
|
$macros = isset($properties['macros']) ? $properties['macros'] : [];
|
||||||
foreach ($macros as $macro_name => $macro_func) {
|
foreach ($macros as $macro_name => $macro_func) {
|
||||||
@@ -380,7 +384,8 @@ class Alias
|
|||||||
$this->alias,
|
$this->alias,
|
||||||
$reflection,
|
$reflection,
|
||||||
$macro_name,
|
$macro_name,
|
||||||
$this->interfaces
|
$this->interfaces,
|
||||||
|
$this->classAliases
|
||||||
);
|
);
|
||||||
$this->usedMethods[] = $macro_name;
|
$this->usedMethods[] = $macro_name;
|
||||||
}
|
}
|
||||||
|
|||||||
+154
-32
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Barryvdh\LaravelIdeHelper\Console;
|
namespace Barryvdh\LaravelIdeHelper\Console;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
|
||||||
use Barryvdh\Reflection\DocBlock;
|
use Barryvdh\Reflection\DocBlock;
|
||||||
use Barryvdh\Reflection\DocBlock\Context;
|
use Barryvdh\Reflection\DocBlock\Context;
|
||||||
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
||||||
@@ -18,6 +19,7 @@ use Barryvdh\Reflection\DocBlock\Tag;
|
|||||||
use Composer\Autoload\ClassMapGenerator;
|
use Composer\Autoload\ClassMapGenerator;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
@@ -34,7 +36,9 @@ use Illuminate\Filesystem\Filesystem;
|
|||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use phpDocumentor\Reflection\Types\ContextFactory;
|
use phpDocumentor\Reflection\Types\ContextFactory;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
|
use ReflectionNamedType;
|
||||||
use ReflectionObject;
|
use ReflectionObject;
|
||||||
|
use ReflectionType;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
@@ -209,8 +213,6 @@ class ModelsCommand extends Command
|
|||||||
|
|
||||||
protected function generateDocs($loadModels, $ignore = '')
|
protected function generateDocs($loadModels, $ignore = '')
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
$output = "<?php
|
$output = "<?php
|
||||||
|
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
@@ -277,6 +279,10 @@ class ModelsCommand extends Command
|
|||||||
$this->getPropertiesFromMethods($model);
|
$this->getPropertiesFromMethods($model);
|
||||||
$this->getSoftDeleteMethods($model);
|
$this->getSoftDeleteMethods($model);
|
||||||
$this->getCollectionMethods($model);
|
$this->getCollectionMethods($model);
|
||||||
|
$this->getFactoryMethods($model);
|
||||||
|
|
||||||
|
$this->runModelHooks($model);
|
||||||
|
|
||||||
$output .= $this->createPhpDocs($name);
|
$output .= $this->createPhpDocs($name);
|
||||||
$ignore[] = $name;
|
$ignore[] = $name;
|
||||||
$this->nullableColumns = [];
|
$this->nullableColumns = [];
|
||||||
@@ -309,9 +315,8 @@ class ModelsCommand extends Command
|
|||||||
|
|
||||||
$dirs = glob($dir, GLOB_ONLYDIR);
|
$dirs = glob($dir, GLOB_ONLYDIR);
|
||||||
foreach ($dirs as $dir) {
|
foreach ($dirs as $dir) {
|
||||||
|
|
||||||
if (!is_dir($dir)) {
|
if (!is_dir($dir)) {
|
||||||
$this->error("Cannot locate directory '{'$dir}'");
|
$this->error("Cannot locate directory '{$dir}'");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,7 +340,7 @@ class ModelsCommand extends Command
|
|||||||
*
|
*
|
||||||
* @param \Illuminate\Database\Eloquent\Model $model
|
* @param \Illuminate\Database\Eloquent\Model $model
|
||||||
*/
|
*/
|
||||||
protected function castPropertiesType($model)
|
public function castPropertiesType($model)
|
||||||
{
|
{
|
||||||
$casts = $model->getCasts();
|
$casts = $model->getCasts();
|
||||||
foreach ($casts as $name => $type) {
|
foreach ($casts as $name => $type) {
|
||||||
@@ -411,7 +416,7 @@ class ModelsCommand extends Command
|
|||||||
*
|
*
|
||||||
* @param \Illuminate\Database\Eloquent\Model $model
|
* @param \Illuminate\Database\Eloquent\Model $model
|
||||||
*/
|
*/
|
||||||
protected function getPropertiesFromTable($model)
|
public function getPropertiesFromTable($model)
|
||||||
{
|
{
|
||||||
$table = $model->getConnection()->getTablePrefix() . $model->getTable();
|
$table = $model->getConnection()->getTablePrefix() . $model->getTable();
|
||||||
$schema = $model->getConnection()->getDoctrineSchemaManager();
|
$schema = $model->getConnection()->getDoctrineSchemaManager();
|
||||||
@@ -508,7 +513,7 @@ class ModelsCommand extends Command
|
|||||||
/**
|
/**
|
||||||
* @param \Illuminate\Database\Eloquent\Model $model
|
* @param \Illuminate\Database\Eloquent\Model $model
|
||||||
*/
|
*/
|
||||||
protected function getPropertiesFromMethods($model)
|
public function getPropertiesFromMethods($model)
|
||||||
{
|
{
|
||||||
$methods = get_class_methods($model);
|
$methods = get_class_methods($model);
|
||||||
if ($methods) {
|
if ($methods) {
|
||||||
@@ -526,7 +531,8 @@ class ModelsCommand extends Command
|
|||||||
$reflection = new \ReflectionMethod($model, $method);
|
$reflection = new \ReflectionMethod($model, $method);
|
||||||
$type = $this->getReturnType($reflection);
|
$type = $this->getReturnType($reflection);
|
||||||
$type = $this->getTypeInModel($model, $type);
|
$type = $this->getTypeInModel($model, $type);
|
||||||
$this->setProperty($name, $type, true, null);
|
$comment = $this->getCommentFromDocBlock($reflection);
|
||||||
|
$this->setProperty($name, $type, true, null, $comment);
|
||||||
}
|
}
|
||||||
} elseif (
|
} elseif (
|
||||||
Str::startsWith($method, 'set') && Str::endsWith(
|
Str::startsWith($method, 'set') && Str::endsWith(
|
||||||
@@ -537,13 +543,16 @@ class ModelsCommand extends Command
|
|||||||
//Magic set<name>Attribute
|
//Magic set<name>Attribute
|
||||||
$name = Str::snake(substr($method, 3, -9));
|
$name = Str::snake(substr($method, 3, -9));
|
||||||
if (!empty($name)) {
|
if (!empty($name)) {
|
||||||
$this->setProperty($name, null, null, true);
|
$reflection = new \ReflectionMethod($model, $method);
|
||||||
|
$comment = $this->getCommentFromDocBlock($reflection);
|
||||||
|
$this->setProperty($name, null, null, true, $comment);
|
||||||
}
|
}
|
||||||
} elseif (Str::startsWith($method, 'scope') && $method !== 'scopeQuery') {
|
} elseif (Str::startsWith($method, 'scope') && $method !== 'scopeQuery') {
|
||||||
//Magic set<name>Attribute
|
//Magic set<name>Attribute
|
||||||
$name = Str::camel(substr($method, 5));
|
$name = Str::camel(substr($method, 5));
|
||||||
if (!empty($name)) {
|
if (!empty($name)) {
|
||||||
$reflection = new \ReflectionMethod($model, $method);
|
$reflection = new \ReflectionMethod($model, $method);
|
||||||
|
$comment = $this->getCommentFromDocBlock($reflection);
|
||||||
$args = $this->getParameters($reflection);
|
$args = $this->getParameters($reflection);
|
||||||
//Remove the first ($query) argument
|
//Remove the first ($query) argument
|
||||||
array_shift($args);
|
array_shift($args);
|
||||||
@@ -555,7 +564,7 @@ class ModelsCommand extends Command
|
|||||||
$reflection->getDeclaringClass(),
|
$reflection->getDeclaringClass(),
|
||||||
$reflection->getDeclaringClass()->getName()
|
$reflection->getDeclaringClass()->getName()
|
||||||
);
|
);
|
||||||
$this->setMethod($name, $builder . '|' . $modelName, $args);
|
$this->setMethod($name, $builder . '|' . $modelName, $args, $comment);
|
||||||
}
|
}
|
||||||
} elseif (in_array($method, ['query', 'newQuery', 'newModelQuery'])) {
|
} elseif (in_array($method, ['query', 'newQuery', 'newModelQuery'])) {
|
||||||
$builder = $this->getClassNameInDestinationFile($model, get_class($model->newModelQuery()));
|
$builder = $this->getClassNameInDestinationFile($model, get_class($model->newModelQuery()));
|
||||||
@@ -576,7 +585,7 @@ class ModelsCommand extends Command
|
|||||||
$reflection = new \ReflectionMethod($model, $method);
|
$reflection = new \ReflectionMethod($model, $method);
|
||||||
|
|
||||||
if ($returnType = $reflection->getReturnType()) {
|
if ($returnType = $reflection->getReturnType()) {
|
||||||
$type = $returnType instanceof \ReflectionNamedType
|
$type = $returnType instanceof ReflectionNamedType
|
||||||
? $returnType->getName()
|
? $returnType->getName()
|
||||||
: (string)$returnType;
|
: (string)$returnType;
|
||||||
} else {
|
} else {
|
||||||
@@ -607,6 +616,7 @@ class ModelsCommand extends Command
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$comment = $this->getCommentFromDocBlock($reflection);
|
||||||
// Adding constraints requires reading model properties which
|
// Adding constraints requires reading model properties which
|
||||||
// can cause errors. Since we don't need constraints we can
|
// can cause errors. Since we don't need constraints we can
|
||||||
// disable them when we fetch the relation to avoid errors.
|
// disable them when we fetch the relation to avoid errors.
|
||||||
@@ -638,7 +648,8 @@ class ModelsCommand extends Command
|
|||||||
$method,
|
$method,
|
||||||
$collectionClassNameInModel . '|' . $relatedModel . '[]',
|
$collectionClassNameInModel . '|' . $relatedModel . '[]',
|
||||||
true,
|
true,
|
||||||
null
|
null,
|
||||||
|
$comment
|
||||||
);
|
);
|
||||||
if ($this->write_model_relation_count_properties) {
|
if ($this->write_model_relation_count_properties) {
|
||||||
$this->setProperty(
|
$this->setProperty(
|
||||||
@@ -646,6 +657,7 @@ class ModelsCommand extends Command
|
|||||||
'int|null',
|
'int|null',
|
||||||
true,
|
true,
|
||||||
false
|
false
|
||||||
|
// What kind of comments should be added to the relation count here?
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} elseif ($relation === 'morphTo') {
|
} elseif ($relation === 'morphTo') {
|
||||||
@@ -654,7 +666,8 @@ class ModelsCommand extends Command
|
|||||||
$method,
|
$method,
|
||||||
$this->getClassNameInDestinationFile($model, Model::class) . '|\Eloquent',
|
$this->getClassNameInDestinationFile($model, Model::class) . '|\Eloquent',
|
||||||
true,
|
true,
|
||||||
null
|
null,
|
||||||
|
$comment
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
//Single model is returned
|
//Single model is returned
|
||||||
@@ -663,7 +676,7 @@ class ModelsCommand extends Command
|
|||||||
$relatedModel,
|
$relatedModel,
|
||||||
true,
|
true,
|
||||||
null,
|
null,
|
||||||
'',
|
$comment,
|
||||||
$this->isRelationNullable($relation, $relationObj)
|
$this->isRelationNullable($relation, $relationObj)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -712,7 +725,7 @@ class ModelsCommand extends Command
|
|||||||
* @param string|null $comment
|
* @param string|null $comment
|
||||||
* @param bool $nullable
|
* @param bool $nullable
|
||||||
*/
|
*/
|
||||||
protected function setProperty($name, $type = null, $read = null, $write = null, $comment = '', $nullable = false)
|
public function setProperty($name, $type = null, $read = null, $write = null, $comment = '', $nullable = false)
|
||||||
{
|
{
|
||||||
if (!isset($this->properties[$name])) {
|
if (!isset($this->properties[$name])) {
|
||||||
$this->properties[$name] = [];
|
$this->properties[$name] = [];
|
||||||
@@ -736,7 +749,7 @@ class ModelsCommand extends Command
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setMethod($name, $type = '', $arguments = [])
|
protected function setMethod($name, $type = '', $arguments = [], $comment = '')
|
||||||
{
|
{
|
||||||
$methods = array_change_key_case($this->methods, CASE_LOWER);
|
$methods = array_change_key_case($this->methods, CASE_LOWER);
|
||||||
|
|
||||||
@@ -744,6 +757,7 @@ class ModelsCommand extends Command
|
|||||||
$this->methods[$name] = [];
|
$this->methods[$name] = [];
|
||||||
$this->methods[$name]['type'] = $type;
|
$this->methods[$name]['type'] = $type;
|
||||||
$this->methods[$name]['arguments'] = $arguments;
|
$this->methods[$name]['arguments'] = $arguments;
|
||||||
|
$this->methods[$name]['comment'] = $comment;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -753,7 +767,6 @@ class ModelsCommand extends Command
|
|||||||
*/
|
*/
|
||||||
protected function createPhpDocs($class)
|
protected function createPhpDocs($class)
|
||||||
{
|
{
|
||||||
|
|
||||||
$reflection = new ReflectionClass($class);
|
$reflection = new ReflectionClass($class);
|
||||||
$namespace = $reflection->getNamespaceName();
|
$namespace = $reflection->getNamespaceName();
|
||||||
$classname = $reflection->getShortName();
|
$classname = $reflection->getShortName();
|
||||||
@@ -820,7 +833,11 @@ class ModelsCommand extends Command
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$arguments = implode(', ', $method['arguments']);
|
$arguments = implode(', ', $method['arguments']);
|
||||||
$tag = Tag::createInstance("@method static {$method['type']} {$name}({$arguments})", $phpdoc);
|
$tagLine = "@method static {$method['type']} {$name}({$arguments})";
|
||||||
|
if ($method['comment'] !== '') {
|
||||||
|
$tagLine .= " {$method['comment']}";
|
||||||
|
}
|
||||||
|
$tag = Tag::createInstance($tagLine, $phpdoc);
|
||||||
$phpdoc->appendTag($tag);
|
$phpdoc->appendTag($tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -980,6 +997,30 @@ class ModelsCommand extends Command
|
|||||||
return $this->getReturnTypeFromReflection($reflection);
|
return $this->getReturnTypeFromReflection($reflection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get method comment based on it DocBlock comment
|
||||||
|
*
|
||||||
|
* @param \ReflectionMethod $reflection
|
||||||
|
*
|
||||||
|
* @return null|string
|
||||||
|
*/
|
||||||
|
protected function getCommentFromDocBlock(\ReflectionMethod $reflection)
|
||||||
|
{
|
||||||
|
$phpDocContext = (new ContextFactory())->createFromReflector($reflection);
|
||||||
|
$context = new Context(
|
||||||
|
$phpDocContext->getNamespace(),
|
||||||
|
$phpDocContext->getNamespaceAliases()
|
||||||
|
);
|
||||||
|
$comment = '';
|
||||||
|
$phpdoc = new DocBlock($reflection, $context);
|
||||||
|
|
||||||
|
if ($phpdoc->hasTag('comment')) {
|
||||||
|
$comment = $phpdoc->getTagsByName('comment')[0]->getContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $comment;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get method return type based on it DocBlock comment
|
* Get method return type based on it DocBlock comment
|
||||||
*
|
*
|
||||||
@@ -1011,16 +1052,12 @@ class ModelsCommand extends Command
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$type = $returnType instanceof \ReflectionNamedType
|
$types = $this->extractReflectionTypes($returnType);
|
||||||
? $returnType->getName()
|
|
||||||
: (string)$returnType;
|
|
||||||
|
|
||||||
if (!$returnType->isBuiltin()) {
|
$type = implode('|', $types);
|
||||||
$type = '\\' . $type;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($returnType->allowsNull()) {
|
if ($returnType->allowsNull()) {
|
||||||
$type .= '|null';
|
$type .='|null';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $type;
|
return $type;
|
||||||
@@ -1043,6 +1080,39 @@ class ModelsCommand extends Command
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate factory method from "HasFactory" trait.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Database\Eloquent\Model $model
|
||||||
|
*/
|
||||||
|
protected function getFactoryMethods($model)
|
||||||
|
{
|
||||||
|
if (!class_exists(Factory::class)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$modelName = get_class($model);
|
||||||
|
|
||||||
|
$traits = class_uses($modelName, true);
|
||||||
|
if (!in_array('Illuminate\\Database\\Eloquent\\Factories\\HasFactory', $traits)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($modelName::newFactory()) {
|
||||||
|
$factory = get_class($modelName::newFactory());
|
||||||
|
} else {
|
||||||
|
$factory = Factory::resolveFactoryName($modelName);
|
||||||
|
}
|
||||||
|
|
||||||
|
$factory = '\\' . trim($factory, '\\');
|
||||||
|
|
||||||
|
if (!class_exists($factory)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->setMethod('factory', $factory, ['...$parameters']);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates methods that return collections
|
* Generates methods that return collections
|
||||||
* @param \Illuminate\Database\Eloquent\Model $model
|
* @param \Illuminate\Database\Eloquent\Model $model
|
||||||
@@ -1101,7 +1171,7 @@ class ModelsCommand extends Command
|
|||||||
$reflectionType = $this->getReturnTypeFromDocBlock($methodReflection);
|
$reflectionType = $this->getReturnTypeFromDocBlock($methodReflection);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($reflectionType === 'static' || $reflectionType === '$this') {
|
if ($reflectionType === 'static' || $reflectionType === '$this') {
|
||||||
$reflectionType = $type;
|
$reflectionType = $type;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1186,17 +1256,19 @@ class ModelsCommand extends Command
|
|||||||
protected function getParamType(\ReflectionMethod $method, \ReflectionParameter $parameter): ?string
|
protected function getParamType(\ReflectionMethod $method, \ReflectionParameter $parameter): ?string
|
||||||
{
|
{
|
||||||
if ($paramType = $parameter->getType()) {
|
if ($paramType = $parameter->getType()) {
|
||||||
$parameterName = $paramType->getName();
|
$types = $this->extractReflectionTypes($paramType);
|
||||||
|
|
||||||
if (!$paramType->isBuiltin()) {
|
$type = implode('|', $types);
|
||||||
$parameterName = '\\' . $parameterName;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($paramType->allowsNull()) {
|
if ($paramType->allowsNull()) {
|
||||||
return '?' . $parameterName;
|
if (count($types)==1) {
|
||||||
|
$type = '?' . $type;
|
||||||
|
} else {
|
||||||
|
$type .='|null';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $parameterName;
|
return $type;
|
||||||
}
|
}
|
||||||
|
|
||||||
$docComment = $method->getDocComment();
|
$docComment = $method->getDocComment();
|
||||||
@@ -1261,4 +1333,54 @@ class ModelsCommand extends Command
|
|||||||
// then we have found the type of the variable if not we return null
|
// then we have found the type of the variable if not we return null
|
||||||
return $type;
|
return $type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function extractReflectionTypes(ReflectionType $reflection_type)
|
||||||
|
{
|
||||||
|
if ($reflection_type instanceof ReflectionNamedType) {
|
||||||
|
$types[] = $this->getReflectionNamedType($reflection_type);
|
||||||
|
} else {
|
||||||
|
$types = [];
|
||||||
|
foreach ($reflection_type->getTypes() as $named_type) {
|
||||||
|
if ($named_type->getName()==='null') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$types[] = $this->getReflectionNamedType($named_type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $types;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getReflectionNamedType(ReflectionNamedType $paramType): string
|
||||||
|
{
|
||||||
|
$parameterName = $paramType->getName();
|
||||||
|
if (!$paramType->isBuiltin()) {
|
||||||
|
$parameterName = '\\' . $parameterName;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $parameterName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Illuminate\Database\Eloquent\Model $model
|
||||||
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
||||||
|
* @throws \RuntimeException
|
||||||
|
*/
|
||||||
|
protected function runModelHooks($model): void
|
||||||
|
{
|
||||||
|
$hooks = $this->laravel['config']->get('ide-helper.model_hooks', []);
|
||||||
|
|
||||||
|
foreach ($hooks as $hook) {
|
||||||
|
$hookInstance = $this->laravel->make($hook);
|
||||||
|
|
||||||
|
if (!$hookInstance instanceof ModelHookInterface) {
|
||||||
|
throw new \RuntimeException(
|
||||||
|
'Your IDE helper model hook must implement Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$hookInstance->run($this, $model);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Contracts;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
interface ModelHookInterface
|
||||||
|
{
|
||||||
|
public function run(ModelsCommand $command, Model $model): void;
|
||||||
|
}
|
||||||
+3
-3
@@ -54,9 +54,9 @@ class Generator
|
|||||||
// Find the drivers to add to the extra/interfaces
|
// Find the drivers to add to the extra/interfaces
|
||||||
$this->detectDrivers();
|
$this->detectDrivers();
|
||||||
|
|
||||||
$this->extra = array_merge($this->extra, $this->config->get('ide-helper.extra'));
|
$this->extra = array_merge($this->extra, $this->config->get('ide-helper.extra'), []);
|
||||||
$this->magic = array_merge($this->magic, $this->config->get('ide-helper.magic'));
|
$this->magic = array_merge($this->magic, $this->config->get('ide-helper.magic'), []);
|
||||||
$this->interfaces = array_merge($this->interfaces, $this->config->get('ide-helper.interfaces'));
|
$this->interfaces = array_merge($this->interfaces, $this->config->get('ide-helper.interfaces'), []);
|
||||||
// Make all interface classes absolute
|
// Make all interface classes absolute
|
||||||
foreach ($this->interfaces as &$interface) {
|
foreach ($this->interfaces as &$interface) {
|
||||||
$interface = '\\' . ltrim($interface, '\\');
|
$interface = '\\' . ltrim($interface, '\\');
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Barryvdh\LaravelIdeHelper;
|
|
||||||
|
|
||||||
class Helpers
|
|
||||||
{
|
|
||||||
public static function isLaravel(): bool
|
|
||||||
{
|
|
||||||
return class_exists('Illuminate\Foundation\Application');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -15,8 +15,10 @@ use Barryvdh\LaravelIdeHelper\Console\EloquentCommand;
|
|||||||
use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand;
|
use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand;
|
||||||
use Barryvdh\LaravelIdeHelper\Console\MetaCommand;
|
use Barryvdh\LaravelIdeHelper\Console\MetaCommand;
|
||||||
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
|
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
|
||||||
|
use Barryvdh\LaravelIdeHelper\Listeners\GenerateModelHelper;
|
||||||
|
use Illuminate\Console\Events\CommandFinished;
|
||||||
use Illuminate\Contracts\Support\DeferrableProvider;
|
use Illuminate\Contracts\Support\DeferrableProvider;
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Database\Events\MigrationsEnded;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Illuminate\View\Engines\EngineResolver;
|
use Illuminate\View\Engines\EngineResolver;
|
||||||
use Illuminate\View\Engines\PhpEngine;
|
use Illuminate\View\Engines\PhpEngine;
|
||||||
@@ -32,6 +34,13 @@ class IdeHelperServiceProvider extends ServiceProvider implements DeferrableProv
|
|||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
|
if (!$this->app->runningUnitTests() && $this->app['config']->get('ide-helper.post_migrate', [])) {
|
||||||
|
$this->app['events']->listen(CommandFinished::class, GenerateModelHelper::class);
|
||||||
|
$this->app['events']->listen(MigrationsEnded::class, function () {
|
||||||
|
GenerateModelHelper::$shouldRun = true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->app->has('view')) {
|
if ($this->app->has('view')) {
|
||||||
$viewPath = __DIR__ . '/../resources/views';
|
$viewPath = __DIR__ . '/../resources/views';
|
||||||
$this->loadViewsFrom($viewPath, 'ide-helper');
|
$this->loadViewsFrom($viewPath, 'ide-helper');
|
||||||
@@ -110,10 +119,6 @@ class IdeHelperServiceProvider extends ServiceProvider implements DeferrableProv
|
|||||||
{
|
{
|
||||||
$resolver = new EngineResolver();
|
$resolver = new EngineResolver();
|
||||||
$resolver->register('php', function () {
|
$resolver->register('php', function () {
|
||||||
if (Helpers::isLaravel() && (int) Application::VERSION < 8) {
|
|
||||||
return new PhpEngine();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new PhpEngine($this->app['files']);
|
return new PhpEngine($this->app['files']);
|
||||||
});
|
});
|
||||||
$finder = new FileViewFinder($this->app['files'], [__DIR__ . '/../resources/views']);
|
$finder = new FileViewFinder($this->app['files'], [__DIR__ . '/../resources/views']);
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Listeners;
|
||||||
|
|
||||||
|
use Illuminate\Console\Events\CommandFinished;
|
||||||
|
use Illuminate\Contracts\Config\Repository as Config;
|
||||||
|
use Illuminate\Contracts\Console\Kernel as Artisan;
|
||||||
|
|
||||||
|
class GenerateModelHelper
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tracks whether we should run the models command on the CommandFinished event or not.
|
||||||
|
* Set to true by the MigrationsEnded event, needs to be cleared before artisan call to prevent infinite loop.
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
public static $shouldRun = false;
|
||||||
|
|
||||||
|
/** @var \Illuminate\Contracts\Console\Kernel */
|
||||||
|
protected $artisan;
|
||||||
|
|
||||||
|
/** @var \Illuminate\Contracts\Config\Repository */
|
||||||
|
protected $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Illuminate\Contracts\Console\Kernel $artisan
|
||||||
|
* @param \Illuminate\Contracts\Config\Repository $config
|
||||||
|
*/
|
||||||
|
public function __construct(Artisan $artisan, Config $config)
|
||||||
|
{
|
||||||
|
$this->artisan = $artisan;
|
||||||
|
$this->config = $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the event.
|
||||||
|
*
|
||||||
|
* @param CommandFinished $event
|
||||||
|
*/
|
||||||
|
public function handle(CommandFinished $event)
|
||||||
|
{
|
||||||
|
if (!self::$shouldRun) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$shouldRun = false;
|
||||||
|
|
||||||
|
foreach ($this->config->get('ide-helper.post_migrate', []) as $command) {
|
||||||
|
$this->artisan->call($command, [], $event->output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+28
-15
@@ -4,6 +4,7 @@ namespace Barryvdh\LaravelIdeHelper;
|
|||||||
|
|
||||||
use Barryvdh\Reflection\DocBlock;
|
use Barryvdh\Reflection\DocBlock;
|
||||||
use Barryvdh\Reflection\DocBlock\Tag;
|
use Barryvdh\Reflection\DocBlock\Tag;
|
||||||
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
class Macro extends Method
|
class Macro extends Method
|
||||||
@@ -16,15 +17,17 @@ class Macro extends Method
|
|||||||
* @param \ReflectionClass $class
|
* @param \ReflectionClass $class
|
||||||
* @param null $methodName
|
* @param null $methodName
|
||||||
* @param array $interfaces
|
* @param array $interfaces
|
||||||
|
* @param array $classAliases
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
$method,
|
$method,
|
||||||
$alias,
|
$alias,
|
||||||
$class,
|
$class,
|
||||||
$methodName = null,
|
$methodName = null,
|
||||||
$interfaces = []
|
$interfaces = [],
|
||||||
|
$classAliases = []
|
||||||
) {
|
) {
|
||||||
parent::__construct($method, $alias, $class, $methodName, $interfaces);
|
parent::__construct($method, $alias, $class, $methodName, $interfaces, $classAliases);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,25 +35,31 @@ class Macro extends Method
|
|||||||
*/
|
*/
|
||||||
protected function initPhpDoc($method)
|
protected function initPhpDoc($method)
|
||||||
{
|
{
|
||||||
$this->phpdoc = new DocBlock('/** */');
|
$this->phpdoc = new DocBlock($method);
|
||||||
|
|
||||||
$this->addLocationToPhpDoc();
|
$this->addLocationToPhpDoc();
|
||||||
|
|
||||||
// Add macro parameters
|
// Add macro parameters if they are missed in original docblock
|
||||||
foreach ($method->getParameters() as $parameter) {
|
if (!$this->phpdoc->hasTag('param')) {
|
||||||
$type = $parameter->hasType() ? $parameter->getType()->getName() : 'mixed';
|
foreach ($method->getParameters() as $parameter) {
|
||||||
$type .= $parameter->hasType() && $parameter->getType()->allowsNull() ? '|null' : '';
|
$type = $parameter->hasType() ? $parameter->getType()->getName() : 'mixed';
|
||||||
|
$type .= $parameter->hasType() && $parameter->getType()->allowsNull() ? '|null' : '';
|
||||||
|
|
||||||
$name = $parameter->isVariadic() ? '...' : '';
|
$name = $parameter->isVariadic() ? '...' : '';
|
||||||
$name .= '$' . $parameter->getName();
|
$name .= '$' . $parameter->getName();
|
||||||
|
|
||||||
$this->phpdoc->appendTag(Tag::createInstance("@param {$type} {$name}"));
|
$this->phpdoc->appendTag(Tag::createInstance("@param {$type} {$name}"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add macro return type
|
// Add macro return type if it missed in original docblock
|
||||||
if ($method->hasReturnType()) {
|
if ($method->hasReturnType() && !$this->phpdoc->hasTag('return')) {
|
||||||
$type = $method->getReturnType()->getName();
|
$builder = EloquentBuilder::class;
|
||||||
$type .= $method->getReturnType()->allowsNull() ? '|null' : '';
|
$return = $method->getReturnType();
|
||||||
|
|
||||||
|
$type = $return->getName();
|
||||||
|
$type .= $this->root === "\\{$builder}" && $return->getName() === $builder ? '|static' : '';
|
||||||
|
$type .= $return->allowsNull() ? '|null' : '';
|
||||||
|
|
||||||
$this->phpdoc->appendTag(Tag::createInstance("@return {$type}"));
|
$this->phpdoc->appendTag(Tag::createInstance("@return {$type}"));
|
||||||
}
|
}
|
||||||
@@ -58,7 +67,11 @@ class Macro extends Method
|
|||||||
|
|
||||||
protected function addLocationToPhpDoc()
|
protected function addLocationToPhpDoc()
|
||||||
{
|
{
|
||||||
$enclosingClass = $this->method->getClosureScopeClass();
|
if ($this->method->name === '__invoke') {
|
||||||
|
$enclosingClass = $this->method->getDeclaringClass();
|
||||||
|
} else {
|
||||||
|
$enclosingClass = $this->method->getClosureScopeClass();
|
||||||
|
}
|
||||||
|
|
||||||
/** @var \ReflectionMethod $enclosingMethod */
|
/** @var \ReflectionMethod $enclosingMethod */
|
||||||
$enclosingMethod = Collection::make($enclosingClass->getMethods())
|
$enclosingMethod = Collection::make($enclosingClass->getMethods())
|
||||||
|
|||||||
+7
-4
@@ -38,6 +38,7 @@ class Method
|
|||||||
protected $real_name;
|
protected $real_name;
|
||||||
protected $return = null;
|
protected $return = null;
|
||||||
protected $root;
|
protected $root;
|
||||||
|
protected $classAliases;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \ReflectionMethod|\ReflectionFunctionAbstract $method
|
* @param \ReflectionMethod|\ReflectionFunctionAbstract $method
|
||||||
@@ -45,17 +46,19 @@ class Method
|
|||||||
* @param \ReflectionClass $class
|
* @param \ReflectionClass $class
|
||||||
* @param string|null $methodName
|
* @param string|null $methodName
|
||||||
* @param array $interfaces
|
* @param array $interfaces
|
||||||
|
* @param array $classAliases
|
||||||
*/
|
*/
|
||||||
public function __construct($method, $alias, $class, $methodName = null, $interfaces = [])
|
public function __construct($method, $alias, $class, $methodName = null, $interfaces = [], array $classAliases = [])
|
||||||
{
|
{
|
||||||
$this->method = $method;
|
$this->method = $method;
|
||||||
$this->interfaces = $interfaces;
|
$this->interfaces = $interfaces;
|
||||||
|
$this->classAliases = $classAliases;
|
||||||
$this->name = $methodName ?: $method->name;
|
$this->name = $methodName ?: $method->name;
|
||||||
$this->real_name = $method->isClosure() ? $this->name : $method->name;
|
$this->real_name = $method->isClosure() ? $this->name : $method->name;
|
||||||
$this->initClassDefinedProperties($method, $class);
|
$this->initClassDefinedProperties($method, $class);
|
||||||
|
|
||||||
//Reference the 'real' function in the declaring class
|
//Reference the 'real' function in the declaring class
|
||||||
$this->root = '\\' . ltrim($class->getName(), '\\');
|
$this->root = '\\' . ltrim($method->name === '__invoke' ? $method->getDeclaringClass()->getName() : $class->getName(), '\\');
|
||||||
|
|
||||||
//Create a DocBlock and serializer instance
|
//Create a DocBlock and serializer instance
|
||||||
$this->initPhpDoc($method);
|
$this->initPhpDoc($method);
|
||||||
@@ -80,7 +83,7 @@ class Method
|
|||||||
*/
|
*/
|
||||||
protected function initPhpDoc($method)
|
protected function initPhpDoc($method)
|
||||||
{
|
{
|
||||||
$this->phpdoc = new DocBlock($method, new Context($this->namespace));
|
$this->phpdoc = new DocBlock($method, new Context($this->namespace, $this->classAliases));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -363,7 +366,7 @@ class Method
|
|||||||
}
|
}
|
||||||
if ($method) {
|
if ($method) {
|
||||||
$namespace = $method->getDeclaringClass()->getNamespaceName();
|
$namespace = $method->getDeclaringClass()->getNamespaceName();
|
||||||
$phpdoc = new DocBlock($method, new Context($namespace));
|
$phpdoc = new DocBlock($method, new Context($namespace, $this->classAliases));
|
||||||
|
|
||||||
if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) {
|
if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) {
|
||||||
//Not at the end yet, try another parent/interface..
|
//Not at the end yet, try another parent/interface..
|
||||||
|
|||||||
@@ -0,0 +1,124 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Laravel IDE Helper Generator
|
||||||
|
*
|
||||||
|
* @author Barry vd. Heuvel <barryvdh@gmail.com>
|
||||||
|
* @copyright 2014 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl)
|
||||||
|
* @license http://www.opensource.org/licenses/mit-license.php MIT
|
||||||
|
* @link https://github.com/barryvdh/laravel-ide-helper
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper;
|
||||||
|
|
||||||
|
use PhpParser\Node\Stmt\GroupUse;
|
||||||
|
use PhpParser\Node\Stmt\Namespace_;
|
||||||
|
use PhpParser\Node\Stmt\Use_;
|
||||||
|
use PhpParser\Node\Stmt\UseUse;
|
||||||
|
use PhpParser\ParserFactory;
|
||||||
|
|
||||||
|
class UsesResolver
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string $classFQN
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function loadFromClass(string $classFQN): array
|
||||||
|
{
|
||||||
|
return $this->loadFromFile(
|
||||||
|
$classFQN,
|
||||||
|
(new \ReflectionClass($classFQN))->getFileName()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $classFQN
|
||||||
|
* @param string $filename
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function loadFromFile(string $classFQN, string $filename): array
|
||||||
|
{
|
||||||
|
return $this->loadFromCode(
|
||||||
|
$classFQN,
|
||||||
|
file_get_contents(
|
||||||
|
$filename
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $classFQN
|
||||||
|
* @param string $code
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function loadFromCode(string $classFQN, string $code): array
|
||||||
|
{
|
||||||
|
$classFQN = ltrim($classFQN, '\\');
|
||||||
|
|
||||||
|
$namespace = rtrim(
|
||||||
|
preg_replace(
|
||||||
|
'/([^\\\\]+)$/',
|
||||||
|
'',
|
||||||
|
$classFQN
|
||||||
|
),
|
||||||
|
'\\'
|
||||||
|
);
|
||||||
|
|
||||||
|
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
|
||||||
|
$namespaceData = null;
|
||||||
|
|
||||||
|
foreach ($parser->parse($code) as $node) {
|
||||||
|
if ($node instanceof Namespace_ && $node->name->toCodeString() === $namespace) {
|
||||||
|
$namespaceData = $node;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($namespaceData === null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var Namespace_ $namespaceData */
|
||||||
|
|
||||||
|
$aliases = [];
|
||||||
|
|
||||||
|
foreach ($namespaceData->stmts as $stmt) {
|
||||||
|
if ($stmt instanceof Use_) {
|
||||||
|
if ($stmt->type !== Use_::TYPE_NORMAL) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($stmt->uses as $use) {
|
||||||
|
/** @var UseUse $use */
|
||||||
|
|
||||||
|
$alias = $use->alias ?
|
||||||
|
$use->alias->name :
|
||||||
|
self::classBasename($use->name->toCodeString());
|
||||||
|
|
||||||
|
$aliases[$alias] = '\\' . $use->name->toCodeString();
|
||||||
|
}
|
||||||
|
} elseif ($stmt instanceof GroupUse) {
|
||||||
|
foreach ($stmt->uses as $use) {
|
||||||
|
/** @var UseUse $use */
|
||||||
|
|
||||||
|
$alias = $use->alias ?
|
||||||
|
$use->alias->name :
|
||||||
|
self::classBasename($use->name->toCodeString());
|
||||||
|
|
||||||
|
$aliases[$alias] = '\\' . $stmt->prefix->toCodeString() . '\\' . $use->name->toCodeString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $aliases;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $classFQN
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function classBasename(string $classFQN): string
|
||||||
|
{
|
||||||
|
return preg_replace('/^.*\\\\([^\\\\]+)$/', '$1', $classFQN);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Alias;
|
||||||
|
use Barryvdh\LaravelIdeHelper\Macro;
|
||||||
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||||
|
use Illuminate\Database\Query\Builder;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @coversDefaultClass \Barryvdh\LaravelIdeHelper\Alias
|
||||||
|
*/
|
||||||
|
class AliasTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers ::detectMethods
|
||||||
|
*/
|
||||||
|
public function testDetectMethodsMacroableMacros(): void
|
||||||
|
{
|
||||||
|
// Mock
|
||||||
|
$macro = __FUNCTION__;
|
||||||
|
$alias = new AliasMock();
|
||||||
|
|
||||||
|
// Macros
|
||||||
|
Builder::macro(
|
||||||
|
$macro,
|
||||||
|
function () {
|
||||||
|
// empty
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Prepare
|
||||||
|
$alias->setClasses([Builder::class]);
|
||||||
|
$alias->detectMethods();
|
||||||
|
|
||||||
|
// Test
|
||||||
|
$this->assertNotNull($this->getAliasMacro($alias, Builder::class, $macro));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::detectMethods
|
||||||
|
*/
|
||||||
|
public function testDetectMethodsEloquentBuilderMacros(): void
|
||||||
|
{
|
||||||
|
// Mock
|
||||||
|
$macro = __FUNCTION__;
|
||||||
|
$alias = new AliasMock();
|
||||||
|
|
||||||
|
// Macros
|
||||||
|
EloquentBuilder::macro(
|
||||||
|
$macro,
|
||||||
|
function () {
|
||||||
|
// empty
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Prepare
|
||||||
|
$alias->setClasses([EloquentBuilder::class]);
|
||||||
|
$alias->detectMethods();
|
||||||
|
|
||||||
|
// Test
|
||||||
|
$this->assertNotNull($this->getAliasMacro($alias, EloquentBuilder::class, $macro));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getAliasMacro(Alias $alias, string $class, string $method): ?Macro
|
||||||
|
{
|
||||||
|
return Arr::first(
|
||||||
|
$alias->getMethods(),
|
||||||
|
function ($macro) use ($class, $method) {
|
||||||
|
return $macro instanceof Macro
|
||||||
|
&& $macro->getDeclaringClass() === "\\{$class}"
|
||||||
|
&& $macro->getName() === $method;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @noinspection PhpMultipleClassesDeclarationsInOneFile
|
||||||
|
*/
|
||||||
|
class AliasMock extends Alias
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
// no need to call parent
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string[] $classes
|
||||||
|
*/
|
||||||
|
public function setClasses(array $classes)
|
||||||
|
{
|
||||||
|
$this->classes = $classes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function detectMethods()
|
||||||
|
{
|
||||||
|
parent::detectMethods();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Comment\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||||
|
|
||||||
|
class Simple extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* There is not comment.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getNotCommentAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* comment There is not format comment, invalid.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getFakerCommentAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment There is format comment, success.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getFormatCommentAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment There is format comment, success.
|
||||||
|
* This is second line, success too.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getFormatCommentLineTwoAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment There is format comment, success.
|
||||||
|
* @comment This is others format comment, invalid.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getManyFormatCommentAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment Set the user's first name.
|
||||||
|
* @param $value
|
||||||
|
*/
|
||||||
|
public function setFirstNameAttribute($value)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment Scope a query to only include active users.
|
||||||
|
*
|
||||||
|
* @param $query
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function scopeActive($query)
|
||||||
|
{
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment HasMany relations.
|
||||||
|
*
|
||||||
|
* @return HasMany
|
||||||
|
*/
|
||||||
|
public function relationHasMany(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(Simple::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment MorphTo relations.
|
||||||
|
* @return MorphTo
|
||||||
|
*/
|
||||||
|
public function relationMorphTo(): MorphTo
|
||||||
|
{
|
||||||
|
return $this->morphTo();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment Others relations.
|
||||||
|
* @return HasOne
|
||||||
|
*/
|
||||||
|
public function relationHasOne(): HasOne
|
||||||
|
{
|
||||||
|
return $this->hasOne(Simple::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment I'm a setter
|
||||||
|
*/
|
||||||
|
public function setBothSameNameAttribute(): void
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment I'm a getter
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getBothSameNameAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment I'm a setter
|
||||||
|
*/
|
||||||
|
public function setBothWithoutGetterCommentAttribute(): void
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getBothWithoutGetterCommentAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Comment;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
|
||||||
|
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
|
||||||
|
|
||||||
|
class Test extends AbstractModelsCommand
|
||||||
|
{
|
||||||
|
public function test(): void
|
||||||
|
{
|
||||||
|
$command = $this->app->make(ModelsCommand::class);
|
||||||
|
|
||||||
|
$tester = $this->runCommand($command, [
|
||||||
|
'--write' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertSame(0, $tester->getStatusCode());
|
||||||
|
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
|
||||||
|
$this->assertMatchesMockedSnapshot();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Comment\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Comment\Models\Simple
|
||||||
|
*
|
||||||
|
* @property integer $id
|
||||||
|
* @property string $both_same_name I'm a getter
|
||||||
|
* @property string $both_without_getter_comment
|
||||||
|
* @property-read string $faker_comment
|
||||||
|
* @property-read string $format_comment There is format comment, success.
|
||||||
|
* @property-read string $format_comment_line_two There is format comment, success.
|
||||||
|
* This is second line, success too.
|
||||||
|
* @property-read string $many_format_comment There is format comment, success.
|
||||||
|
* @property-read string $not_comment
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|Simple[] $relationHasMany HasMany relations.
|
||||||
|
* @property-read int|null $relation_has_many_count
|
||||||
|
* @property-read Simple|null $relationHasOne Others relations.
|
||||||
|
* @property-read Model|\Eloquent $relationMorphTo MorphTo relations.
|
||||||
|
* @property-write mixed $first_name Set the user's first name.
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|Simple active() Scope a query to only include active users.
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|Simple query()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value)
|
||||||
|
* @mixin \Eloquent
|
||||||
|
*/
|
||||||
|
class Simple extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* There is not comment.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getNotCommentAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* comment There is not format comment, invalid.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getFakerCommentAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment There is format comment, success.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getFormatCommentAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment There is format comment, success.
|
||||||
|
* This is second line, success too.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getFormatCommentLineTwoAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment There is format comment, success.
|
||||||
|
* @comment This is others format comment, invalid.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getManyFormatCommentAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment Set the user's first name.
|
||||||
|
* @param $value
|
||||||
|
*/
|
||||||
|
public function setFirstNameAttribute($value)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment Scope a query to only include active users.
|
||||||
|
*
|
||||||
|
* @param $query
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function scopeActive($query)
|
||||||
|
{
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment HasMany relations.
|
||||||
|
*
|
||||||
|
* @return HasMany
|
||||||
|
*/
|
||||||
|
public function relationHasMany(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(Simple::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment MorphTo relations.
|
||||||
|
* @return MorphTo
|
||||||
|
*/
|
||||||
|
public function relationMorphTo(): MorphTo
|
||||||
|
{
|
||||||
|
return $this->morphTo();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment Others relations.
|
||||||
|
* @return HasOne
|
||||||
|
*/
|
||||||
|
public function relationHasOne(): HasOne
|
||||||
|
{
|
||||||
|
return $this->hasOne(Simple::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment I'm a setter
|
||||||
|
*/
|
||||||
|
public function setBothSameNameAttribute(): void
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment I'm a getter
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getBothSameNameAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @comment I'm a setter
|
||||||
|
*/
|
||||||
|
public function setBothWithoutGetterCommentAttribute(): void
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getBothWithoutGetterCommentAttribute(): string
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\CustomSpace;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithCustomNamespace;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
class ModelWithCustomNamespaceFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name of the factory's corresponding model.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $model = ModelWithCustomNamespace::class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function definition()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Factories;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
class ModelWithFactoryFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name of the factory's corresponding model.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $model = ModelWithFactory::class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function definition()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\CustomSpace\ModelWithCustomNamespaceFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class ModelWithCustomNamespace extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new factory instance for the model.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Factories\Factory
|
||||||
|
*/
|
||||||
|
protected static function newFactory()
|
||||||
|
{
|
||||||
|
return ModelWithCustomNamespaceFactory::new();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class ModelWithFactory extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class ModelWithoutFactory extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
|
||||||
|
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Illuminate\Foundation\Application;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
class Test extends AbstractModelsCommand
|
||||||
|
{
|
||||||
|
public function test(): void
|
||||||
|
{
|
||||||
|
if (!version_compare(Application::VERSION, '8.2', '>=')) {
|
||||||
|
$this->markTestSkipped(
|
||||||
|
'This test only works in Laravel >= 8.2'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Factory::guessFactoryNamesUsing(static::getFactoryNameResolver());
|
||||||
|
|
||||||
|
$command = $this->app->make(ModelsCommand::class);
|
||||||
|
|
||||||
|
$tester = $this->runCommand($command, [
|
||||||
|
'--write' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertSame(0, $tester->getStatusCode());
|
||||||
|
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
|
||||||
|
$this->assertStringNotContainsString('not found', $tester->getDisplay());
|
||||||
|
$this->assertMatchesMockedSnapshot();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getFactoryNameResolver(): Closure
|
||||||
|
{
|
||||||
|
// This mimics the default resolver, but with adjusted test namespaces.
|
||||||
|
// Illuminate\Database\Eloquent\Factories\Factory::resolveFactoryName
|
||||||
|
return function (string $modelName): string {
|
||||||
|
$appNamespace = 'Barryvdh\\LaravelIdeHelper\\Tests\\Console\\ModelsCommand\\Factories\\';
|
||||||
|
|
||||||
|
$modelName = Str::startsWith($modelName, $appNamespace . 'Models\\')
|
||||||
|
? Str::after($modelName, $appNamespace . 'Models\\')
|
||||||
|
: Str::after($modelName, $appNamespace);
|
||||||
|
|
||||||
|
return $appNamespace . 'Factories\\' . $modelName . 'Factory';
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\CustomSpace\ModelWithCustomNamespaceFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithCustomNamespace
|
||||||
|
*
|
||||||
|
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\CustomSpace\ModelWithCustomNamespaceFactory factory(...$parameters)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithCustomNamespace newModelQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithCustomNamespace newQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithCustomNamespace query()
|
||||||
|
* @mixin \Eloquent
|
||||||
|
*/
|
||||||
|
class ModelWithCustomNamespace extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new factory instance for the model.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Factories\Factory
|
||||||
|
*/
|
||||||
|
protected static function newFactory()
|
||||||
|
{
|
||||||
|
return ModelWithCustomNamespaceFactory::new();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithFactory
|
||||||
|
*
|
||||||
|
* @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Factories\ModelWithFactoryFactory factory(...$parameters)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithFactory newModelQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithFactory newQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithFactory query()
|
||||||
|
* @mixin \Eloquent
|
||||||
|
*/
|
||||||
|
class ModelWithFactory extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
}
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithoutFactory
|
||||||
|
*
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithoutFactory newModelQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithoutFactory newQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithoutFactory query()
|
||||||
|
* @mixin \Eloquent
|
||||||
|
*/
|
||||||
|
class ModelWithoutFactory extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
}
|
||||||
@@ -6,19 +6,9 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCas
|
|||||||
|
|
||||||
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
|
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
|
||||||
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
|
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
|
||||||
use Illuminate\Foundation\Application;
|
|
||||||
|
|
||||||
class Test extends AbstractModelsCommand
|
class Test extends AbstractModelsCommand
|
||||||
{
|
{
|
||||||
protected function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
|
|
||||||
if (version_compare(Application::VERSION, '7.0', '<')) {
|
|
||||||
$this->markTestSkipped('This test requires Laravel 7.0 or higher');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test(): void
|
public function test(): void
|
||||||
{
|
{
|
||||||
$command = $this->app->make(ModelsCommand::class);
|
$command = $this->app->make(ModelsCommand::class);
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
|
||||||
|
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class CustomProperty implements ModelHookInterface
|
||||||
|
{
|
||||||
|
public function run(ModelsCommand $command, Model $model): void
|
||||||
|
{
|
||||||
|
$command->setProperty('custom', 'string', true, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Simple extends Model
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
|
||||||
|
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
|
||||||
|
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks\CustomProperty;
|
||||||
|
use Illuminate\Filesystem\Filesystem;
|
||||||
|
use Mockery;
|
||||||
|
|
||||||
|
class Test extends AbstractModelsCommand
|
||||||
|
{
|
||||||
|
protected function getEnvironmentSetUp($app)
|
||||||
|
{
|
||||||
|
parent::getEnvironmentSetUp($app);
|
||||||
|
|
||||||
|
$app['config']->set('ide-helper', [
|
||||||
|
'model_locations' => [
|
||||||
|
// This is calculated from the base_path() which points to
|
||||||
|
// vendor/orchestra/testbench-core/laravel
|
||||||
|
'/../../../../tests/Console/ModelsCommand/ModelHooks/Models',
|
||||||
|
],
|
||||||
|
'model_hooks' => [
|
||||||
|
CustomProperty::class,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test(): void
|
||||||
|
{
|
||||||
|
$actualContent = null;
|
||||||
|
|
||||||
|
$mockFilesystem = Mockery::mock(Filesystem::class);
|
||||||
|
$mockFilesystem
|
||||||
|
->shouldReceive('get')
|
||||||
|
->andReturn(file_get_contents(__DIR__ . '/Models/Simple.php'))
|
||||||
|
->once();
|
||||||
|
$mockFilesystem
|
||||||
|
->shouldReceive('put')
|
||||||
|
->with(
|
||||||
|
Mockery::any(),
|
||||||
|
Mockery::capture($actualContent)
|
||||||
|
)
|
||||||
|
->andReturn(1) // Simulate we wrote _something_ to the file
|
||||||
|
->once();
|
||||||
|
|
||||||
|
$this->instance(Filesystem::class, $mockFilesystem);
|
||||||
|
|
||||||
|
$command = $this->app->make(ModelsCommand::class);
|
||||||
|
|
||||||
|
$tester = $this->runCommand($command, [
|
||||||
|
'--write' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertSame(0, $tester->getStatusCode());
|
||||||
|
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
|
||||||
|
|
||||||
|
$expectedContent = <<<'PHP'
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Models\Simple
|
||||||
|
*
|
||||||
|
* @property int $id
|
||||||
|
* @property-read string $custom
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|Simple query()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value)
|
||||||
|
* @mixin \Eloquent
|
||||||
|
*/
|
||||||
|
class Simple extends Model
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
PHP;
|
||||||
|
|
||||||
|
$this->assertSame($expectedContent, $actualContent);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\UnionTypes\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Database\Query\Builder;
|
||||||
|
|
||||||
|
class UnionTypeModel extends Model
|
||||||
|
{
|
||||||
|
public function scopeWithUnionTypeParameter(Builder $query, string|int $bar): Builder
|
||||||
|
{
|
||||||
|
return $query->where('foo', $bar);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function scopeWithNullableUnionTypeParameter(Builder $query, null|string|int $bar): Builder
|
||||||
|
{
|
||||||
|
return $query->where('foo', $bar);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function withUnionTypeReturn(): HasMany|UnionTypeModel
|
||||||
|
{
|
||||||
|
return $this->hasMany(UnionTypeModel::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFooAttribute(): string|int|null
|
||||||
|
{
|
||||||
|
return $this->getAttribute('foo');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\UnionTypes;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
|
||||||
|
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
|
||||||
|
|
||||||
|
class Test extends AbstractModelsCommand
|
||||||
|
{
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
if (PHP_VERSION_ID < 80000) {
|
||||||
|
$this->markTestSkipped('This test requires PHP 8.0 or higher');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test(): void
|
||||||
|
{
|
||||||
|
$command = $this->app->make(ModelsCommand::class);
|
||||||
|
|
||||||
|
$tester = $this->runCommand($command, [
|
||||||
|
'--write' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertSame(0, $tester->getStatusCode());
|
||||||
|
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
|
||||||
|
$this->assertMatchesMockedSnapshot();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\UnionTypes\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Database\Query\Builder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\UnionTypes\Models\UnionTypeModel
|
||||||
|
*
|
||||||
|
* @property-read string|int|null $foo
|
||||||
|
* @property-read \Illuminate\Database\Eloquent\Collection|UnionTypeModel[] $withUnionTypeReturn
|
||||||
|
* @property-read int|null $with_union_type_return_count
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|UnionTypeModel newModelQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|UnionTypeModel newQuery()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|UnionTypeModel query()
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|UnionTypeModel withNullableUnionTypeParameter(string|int|null $bar)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|UnionTypeModel withUnionTypeParameter(string|int $bar)
|
||||||
|
* @mixin \Eloquent
|
||||||
|
*/
|
||||||
|
class UnionTypeModel extends Model
|
||||||
|
{
|
||||||
|
public function scopeWithUnionTypeParameter(Builder $query, string|int $bar): Builder
|
||||||
|
{
|
||||||
|
return $query->where('foo', $bar);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function scopeWithNullableUnionTypeParameter(Builder $query, null|string|int $bar): Builder
|
||||||
|
{
|
||||||
|
return $query->where('foo', $bar);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function withUnionTypeReturn(): HasMany|UnionTypeModel
|
||||||
|
{
|
||||||
|
return $this->hasMany(UnionTypeModel::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFooAttribute(): string|int|null
|
||||||
|
{
|
||||||
|
return $this->getAttribute('foo');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,278 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\Macro;
|
||||||
|
use Barryvdh\Reflection\DocBlock;
|
||||||
|
use Barryvdh\Reflection\DocBlock\Tag;
|
||||||
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||||
|
use Illuminate\Routing\UrlGenerator;
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionFunction;
|
||||||
|
use ReflectionFunctionAbstract;
|
||||||
|
|
||||||
|
use function array_map;
|
||||||
|
use function implode;
|
||||||
|
|
||||||
|
use const PHP_EOL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @coversDefaultClass \Barryvdh\LaravelIdeHelper\Macro
|
||||||
|
*/
|
||||||
|
class MacroTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers ::initPhpDoc
|
||||||
|
* @throws \ReflectionException
|
||||||
|
*/
|
||||||
|
public function testInitPhpDocEloquentBuilderHasStaticInReturnType(): void
|
||||||
|
{
|
||||||
|
$class = new ReflectionClass(EloquentBuilder::class);
|
||||||
|
$phpdoc = (new MacroMock())->getPhpDoc(
|
||||||
|
new ReflectionFunction(
|
||||||
|
function (): EloquentBuilder {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
),
|
||||||
|
$class
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertNotNull($phpdoc);
|
||||||
|
$this->assertEquals(
|
||||||
|
'@return \Illuminate\Database\Eloquent\Builder|static',
|
||||||
|
$this->tagsToString($phpdoc, 'return')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::initPhpDoc
|
||||||
|
* @throws \ReflectionException
|
||||||
|
*/
|
||||||
|
public function testInitPhpDocClosureWithoutDocBlock(): void
|
||||||
|
{
|
||||||
|
$phpdoc = (new MacroMock())->getPhpDoc(
|
||||||
|
new ReflectionFunction(
|
||||||
|
function (int $a = null): int {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertNotNull($phpdoc);
|
||||||
|
$this->assertEmpty($phpdoc->getText());
|
||||||
|
$this->assertEquals('@param int|null $a', $this->tagsToString($phpdoc, 'param'));
|
||||||
|
$this->assertEquals('@return int', $this->tagsToString($phpdoc, 'return'));
|
||||||
|
$this->assertTrue($phpdoc->hasTag('see'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::initPhpDoc
|
||||||
|
* @throws \ReflectionException
|
||||||
|
*/
|
||||||
|
public function testInitPhpDocClosureWithArgsAndReturnType(): void
|
||||||
|
{
|
||||||
|
$phpdoc = (new MacroMock())->getPhpDoc(
|
||||||
|
new ReflectionFunction(
|
||||||
|
/**
|
||||||
|
* Test docblock.
|
||||||
|
*/
|
||||||
|
function (int $a = null): int {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertNotNull($phpdoc);
|
||||||
|
$this->assertStringContainsString('Test docblock', $phpdoc->getText());
|
||||||
|
$this->assertEquals('@param int|null $a', $this->tagsToString($phpdoc, 'param'));
|
||||||
|
$this->assertEquals('@return int', $this->tagsToString($phpdoc, 'return'));
|
||||||
|
$this->assertTrue($phpdoc->hasTag('see'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::initPhpDoc
|
||||||
|
* @throws \ReflectionException
|
||||||
|
*/
|
||||||
|
public function testInitPhpDocClosureWithArgs(): void
|
||||||
|
{
|
||||||
|
$phpdoc = (new MacroMock())->getPhpDoc(
|
||||||
|
new ReflectionFunction(
|
||||||
|
/**
|
||||||
|
* Test docblock.
|
||||||
|
*/
|
||||||
|
function (int $a = null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertNotNull($phpdoc);
|
||||||
|
$this->assertStringContainsString('Test docblock', $phpdoc->getText());
|
||||||
|
$this->assertEquals('@param int|null $a', $this->tagsToString($phpdoc, 'param'));
|
||||||
|
$this->assertFalse($phpdoc->hasTag('return'));
|
||||||
|
$this->assertTrue($phpdoc->hasTag('see'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::initPhpDoc
|
||||||
|
* @throws \ReflectionException
|
||||||
|
*/
|
||||||
|
public function testInitPhpDocClosureWithReturnType(): void
|
||||||
|
{
|
||||||
|
$phpdoc = (new MacroMock())->getPhpDoc(
|
||||||
|
new ReflectionFunction(
|
||||||
|
/**
|
||||||
|
* Test docblock.
|
||||||
|
*/
|
||||||
|
function (): int {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertNotNull($phpdoc);
|
||||||
|
$this->assertStringContainsString('Test docblock', $phpdoc->getText());
|
||||||
|
$this->assertFalse($phpdoc->hasTag('param'));
|
||||||
|
$this->assertEquals('@return int', $this->tagsToString($phpdoc, 'return'));
|
||||||
|
$this->assertTrue($phpdoc->hasTag('see'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::initPhpDoc
|
||||||
|
*/
|
||||||
|
public function testInitPhpDocParamsAddedOnlyNotPresent(): void
|
||||||
|
{
|
||||||
|
$phpdoc = (new MacroMock())->getPhpDoc(
|
||||||
|
new ReflectionFunction(
|
||||||
|
/**
|
||||||
|
* Test docblock.
|
||||||
|
*
|
||||||
|
* @param \stdClass|null $a aaaaa
|
||||||
|
*/
|
||||||
|
function ($a = null): int {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertNotNull($phpdoc);
|
||||||
|
$this->assertStringContainsString('Test docblock', $phpdoc->getText());
|
||||||
|
$this->assertEquals('@param \stdClass|null $a aaaaa', $this->tagsToString($phpdoc, 'param'));
|
||||||
|
$this->assertEquals('@return int', $this->tagsToString($phpdoc, 'return'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::initPhpDoc
|
||||||
|
*/
|
||||||
|
public function testInitPhpDocReturnAddedOnlyNotPresent(): void
|
||||||
|
{
|
||||||
|
$phpdoc = (new MacroMock())->getPhpDoc(
|
||||||
|
new ReflectionFunction(
|
||||||
|
/**
|
||||||
|
* Test docblock.
|
||||||
|
*
|
||||||
|
* @return \stdClass|null rrrrrrr
|
||||||
|
*/
|
||||||
|
function ($a = null): int {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertNotNull($phpdoc);
|
||||||
|
$this->assertStringContainsString('Test docblock', $phpdoc->getText());
|
||||||
|
$this->assertEquals('@param mixed $a', $this->tagsToString($phpdoc, 'param'));
|
||||||
|
$this->assertEquals('@return \stdClass|null rrrrrrr', $this->tagsToString($phpdoc, 'return'));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tagsToString(DocBlock $docBlock, string $name)
|
||||||
|
{
|
||||||
|
$tags = $docBlock->getTagsByName($name);
|
||||||
|
$tags = array_map(
|
||||||
|
function (Tag $tag) {
|
||||||
|
return trim((string)$tag);
|
||||||
|
},
|
||||||
|
$tags
|
||||||
|
);
|
||||||
|
$tags = implode(PHP_EOL, $tags);
|
||||||
|
|
||||||
|
return $tags;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Test that we can actually instantiate the class
|
||||||
|
*/
|
||||||
|
public function testCanInstantiate()
|
||||||
|
{
|
||||||
|
$reflectionMethod = new \ReflectionMethod(UrlGeneratorMacroClass::class, '__invoke');
|
||||||
|
|
||||||
|
$macro = new Macro($reflectionMethod, UrlGenerator::class, new \ReflectionClass(UrlGenerator::class), 'macroName');
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Macro::class, $macro);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the output of a class
|
||||||
|
*/
|
||||||
|
public function testOutput()
|
||||||
|
{
|
||||||
|
$reflectionMethod = new \ReflectionMethod(UrlGeneratorMacroClass::class, '__invoke');
|
||||||
|
|
||||||
|
$macro = new Macro($reflectionMethod, 'URL', new \ReflectionClass(UrlGenerator::class), 'macroName');
|
||||||
|
$output = <<<'DOC'
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param string $foo
|
||||||
|
* @param int $bar
|
||||||
|
* @return string
|
||||||
|
* @see \Barryvdh\LaravelIdeHelper\Tests\UrlGeneratorMacroClass::__invoke()
|
||||||
|
* @static
|
||||||
|
*/
|
||||||
|
DOC;
|
||||||
|
$this->assertSame($output, $macro->getDocComment(''));
|
||||||
|
$this->assertSame('__invoke', $macro->getRealName());
|
||||||
|
$this->assertSame('\\' . UrlGenerator::class, $macro->getDeclaringClass());
|
||||||
|
$this->assertSame('$foo, $bar', $macro->getParams(true));
|
||||||
|
$this->assertSame(['$foo', '$bar'], $macro->getParams(false));
|
||||||
|
$this->assertSame('$foo, $bar = 0', $macro->getParamsWithDefault(true));
|
||||||
|
$this->assertSame(['$foo', '$bar = 0'], $macro->getParamsWithDefault(false));
|
||||||
|
$this->assertTrue($macro->shouldReturn());
|
||||||
|
$this->assertSame('$instance->__invoke($foo, $bar)', $macro->getRootMethodCall());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @noinspection PhpMultipleClassesDeclarationsInOneFile
|
||||||
|
*/
|
||||||
|
class MacroMock extends Macro
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
// no need to call parent
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPhpDoc(ReflectionFunctionAbstract $method, ReflectionClass $class = null): DocBlock
|
||||||
|
{
|
||||||
|
return (new Macro($method, '', $class ?? $method->getClosureScopeClass()))->phpdoc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Example of an invokable class to be used as a macro.
|
||||||
|
*/
|
||||||
|
class UrlGeneratorMacroClass
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param string $foo
|
||||||
|
* @param int $bar
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __invoke(string $foo, int $bar = 0): string
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
+39
-5
@@ -6,7 +6,6 @@ namespace Barryvdh\LaravelIdeHelper\Tests;
|
|||||||
|
|
||||||
use Barryvdh\LaravelIdeHelper\Method;
|
use Barryvdh\LaravelIdeHelper\Method;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Foundation\Application;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class MethodTest extends TestCase
|
class MethodTest extends TestCase
|
||||||
@@ -59,10 +58,6 @@ DOC;
|
|||||||
*/
|
*/
|
||||||
public function testEloquentBuilderOutput()
|
public function testEloquentBuilderOutput()
|
||||||
{
|
{
|
||||||
if ((int) Application::VERSION < 8) {
|
|
||||||
$this->markTestSkipped('This test requires Laravel 8.0 or higher');
|
|
||||||
}
|
|
||||||
|
|
||||||
$reflectionClass = new \ReflectionClass(Builder::class);
|
$reflectionClass = new \ReflectionClass(Builder::class);
|
||||||
$reflectionMethod = $reflectionClass->getMethod('with');
|
$reflectionMethod = $reflectionClass->getMethod('with');
|
||||||
|
|
||||||
@@ -102,6 +97,37 @@ DOC;
|
|||||||
$this->assertSame('$chars = \'$\\\'\\\\\'', $method->getParamsWithDefault(true));
|
$this->assertSame('$chars = \'$\\\'\\\\\'', $method->getParamsWithDefault(true));
|
||||||
$this->assertSame(['$chars = \'$\\\'\\\\\''], $method->getParamsWithDefault(false));
|
$this->assertSame(['$chars = \'$\\\'\\\\\''], $method->getParamsWithDefault(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the output of a class when using class aliases for it
|
||||||
|
*/
|
||||||
|
public function testClassAliases()
|
||||||
|
{
|
||||||
|
$reflectionClass = new \ReflectionClass(ExampleClass::class);
|
||||||
|
$reflectionMethod = $reflectionClass->getMethod('getApplication');
|
||||||
|
|
||||||
|
$method = new Method($reflectionMethod, 'Example', $reflectionClass, null, [], [
|
||||||
|
'Application' => '\\Illuminate\\Foundation\\Application',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$output = <<<'DOC'
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Foundation\Application
|
||||||
|
* @static
|
||||||
|
*/
|
||||||
|
DOC;
|
||||||
|
|
||||||
|
$this->assertSame($output, $method->getDocComment(''));
|
||||||
|
$this->assertSame('getApplication', $method->getName());
|
||||||
|
$this->assertSame('\\' . ExampleClass::class, $method->getDeclaringClass());
|
||||||
|
$this->assertSame('', $method->getParams(true));
|
||||||
|
$this->assertSame([], $method->getParams(false));
|
||||||
|
$this->assertSame('', $method->getParamsWithDefault(true));
|
||||||
|
$this->assertSame([], $method->getParamsWithDefault(false));
|
||||||
|
$this->assertTrue($method->shouldReturn());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ExampleClass
|
class ExampleClass
|
||||||
@@ -120,4 +146,12 @@ class ExampleClass
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Application
|
||||||
|
*/
|
||||||
|
public function getApplication()
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\UsesResolver;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class UsesResolverTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Test that we can correctly load uses from supplied code
|
||||||
|
*/
|
||||||
|
public function testLoadFromCode()
|
||||||
|
{
|
||||||
|
$usesResolver = new UsesResolver();
|
||||||
|
|
||||||
|
$code = <<<'DOC'
|
||||||
|
<?php
|
||||||
|
namespace Barryvdh\LaravelIdeHelper\Tests;
|
||||||
|
|
||||||
|
use Barryvdh\LaravelIdeHelper\UsesResolver as MyUsesResolver;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class UsesResolverTest extends TestCase
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
DOC;
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
$usesResolver->loadFromCode('Barryvdh\\LaravelIdeHelper\\Tests\\UsesResolverTest', $code),
|
||||||
|
[
|
||||||
|
'MyUsesResolver' => '\\Barryvdh\\LaravelIdeHelper\\UsesResolver',
|
||||||
|
'TestCase' => '\\PHPUnit\Framework\TestCase',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that we can correctly load uses from a class
|
||||||
|
*/
|
||||||
|
public function testLoadFromClass()
|
||||||
|
{
|
||||||
|
$usesResolver = new UsesResolver();
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
$usesResolver->loadFromClass(self::class),
|
||||||
|
[
|
||||||
|
'UsesResolver' => '\\Barryvdh\\LaravelIdeHelper\\UsesResolver',
|
||||||
|
'TestCase' => '\\PHPUnit\Framework\TestCase',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user