staudenmeir /
eloquent-has-many-deep
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Staudenmeir\EloquentHasManyDeep\Eloquent\Traits; |
||||
| 4 | |||||
| 5 | use Illuminate\Database\Eloquent\Model; |
||||
| 6 | use Illuminate\Database\Eloquent\Relations\Pivot; |
||||
| 7 | use Staudenmeir\EloquentHasManyDeep\HasManyDeep; |
||||
| 8 | use Staudenmeir\EloquentHasManyDeep\HasOneDeep; |
||||
| 9 | |||||
| 10 | trait ReversesRelationships |
||||
| 11 | { |
||||
| 12 | /** |
||||
| 13 | * Define a has-many-deep relationship by reversing an existing deep relationship. |
||||
| 14 | * |
||||
| 15 | * @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep $relation |
||||
| 16 | * @return \Staudenmeir\EloquentHasManyDeep\HasManyDeep |
||||
| 17 | */ |
||||
| 18 | public function hasManyDeepFromReverse(HasManyDeep $relation): HasManyDeep |
||||
| 19 | { |
||||
| 20 | return $this->hasManyDeep(...$this->hasOneOrManyDeepFromReverse($relation)); |
||||
|
0 ignored issues
–
show
|
|||||
| 21 | } |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * Define a has-one-deep relationship by reversing an existing deep relationship. |
||||
| 25 | * |
||||
| 26 | * @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep $relation |
||||
| 27 | * @return \Staudenmeir\EloquentHasManyDeep\HasOneDeep |
||||
| 28 | */ |
||||
| 29 | public function hasOneDeepFromReverse(HasManyDeep $relation): HasOneDeep |
||||
| 30 | { |
||||
| 31 | return $this->hasOneDeep(...$this->hasOneOrManyDeepFromReverse($relation)); |
||||
|
0 ignored issues
–
show
The method
hasOneDeep() does not exist on Staudenmeir\EloquentHasM...s\ReversesRelationships. Did you maybe mean hasOneDeepFromReverse()?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 32 | } |
||||
| 33 | |||||
| 34 | /** |
||||
| 35 | * Prepare a has-one-deep or has-many-deep relationship by reversing an existing deep relationship. |
||||
| 36 | * |
||||
| 37 | * @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep $relation |
||||
| 38 | * @return array |
||||
| 39 | */ |
||||
| 40 | protected function hasOneOrManyDeepFromReverse(HasManyDeep $relation): array |
||||
| 41 | { |
||||
| 42 | $related = $relation->getFarParent()::class; |
||||
| 43 | |||||
| 44 | $through = []; |
||||
| 45 | |||||
| 46 | foreach (array_reverse($relation->getThroughParents()) as $throughParent) { |
||||
| 47 | $through[] = $this->hasOneOrManyDeepFromReverseThroughClass($throughParent); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | $foreignKeys = array_reverse( |
||||
| 51 | $relation->getLocalKeys() |
||||
| 52 | ); |
||||
| 53 | |||||
| 54 | $localKeys = array_reverse( |
||||
| 55 | $relation->getForeignKeys() |
||||
| 56 | ); |
||||
| 57 | |||||
| 58 | return [$related, $through, $foreignKeys, $localKeys]; |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * Prepare a has-one-deep or has-many-deep relationship through class. |
||||
| 63 | * |
||||
| 64 | * @param \Illuminate\Database\Eloquent\Model $throughParent |
||||
| 65 | * @return string |
||||
| 66 | */ |
||||
| 67 | protected function hasOneOrManyDeepFromReverseThroughClass(Model $throughParent): string |
||||
| 68 | { |
||||
| 69 | $table = $throughParent->getTable(); |
||||
| 70 | |||||
| 71 | $segments = preg_split('/\s+as\s+/i', $table); |
||||
| 72 | |||||
| 73 | if ($throughParent instanceof Pivot) { |
||||
| 74 | if (isset($segments[1])) { |
||||
| 75 | $class = $throughParent::class . " as $segments[1]"; |
||||
| 76 | } else { |
||||
| 77 | $class = $table; |
||||
| 78 | } |
||||
| 79 | } else { |
||||
| 80 | $class = $throughParent::class; |
||||
| 81 | |||||
| 82 | if (isset($segments[1])) { |
||||
| 83 | $class .= " as $segments[1]"; |
||||
| 84 | } elseif ($table !== (new $throughParent())->getTable()) { |
||||
| 85 | $class .= " from $table"; |
||||
| 86 | } |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | return $class; |
||||
| 90 | } |
||||
| 91 | } |
||||
| 92 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.