1 | <?php |
||||||
2 | |||||||
3 | namespace VGirol\JsonApi\Resources; |
||||||
4 | |||||||
5 | use Illuminate\Support\Collection; |
||||||
6 | use Illuminate\Support\Str; |
||||||
7 | |||||||
8 | trait HasRelationships |
||||||
9 | { |
||||||
10 | /** |
||||||
11 | * Undocumented function |
||||||
12 | * Could be overloaded, but it is not recommended. |
||||||
13 | * |
||||||
14 | * @param \Illuminate\Http\Request $request |
||||||
15 | * |
||||||
16 | * @return void |
||||||
17 | */ |
||||||
18 | protected function setRelationships($request) |
||||||
19 | { |
||||||
20 | foreach ($this->includes->map->first()->filter() as $include) { |
||||||
21 | // Get relationship |
||||||
22 | $relationship = $this->getRelationship($request, $include); |
||||||
0 ignored issues
–
show
|
|||||||
23 | |||||||
24 | // Add relationship |
||||||
25 | $this->addResourceRelationship($include, $relationship); |
||||||
0 ignored issues
–
show
It seems like
addResourceRelationship() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
26 | } |
||||||
27 | } |
||||||
28 | |||||||
29 | /** |
||||||
30 | * Undocumented function |
||||||
31 | * |
||||||
32 | * @param \Illuminate\Http\Request $request |
||||||
33 | * @param string $relationshipName |
||||||
34 | * |
||||||
35 | * @return void |
||||||
36 | */ |
||||||
37 | private function getRelationship($request, string $relationshipName) |
||||||
38 | { |
||||||
39 | // Get relationship collection |
||||||
40 | $elm = $this->resource->getRelation($relationshipName); |
||||||
41 | |||||||
42 | if ($elm instanceof Collection) { |
||||||
43 | $className = $this->aliases->getResourceIdentifierCollectionClassName(Str::plural($relationshipName)); |
||||||
44 | } else { |
||||||
45 | $className = $this->aliases->getResourceIdentifierClassName(Str::plural($relationshipName)); |
||||||
46 | } |
||||||
47 | |||||||
48 | return call_user_func_array( |
||||||
49 | [$className, 'make'], |
||||||
50 | [&$elm, $this->aliases] |
||||||
51 | )->asRelationship($request, $this->resource, $relationshipName); |
||||||
52 | } |
||||||
53 | |||||||
54 | /** |
||||||
55 | * Undocumented function |
||||||
56 | * Could be overloaded, but it is not recommended. |
||||||
57 | * |
||||||
58 | * @param \Illuminate\Http\Request $request |
||||||
59 | * |
||||||
60 | * @return static |
||||||
61 | */ |
||||||
62 | protected function setIncluded($request) |
||||||
63 | { |
||||||
64 | /** @var Collection $included A collection of ResourceObject objects */ |
||||||
65 | $included = $this->getAllIncludedResources(); |
||||||
66 | |||||||
67 | if ($included->isEmpty()) { |
||||||
68 | return; |
||||||
69 | } |
||||||
70 | |||||||
71 | $this->addIncludedResources( |
||||||
0 ignored issues
–
show
It seems like
addIncludedResources() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
72 | $included->map->asIncluded($request)->toArray() |
||||||
73 | ); |
||||||
74 | |||||||
75 | return $this; |
||||||
76 | } |
||||||
77 | |||||||
78 | /** |
||||||
79 | * Undocumented function |
||||||
80 | * |
||||||
81 | * @return Collection |
||||||
82 | */ |
||||||
83 | public function getAllIncludedResources() |
||||||
84 | { |
||||||
85 | $collection = collect([]); |
||||||
86 | foreach ($this->includes as $include) { |
||||||
87 | /** @var Collection $include */ |
||||||
88 | |||||||
89 | if ($include->isEmpty()) { |
||||||
90 | continue; |
||||||
91 | } |
||||||
92 | |||||||
93 | // Retrieve included resources |
||||||
94 | /** @var Collection $inc */ |
||||||
95 | $inc = $this->retrieveIncludedTree(clone $include); |
||||||
96 | if ($inc->isEmpty()) { |
||||||
97 | continue; |
||||||
98 | } |
||||||
99 | |||||||
100 | $collection = $collection->merge($inc); |
||||||
101 | } |
||||||
102 | |||||||
103 | return $collection->unique()->values(); |
||||||
104 | } |
||||||
105 | |||||||
106 | /** |
||||||
107 | * Returns a collection of ResourceObject objects. |
||||||
108 | * |
||||||
109 | * @param Collection $include |
||||||
110 | * |
||||||
111 | * @return Collection |
||||||
112 | */ |
||||||
113 | private function retrieveIncludedTree($include) |
||||||
114 | { |
||||||
115 | // Get relationship name |
||||||
116 | $relationshipName = $include->shift(); |
||||||
117 | |||||||
118 | // Get related collection |
||||||
119 | if (!$this->resource->relationLoaded($relationshipName)) { |
||||||
120 | $this->resource->load($relationshipName); |
||||||
121 | } |
||||||
122 | $elm = $this->resource->getRelation($relationshipName); |
||||||
123 | if (!($elm instanceof Collection)) { |
||||||
124 | $elm = collect([$elm]); |
||||||
125 | } |
||||||
126 | |||||||
127 | // Get resource class name |
||||||
128 | $routeKey = Str::plural($relationshipName); |
||||||
129 | $className = $this->aliases->getResourceClassName($routeKey); |
||||||
130 | |||||||
131 | $inc = $elm->map(function ($item) use ($className, $include) { |
||||||
132 | return call_user_func_array( |
||||||
133 | [$className, 'make'], |
||||||
134 | [$item, collect([$include])] |
||||||
135 | ); |
||||||
136 | }); |
||||||
137 | |||||||
138 | if ($inc->isNotEmpty() && $include->isNotEmpty()) { |
||||||
139 | $inc = $inc->merge( |
||||||
140 | $inc->flatmap( |
||||||
141 | function ($item) use ($include) { |
||||||
142 | return $item->retrieveIncludedTree(clone $include); |
||||||
143 | } |
||||||
144 | ) |
||||||
145 | ); |
||||||
146 | } |
||||||
147 | |||||||
148 | return $inc->unique(); |
||||||
149 | } |
||||||
150 | } |
||||||
151 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.