From 0831b7e8e53bc0654c09c211e6814b068cb849ca Mon Sep 17 00:00:00 2001 From: pataar Date: Fri, 18 Oct 2024 16:39:09 +0200 Subject: [PATCH 1/6] feat(pivot): add support for multiple pivot types when using the same accessor --- src/Console/ModelsCommand.php | 12 +++++ .../Pivot/Models/ModelWithPivot.php | 23 ++++++++ .../Models/Pivots/DifferentCustomPivot.php | 11 ++++ .../Pivot/__snapshots__/Test__test__1.php | 52 ++++++++++++++++++- 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 tests/Console/ModelsCommand/Pivot/Models/Pivots/DifferentCustomPivot.php diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index de834bd14..cd81b8098 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -714,6 +714,17 @@ public function getPropertiesFromMethods($model) if ($relationObj instanceof BelongsToMany) { $pivot = get_class($relationObj->newPivot()); if (!in_array($pivot, [Pivot::class, MorphPivot::class])) { + + $pivot = $this->getClassNameInDestinationFile($model, $pivot); + + if($existingPivot = ($this->properties[$relationObj->getPivotAccessor()] ?? null)){ + // If the pivot is already set, we need to append the type to it + $pivot .= '|'. $existingPivot['type']; + } else{ + // pivots are not always set + $pivot .= '|null'; + } + $this->setProperty( $relationObj->getPivotAccessor(), $this->getClassNameInDestinationFile($model, $pivot), @@ -722,6 +733,7 @@ public function getPropertiesFromMethods($model) ); } } + //Collection or array of models (because Collection is Arrayable) $relatedClass = '\\' . get_class($relationObj->getRelated()); $collectionClass = $this->getCollectionClass($relatedClass); diff --git a/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php b/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php index 3f9e0bd2c..a01adb325 100644 --- a/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php +++ b/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php @@ -5,6 +5,7 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots\CustomPivot; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots\DifferentCustomPivot; use Illuminate\Database\Eloquent\Model; class ModelWithPivot extends Model @@ -15,4 +16,26 @@ public function relationWithCustomPivot() ->using(CustomPivot::class) ->as('customAccessor'); } + + + public function relationWithDifferentCustomPivot() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(DifferentCustomPivot::class) + ->as('differentCustomAccessor'); + } + + // without an accessor + + public function relationCustomPivotUsingSameAccessor() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(CustomPivot::class); + } + + public function relationWithDifferentCustomPivotUsingSameAccessor() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(DifferentCustomPivot::class); + } } diff --git a/tests/Console/ModelsCommand/Pivot/Models/Pivots/DifferentCustomPivot.php b/tests/Console/ModelsCommand/Pivot/Models/Pivots/DifferentCustomPivot.php new file mode 100644 index 000000000..71a642657 --- /dev/null +++ b/tests/Console/ModelsCommand/Pivot/Models/Pivots/DifferentCustomPivot.php @@ -0,0 +1,11 @@ + $relationCustomPivotUsingSameAccessor + * @property-read int|null $relation_custom_pivot_using_same_accessor_count + * @property-read \CustomPivot|null $customAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithCustomPivot * @property-read int|null $relation_with_custom_pivot_count + * @property-read \DifferentCustomPivot|null $differentCustomAccessor + * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivot + * @property-read int|null $relation_with_different_custom_pivot_count + * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivotUsingSameAccessor + * @property-read int|null $relation_with_different_custom_pivot_using_same_accessor_count * @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot newQuery() * @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot query() @@ -26,6 +35,28 @@ public function relationWithCustomPivot() ->using(CustomPivot::class) ->as('customAccessor'); } + + + public function relationWithDifferentCustomPivot() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(DifferentCustomPivot::class) + ->as('differentCustomAccessor'); + } + + // without an accessor + + public function relationCustomPivotUsingSameAccessor() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(CustomPivot::class); + } + + public function relationWithDifferentCustomPivotUsingSameAccessor() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(DifferentCustomPivot::class); + } } |DifferentCustomPivot newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|DifferentCustomPivot newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|DifferentCustomPivot query() + * @mixin \Eloquent + */ +class DifferentCustomPivot extends Pivot +{ +} From d7817ec017cad87d617fab8f6bdd3db17215f560 Mon Sep 17 00:00:00 2001 From: laravel-ide-helper Date: Fri, 18 Oct 2024 14:40:37 +0000 Subject: [PATCH 2/6] composer fix-style --- src/Console/ModelsCommand.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index cd81b8098..2368048f9 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -714,13 +714,12 @@ public function getPropertiesFromMethods($model) if ($relationObj instanceof BelongsToMany) { $pivot = get_class($relationObj->newPivot()); if (!in_array($pivot, [Pivot::class, MorphPivot::class])) { - $pivot = $this->getClassNameInDestinationFile($model, $pivot); - if($existingPivot = ($this->properties[$relationObj->getPivotAccessor()] ?? null)){ + if ($existingPivot = ($this->properties[$relationObj->getPivotAccessor()] ?? null)) { // If the pivot is already set, we need to append the type to it - $pivot .= '|'. $existingPivot['type']; - } else{ + $pivot .= '|' . $existingPivot['type']; + } else { // pivots are not always set $pivot .= '|null'; } From a39c9faa31d0a676ff2aa10794bc5f5e2f62b95a Mon Sep 17 00:00:00 2001 From: pataar Date: Fri, 18 Oct 2024 16:43:56 +0200 Subject: [PATCH 3/6] changelog --- CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 039bc4072..f8a2643d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,13 @@ All notable changes to this project will be documented in this file. -[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v3.1.0...master) +[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v3.2.0...master) +-------------- + +### Changed +Add support for multiple pivot types when using the same accessor. + +2024-10-18, 3.2.0 -------------- ### Fixed From 37559ab089b5e21bc7c227eeca0734cffb492e69 Mon Sep 17 00:00:00 2001 From: pataar Date: Fri, 18 Oct 2024 16:52:09 +0200 Subject: [PATCH 4/6] resolve namespaces --- src/Console/ModelsCommand.php | 26 ++++++++++--------- .../Pivot/__snapshots__/Test__test__1.php | 6 ++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 2368048f9..1834c79a5 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -671,6 +671,8 @@ public function getPropertiesFromMethods($model) $begin = strpos($code, 'function('); $code = substr($code, $begin, strrpos($code, '}') - $begin + 1); + $customPivots = []; + foreach ( $this->getRelationTypes() as $relation => $impl ) { @@ -716,20 +718,11 @@ public function getPropertiesFromMethods($model) if (!in_array($pivot, [Pivot::class, MorphPivot::class])) { $pivot = $this->getClassNameInDestinationFile($model, $pivot); - if ($existingPivot = ($this->properties[$relationObj->getPivotAccessor()] ?? null)) { - // If the pivot is already set, we need to append the type to it - $pivot .= '|' . $existingPivot['type']; + if(isset($customPivots[$relationObj->getPivotAccessor()])){ + $customPivots[$relationObj->getPivotAccessor()][] = $pivot; } else { - // pivots are not always set - $pivot .= '|null'; + $customPivots[$relationObj->getPivotAccessor()] = [$pivot]; } - - $this->setProperty( - $relationObj->getPivotAccessor(), - $this->getClassNameInDestinationFile($model, $pivot), - true, - false - ); } } @@ -787,6 +780,15 @@ public function getPropertiesFromMethods($model) } } } + + foreach($customPivots as $pivotAccessor => $pivots){ + $this->setProperty( + $pivotAccessor, + implode('|', $pivots) . '|null', + true, + false, + ); + } } } } diff --git a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php index 526abd748..4ef086b17 100644 --- a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php @@ -11,15 +11,15 @@ /** * * - * @property-read \DifferentCustomPivot|\CustomPivot|null $pivot * @property-read \Illuminate\Database\Eloquent\Collection $relationCustomPivotUsingSameAccessor * @property-read int|null $relation_custom_pivot_using_same_accessor_count - * @property-read \CustomPivot|null $customAccessor + * @property-read DifferentCustomPivot|null $pivot * @property-read \Illuminate\Database\Eloquent\Collection $relationWithCustomPivot * @property-read int|null $relation_with_custom_pivot_count - * @property-read \DifferentCustomPivot|null $differentCustomAccessor + * @property-read CustomPivot|null $customAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivot * @property-read int|null $relation_with_different_custom_pivot_count + * @property-read DifferentCustomPivot|null $differentCustomAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivotUsingSameAccessor * @property-read int|null $relation_with_different_custom_pivot_using_same_accessor_count * @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot newModelQuery() From d7806bdaf412f32fee9bc98ba32c3e9425f1ccb5 Mon Sep 17 00:00:00 2001 From: pataar Date: Fri, 18 Oct 2024 16:57:38 +0200 Subject: [PATCH 5/6] Revert "resolve namespaces" This reverts commit 37559ab089b5e21bc7c227eeca0734cffb492e69. --- src/Console/ModelsCommand.php | 26 +++++++++---------- .../Pivot/__snapshots__/Test__test__1.php | 6 ++--- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 1834c79a5..2368048f9 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -671,8 +671,6 @@ public function getPropertiesFromMethods($model) $begin = strpos($code, 'function('); $code = substr($code, $begin, strrpos($code, '}') - $begin + 1); - $customPivots = []; - foreach ( $this->getRelationTypes() as $relation => $impl ) { @@ -718,11 +716,20 @@ public function getPropertiesFromMethods($model) if (!in_array($pivot, [Pivot::class, MorphPivot::class])) { $pivot = $this->getClassNameInDestinationFile($model, $pivot); - if(isset($customPivots[$relationObj->getPivotAccessor()])){ - $customPivots[$relationObj->getPivotAccessor()][] = $pivot; + if ($existingPivot = ($this->properties[$relationObj->getPivotAccessor()] ?? null)) { + // If the pivot is already set, we need to append the type to it + $pivot .= '|' . $existingPivot['type']; } else { - $customPivots[$relationObj->getPivotAccessor()] = [$pivot]; + // pivots are not always set + $pivot .= '|null'; } + + $this->setProperty( + $relationObj->getPivotAccessor(), + $this->getClassNameInDestinationFile($model, $pivot), + true, + false + ); } } @@ -780,15 +787,6 @@ public function getPropertiesFromMethods($model) } } } - - foreach($customPivots as $pivotAccessor => $pivots){ - $this->setProperty( - $pivotAccessor, - implode('|', $pivots) . '|null', - true, - false, - ); - } } } } diff --git a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php index 4ef086b17..526abd748 100644 --- a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php @@ -11,15 +11,15 @@ /** * * + * @property-read \DifferentCustomPivot|\CustomPivot|null $pivot * @property-read \Illuminate\Database\Eloquent\Collection $relationCustomPivotUsingSameAccessor * @property-read int|null $relation_custom_pivot_using_same_accessor_count - * @property-read DifferentCustomPivot|null $pivot + * @property-read \CustomPivot|null $customAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithCustomPivot * @property-read int|null $relation_with_custom_pivot_count - * @property-read CustomPivot|null $customAccessor + * @property-read \DifferentCustomPivot|null $differentCustomAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivot * @property-read int|null $relation_with_different_custom_pivot_count - * @property-read DifferentCustomPivot|null $differentCustomAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivotUsingSameAccessor * @property-read int|null $relation_with_different_custom_pivot_using_same_accessor_count * @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot newModelQuery() From 8c260252facd729434a4f694fdcc83bbb2f9c7da Mon Sep 17 00:00:00 2001 From: pataar Date: Fri, 18 Oct 2024 16:58:29 +0200 Subject: [PATCH 6/6] resolve namespaces --- src/Console/ModelsCommand.php | 2 +- .../ModelsCommand/Pivot/__snapshots__/Test__test__1.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 2368048f9..9cc287d69 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -726,7 +726,7 @@ public function getPropertiesFromMethods($model) $this->setProperty( $relationObj->getPivotAccessor(), - $this->getClassNameInDestinationFile($model, $pivot), + $pivot, true, false ); diff --git a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php index 526abd748..b51b53dfd 100644 --- a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php @@ -11,13 +11,13 @@ /** * * - * @property-read \DifferentCustomPivot|\CustomPivot|null $pivot + * @property-read DifferentCustomPivot|CustomPivot|null $pivot * @property-read \Illuminate\Database\Eloquent\Collection $relationCustomPivotUsingSameAccessor * @property-read int|null $relation_custom_pivot_using_same_accessor_count - * @property-read \CustomPivot|null $customAccessor + * @property-read CustomPivot|null $customAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithCustomPivot * @property-read int|null $relation_with_custom_pivot_count - * @property-read \DifferentCustomPivot|null $differentCustomAccessor + * @property-read DifferentCustomPivot|null $differentCustomAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivot * @property-read int|null $relation_with_different_custom_pivot_count * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivotUsingSameAccessor