ObjectToResult::transform()   C
last analyzed

Complexity

Conditions 11
Paths 99

Size

Total Lines 62
Code Lines 28

Duplication

Lines 8
Ratio 12.9 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 8
loc 62
ccs 0
cts 50
cp 0
rs 6.1722
cc 11
eloc 28
nc 99
nop 1
crap 132

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace ApaiIO\ResponseTransformer;
4
5
class ObjectToResult extends ObjectToArray implements ResponseTransformerInterface {
6
7
    public function transform($response)
8
    {
9
        $data = array();
10
        $response = $this->buildArray( $response );
0 ignored issues
show
Bug introduced by
The method buildArray() does not seem to exist on object<ApaiIO\ResponseTransformer\ObjectToResult>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
11
12
        if( !isset( $response['Items']['Item'] ) )
13
        {
14
            return $data;
15
        }
16
17
        foreach( $response['Items']['Item'] as $item )
18
        {
19
            if( !isset( $item['ItemAttributes']['Title'] ) )
20
            {
21
                continue;
22
            }
23
24
            $row = array();
25
26
            $row['asin'] = $item['ASIN'];
27
            $row['title'] = strip_tags( $item['ItemAttributes']['Title'] );
28
29
            if( isset( $item['LargeImage']['URL'] ) )
30
            {
31
                $row['large_image'] = $item['LargeImage']['URL'];
32
            }
33
34
            if( isset( $item['MediumImage']['URL'] ) )
35
            {
36
                $row['medium_image'] = $item['MediumImage']['URL'];
37
            }
38
39
            if( isset( $item['SmallImage']['URL'] ) )
40
            {
41
                $row['small_image'] = $item['SmallImage']['URL'];
42
            }
43
44 View Code Duplication
            if( isset( $item['ItemAttributes']['ISBN'] ) )
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
            {
46
                $row['isbn'] = $item['ItemAttributes']['ISBN'];
47
            }
48
49 View Code Duplication
            if( isset( $item['ItemAttributes']['Edition'] ) )
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            {
51
                $row['edition'] = $item['ItemAttributes']['Edition'];
52
            }
53
54
            if( isset( $item['ItemAttributes']['Author'] ) )
55
            {
56
                $author = $item['ItemAttributes']['Author'];
57
                if( ! is_array( $author ) )
58
                {
59
                    $author = array($author);
60
                }
61
                $row['author'] = $author;
62
            }
63
64
            $data[] = $row;
65
        }
66
67
        return $data;
68
    }
69
70
}
71