HasMeta::toResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\ResourceLinks;
4
5
use Illuminate\Http\Resources\Json\ResourceCollection;
6
7
/** @mixin \Illuminate\Http\Resources\Json\JsonResource */
8
trait HasMeta
9
{
10
    public static function collection($resource)
11
    {
12
        $meta = self::meta();
13
14
        if (! count($meta)) {
15
            return parent::collection($resource);
16
        }
17
18
        return parent::collection($resource)->additional([
19
            'meta' => $meta,
20
        ]);
21
    }
22
23
    public static function make(...$parameters)
24
    {
25
        return parent::make(...$parameters)->withCollectionLinks();
26
    }
27
28
    public function toResponse($request)
29
    {
30
        if (is_subclass_of($this, ResourceCollection::class)) {
31
            $this->additional([
0 ignored issues
show
Bug introduced by
It seems like additional() 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 ignore-call  annotation

31
            $this->/** @scrutinizer ignore-call */ 
32
                   additional([
Loading history...
32
                'meta' => $meta = self::meta(),
0 ignored issues
show
Unused Code introduced by
The assignment to $meta is dead and can be removed.
Loading history...
33
            ]);
34
        }
35
36
        return parent::toResponse($request);
37
    }
38
39
    public static function meta()
40
    {
41
        return [];
42
    }
43
}
44