Completed
Pull Request — master (#159)
by Robin
01:14
created

TransformerAbstract   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 4
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __call() 0 16 3
1
<?php
2
3
namespace Spatie\Fractal;
4
5
use Illuminate\Support\Str;
6
use League\Fractal\Resource\Item;
7
use League\Fractal\Resource\Collection;
8
use League\Fractal\TransformerAbstract as FractalTransformer;
9
10
abstract class TransformerAbstract extends FractalTransformer
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $itemIncludes = [
16
        // 'foo' => FooTransformer::class
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
17
    ];
18
19
    /**
20
     * @var array
21
     */
22
    protected $collectionIncludes = [
23
        // 'bar' => BarTransformer::class
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
24
    ];
25
26
    /**
27
     * TransformerAbstract constructor.
28
     */
29
    public function __construct()
30
    {
31
        $this->availableIncludes = array_merge($this->availableIncludes,
32
            array_keys($this->itemIncludes),
33
            array_keys($this->collectionIncludes));
34
    }
35
36
    /**
37
     * Simulate 'include{$resource}()' methods.
38
     *
39
     * @param string $method
40
     * @param array  $arguments
41
     * @return Collection|Item
42
     */
43
    public function __call($method, $arguments)
44
    {
45
        $baseResource = $arguments[0] ?? null;
46
        $relatedResource = Str::snake(preg_replace('/^include/', '', $method));
47
48
        if (array_key_exists($relatedResource, $this->itemIncludes)) {
49
            return $this->item($baseResource->{$relatedResource}, new $this->itemIncludes[$relatedResource]);
50
        }
51
52
        if (array_key_exists($relatedResource, $this->collectionIncludes)) {
53
            return $this->collection(
54
                $baseResource->{$relatedResource}, new $this->collectionIncludes[$relatedResource]);
55
        }
56
57
        throw new \BadMethodCallException('Unknown method "'.$method.'" called on '.static::class.'.');
58
    }
59
}
60