1 | <?php |
||
15 | abstract class ApiResource implements ArrayAccess, ResourceContract |
||
16 | { |
||
17 | use ArrayAccessTrait; |
||
18 | |||
19 | /** |
||
20 | * @var \Laravel\Forge\ApiProvider |
||
21 | */ |
||
22 | protected $api; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $data = []; |
||
28 | |||
29 | /** |
||
30 | * @var \Laravel\Forge\ApiResource |
||
31 | */ |
||
32 | protected $owner; |
||
33 | |||
34 | /** |
||
35 | * Create new resource instance. |
||
36 | * |
||
37 | * @param \Laravel\Forge\ApiProvider $api = null |
||
38 | * @param array $data = [] |
||
39 | * @param \Laravel\Forge\ApiResource $owner |
||
40 | */ |
||
41 | public function __construct(ApiProvider $api = null, array $data = [], ApiResource $owner = null) |
||
49 | |||
50 | /** |
||
51 | * Resource type. |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | abstract public static function resourceType(); |
||
56 | |||
57 | /** |
||
58 | * Resource path (relative to owner or API root). |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | abstract public function resourcePath(); |
||
63 | |||
64 | /** |
||
65 | * Initialize new resource. |
||
66 | * |
||
67 | * @return mixed |
||
68 | */ |
||
69 | protected function initializeResource() |
||
73 | |||
74 | /** |
||
75 | * Create new Resource instance from HTTP response. |
||
76 | * |
||
77 | * @param \Psr\Http\Message\ResponseInterface $response |
||
78 | * @param \Laravel\Forge\ApiProvider $api |
||
79 | * @param \Laravel\Forge\ApiResource $owner = null |
||
80 | */ |
||
81 | public static function createFromResponse(ResponseInterface $response, ApiProvider $api, ApiResource $owner = null) |
||
92 | |||
93 | /** |
||
94 | * Throw HTTP Not Found exception. |
||
95 | * |
||
96 | * @throws \Exception |
||
97 | */ |
||
98 | protected static function throwNotFoundException() |
||
102 | |||
103 | /** |
||
104 | * Determines if current resource has an owner. |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | public function hasResourceOwner(): bool |
||
112 | |||
113 | /** |
||
114 | * Get current resource owner. |
||
115 | * |
||
116 | * @return \Laravel\Forge\ApiResource|null |
||
117 | */ |
||
118 | public function resourceOwner() |
||
122 | |||
123 | /** |
||
124 | * Get API provider. |
||
125 | * |
||
126 | * @return \Laravel\Forge\ApiProvider |
||
127 | */ |
||
128 | public function getApi(): ApiProvider |
||
132 | |||
133 | /** |
||
134 | * Get underlying API provider's HTTP client. |
||
135 | * |
||
136 | * @return \GuzzleHttp\ClientInterface |
||
137 | */ |
||
138 | public function getHttpClient(): ClientInterface |
||
142 | |||
143 | /** |
||
144 | * Get resource data. |
||
145 | * |
||
146 | * @param string|int $key |
||
147 | * @param mixed $default = null |
||
148 | * |
||
149 | * @return mixed|null |
||
150 | */ |
||
151 | public function getData($key, $default = null) |
||
155 | |||
156 | /** |
||
157 | * Resource API URL. |
||
158 | * |
||
159 | * @param string $path = '' |
||
160 | * @param bool $withPropagation = true |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | public function apiUrl(string $path = '', bool $withPropagation = true): string |
||
175 | |||
176 | /** |
||
177 | * Resource ID. |
||
178 | * |
||
179 | * @return int |
||
180 | */ |
||
181 | public function id(): int |
||
185 | |||
186 | /** |
||
187 | * Resource name. |
||
188 | * |
||
189 | * @return string|null |
||
190 | */ |
||
191 | public function name() |
||
195 | |||
196 | /** |
||
197 | * Resource status. |
||
198 | * |
||
199 | * @return string|null |
||
200 | */ |
||
201 | public function status() |
||
205 | |||
206 | /** |
||
207 | * Get resource creation date. |
||
208 | * |
||
209 | * @return string|null |
||
210 | */ |
||
211 | public function createdAt() |
||
215 | |||
216 | /** |
||
217 | * Update resource data. |
||
218 | * |
||
219 | * @throws UpdateResourceException |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | public function update(array $payload): bool |
||
245 | |||
246 | /** |
||
247 | * Delete current resource. |
||
248 | * |
||
249 | * @throws \Laravel\Forge\Exceptions\Resources\DeleteResourceException |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function delete() |
||
263 | |||
264 | /** |
||
265 | * @throws \Exception |
||
266 | */ |
||
267 | protected function throwResourceException(ResponseInterface $response, string $action, string $className) |
||
274 | } |
||
275 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: