Skip to content

Commit 7564f98

Browse files
authored
Merge branch 'master' into feature/protected-accessor
2 parents 656b932 + ee9188a commit 7564f98

File tree

12 files changed

+197
-8
lines changed

12 files changed

+197
-8
lines changed

.github/workflows/run-integration-tests.yml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,22 @@ jobs:
5757
- name: Execute meta run
5858
run: |
5959
cd sample
60-
php artisan ide-helper:meta
60+
php artisan ide-helper:meta -v
6161
6262
- name: Check file existence
6363
run: |
6464
ls sample/_ide_helper.php
6565
ls sample/.phpstorm.meta.php
6666
67-
- name: Check file count in logs
67+
- name: Check logs
6868
run: |
69-
if [ `ls -1q "sample/storage/logs/" | wc -l` -gt 0 ];then exit 1;fi
69+
if [ `ls -1q "sample/storage/logs/" | wc -l` -gt 0 ]; then
70+
for logfile in sample/storage/logs/*; do
71+
echo "-- $logfile --"
72+
cat $logfile
73+
done
74+
exit 1
75+
fi
7076
7177
php-laravel-integration-tests:
7278
runs-on: ubuntu-20.04
@@ -115,13 +121,20 @@ jobs:
115121
- name: Execute meta run
116122
run: |
117123
cd sample
118-
php artisan ide-helper:meta
124+
php artisan ide-helper:meta -v
119125
120126
- name: Check file existence
121127
run: |
122128
ls sample/_ide_helper.php
123129
ls sample/.phpstorm.meta.php
124130
125-
- name: Check file count in logs
131+
132+
- name: Check logs
126133
run: |
127-
if [ `ls -1q "sample/storage/logs/" | wc -l` -gt 0 ];then exit 1;fi
134+
if [ `ls -1q "sample/storage/logs/" | wc -l` -gt 0 ]; then
135+
for logfile in sample/storage/logs/*; do
136+
echo "-- $logfile --"
137+
cat $logfile
138+
done
139+
exit 1
140+
fi

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ All notable changes to this project will be documented in this file.
88
### Added
99
- Add support for attribute accessors marked as protected. [#1339 / pindab0ter](https://github.com/barryvdh/laravel-ide-helper/pull/1339)
1010

11+
### Fixed
12+
- Handle PHP 8.1 deprecation warnings when passing `null` to `new \ReflectionClass` [#1351 / mfn](https://github.com/barryvdh/laravel-ide-helper/pull/1351)
13+
1114
2022-03-06, 2.12.3
1215
------------------
1316

@@ -30,6 +33,7 @@ All notable changes to this project will be documented in this file.
3033
### Added
3134
- Add support for custom casts that using `Castable` [#1287 / binotaliu](https://github.com/barryvdh/laravel-ide-helper/pull/1287)
3235
- Added Laravel 9 support [#1297 / rcerljenko](https://github.com/barryvdh/laravel-ide-helper/pull/1297)
36+
- Added option `additional_relation_return_types` for custom relations that don't fit the typical naming scheme
3337

3438
2022-01-03, 2.11.0
3539
------------------

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
This package generates helper files that enable your IDE to provide accurate autocompletion.
1212
Generation is done based on the files in your project, so they are always up-to-date.
1313

14+
It supports Laravel 8+ and PHP 7.3+
15+
1416
- [Installation](#installation)
1517
- [Usage](#usage)
1618
- [Automatic PHPDoc generation for Laravel Facades](#automatic-phpdoc-generation-for-laravel-facades)
@@ -279,6 +281,26 @@ For those special cases, you can map them via the config `custom_db_types`. Exam
279281
],
280282
```
281283

284+
#### Custom Relationship Types
285+
286+
If you are using relationships not built into Laravel you will need to specify the name and returning class in the config to get proper generation.
287+
288+
```php
289+
'additional_relation_types' => [
290+
'externalHasMany' => \My\Package\externalHasMany::class
291+
],
292+
```
293+
294+
Found relationships will typically generate a return value based on the name of the relationship.
295+
296+
If your custom relationships don't follow this traditional naming scheme you can define its return type manually. The available options are `many` and `morphTo`.
297+
298+
```php
299+
'additional_relation_return_types' => [
300+
'externalHasMultiple' => 'many'
301+
],
302+
```
303+
282304
#### Model Hooks
283305

284306
If you need additional information on your model from sources that are not handled by default, you can hook in to the

config/ide-helper.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,19 @@
304304
*/
305305
'additional_relation_types' => [],
306306

307+
/*
308+
|--------------------------------------------------------------------------
309+
| Additional relation return types
310+
|--------------------------------------------------------------------------
311+
|
312+
| When using custom relation types its possible for the class name to not contain
313+
| the proper return type of the relation. The key of the array is the relationship
314+
| method name. The value of the array is the return type of the relation.
315+
| e.g. `'relationName' => 'many'`.
316+
|
317+
*/
318+
'additional_relation_return_types' => [],
319+
307320
/*
308321
|--------------------------------------------------------------------------
309322
| Run artisan commands after migrations to generate model helpers

src/Console/MetaCommand.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Barryvdh\LaravelIdeHelper\Factories;
1515
use Illuminate\Console\Command;
16+
use RuntimeException;
1617
use Symfony\Component\Console\Input\InputOption;
1718
use Symfony\Component\Console\Output\OutputInterface;
1819

@@ -95,6 +96,11 @@ public function handle()
9596

9697
try {
9798
$concrete = $this->laravel->make($abstract);
99+
100+
if ($concrete === null) {
101+
throw new RuntimeException("Cannot create instance for '$abstract', received 'null'");
102+
}
103+
98104
$reflectionClass = new \ReflectionClass($concrete);
99105
if (is_object($concrete) && !$reflectionClass->isAnonymous()) {
100106
$bindings[$abstract] = get_class($concrete);

src/Console/ModelsCommand.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,10 @@ public function getPropertiesFromMethods($model)
714714
get_class($relationObj->getRelated())
715715
);
716716

717-
if (strpos(get_class($relationObj), 'Many') !== false) {
717+
if (
718+
strpos(get_class($relationObj), 'Many') !== false ||
719+
($this->getRelationReturnTypes()[$relation] ?? '') === 'many'
720+
) {
718721
//Collection or array of models (because Collection is Arrayable)
719722
$relatedClass = '\\' . get_class($relationObj->getRelated());
720723
$collectionClass = $this->getCollectionClass($relatedClass);
@@ -738,7 +741,10 @@ public function getPropertiesFromMethods($model)
738741
// What kind of comments should be added to the relation count here?
739742
);
740743
}
741-
} elseif ($relation === 'morphTo') {
744+
} elseif (
745+
$relation === 'morphTo' ||
746+
($this->getRelationReturnTypes()[$relation] ?? '') === 'morphTo'
747+
) {
742748
// Model isn't specified because relation is polymorphic
743749
$this->setProperty(
744750
$methodReflection->getName(),
@@ -1086,6 +1092,14 @@ protected function getRelationTypes(): array
10861092
return array_merge(self::RELATION_TYPES, $configuredRelations);
10871093
}
10881094

1095+
/**
1096+
* Returns the return types of relations
1097+
*/
1098+
protected function getRelationReturnTypes(): array
1099+
{
1100+
return $this->laravel['config']->get('ide-helper.additional_relation_return_types', []);
1101+
}
1102+
10891103
/**
10901104
* @return bool
10911105
*/

tests/Console/ModelsCommand/Relations/Models/Simple.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,14 @@ public function relationSampleRelationType()
9797
{
9898
return $this->testToManyRelation(Simple::class);
9999
}
100+
101+
public function relationSampleToAnyRelationType()
102+
{
103+
return $this->testToAnyRelation(Simple::class);
104+
}
105+
106+
public function relationSampleToAnyMorphedRelationType()
107+
{
108+
return $this->testToAnyMorphedRelation(Simple::class);
109+
}
100110
}

tests/Console/ModelsCommand/Relations/Test.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
88
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
9+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyRelationType;
910
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToManyRelationType;
1011
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToOneRelationType;
1112
use Illuminate\Support\Facades\Config;
@@ -19,6 +20,13 @@ protected function setUp(): void
1920
Config::set('ide-helper.additional_relation_types', [
2021
'testToOneRelation' => SampleToOneRelationType::class,
2122
'testToManyRelation' => SampleToManyRelationType::class,
23+
'testToAnyRelation' => SampleToAnyRelationType::class,
24+
'testToAnyMorphedRelation' => SampleToAnyMorphedRelationType::class,
25+
]);
26+
27+
Config::set('ide-helper.additional_relation_return_types', [
28+
'testToAnyRelation' => 'many',
29+
'testToAnyMorphedRelation' => 'morphTo',
2230
]);
2331
}
2432

tests/Console/ModelsCommand/Relations/Traits/HasTestRelations.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Traits;
66

7+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyRelationType;
8+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToAnyMorphedRelationType;
79
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToManyRelationType;
810
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types\SampleToOneRelationType;
911

@@ -20,4 +22,16 @@ public function testToManyRelation($related)
2022
$instance = $this->newRelatedInstance($related);
2123
return new SampleToManyRelationType($instance->newQuery(), $this);
2224
}
25+
26+
public function testToAnyRelation($related)
27+
{
28+
$instance = $this->newRelatedInstance($related);
29+
return new SampleToAnyRelationType($instance->newQuery(), $this);
30+
}
31+
32+
public function testToAnyMorphedRelation($related)
33+
{
34+
$instance = $this->newRelatedInstance($related);
35+
return new SampleToAnyMorphedRelationType($instance->newQuery(), $this);
36+
}
2337
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Types;
6+
7+
use Illuminate\Database\Eloquent\Collection;
8+
use Illuminate\Database\Eloquent\Relations\Relation;
9+
10+
class SampleToAnyMorphedRelationType extends Relation
11+
{
12+
public function addConstraints()
13+
{
14+
// Fake
15+
}
16+
17+
public function addEagerConstraints(array $models)
18+
{
19+
// Fake
20+
}
21+
22+
public function initRelation(array $models, $relation)
23+
{
24+
// Fake
25+
}
26+
27+
public function match(array $models, Collection $results, $relation)
28+
{
29+
// Fake
30+
}
31+
32+
public function getResults()
33+
{
34+
// Fake
35+
}
36+
}

0 commit comments

Comments
 (0)