Completed
Pull Request — master (#309)
by
unknown
03:02
created

JsonApiBookTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 57.14 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 8 1
A includeAuthor() 12 12 3
A includeCoAuthor() 12 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace League\Fractal\Test\Stub\Transformer;
2
3
use League\Fractal\TransformerAbstract;
4
5
class JsonApiBookTransformer extends TransformerAbstract
6
{
7
    protected $availableIncludes = [
8
        'author',
9
        'co-author',
10
    ];
11
12
    public function transform(array $book)
13
    {
14
        $book['year'] = (int) $book['year'];
15
        unset($book['_author']);
16
        unset($book['_co_author']);
17
18
        return $book;
19
    }
20
21 View Code Duplication
    public function includeAuthor(array $book)
22
    {
23
        if (!array_key_exists('_author', $book)) {
24
            return;
25
        }
26
27
        if ($book['_author'] === null) {
28
            return $this->null();
29
        }
30
31
        return $this->item($book['_author'], new JsonApiAuthorTransformer(), 'people');
32
    }
33
34 View Code Duplication
    public function includeCoAuthor(array $book)
35
    {
36
        if (!array_key_exists('_co_author', $book)) {
37
            return;
38
        }
39
40
        if ($book['_co_author'] === null) {
41
            return $this->null();
42
        }
43
44
        return $this->item($book['_co_author'], new JsonApiAuthorTransformer(), 'people');
45
    }
46
}
47