Passed
Push — master ( a59b2c...523153 )
by Cyril
02:17
created

ObjectToPreview   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 230
Duplicated Lines 9.13 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 55
c 6
b 0
f 0
lcom 1
cbo 1
dl 21
loc 230
ccs 0
cts 178
cp 0
rs 6.8

5 Methods

Rating   Name   Duplication   Size   Complexity  
B transform() 0 30 4
A getItems() 8 8 3
B getCategories() 0 13 6
D categoryValue() 0 106 34
C set() 10 25 8

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ObjectToPreview often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ObjectToPreview, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace ApaiIO\ResponseTransformer;
3
4
class ObjectToPreview extends ObjectToArray implements ResponseTransformerInterface
5
{
6
    /**
7
     *
8
     * @var array
9
     */
10
    protected $data = [];
11
12
    /**
13
     *
14
     * @var array
15
     */
16
    protected $items = [];
17
18
    /**
19
     *
20
     * @var array
21
     */
22
    protected $categories = [];
23
24
    /**
25
     *
26
     * @param string $response
27
     * @return array
28
     */
29
    public function transform($response)
30
    {
31
        $response = parent::transform($response);
32
33
        if (!$this->getItems($response)) {
34
            return [];
35
        }
36
37
        $this->getCategories($response);
38
39
        $c = count($this->items);
40
        for ($i = 0; $i < $c; $i++) {
41
            $this->set($i, 'asin', 'ASIN');
42
            if (!$this->set($i, 'category', 'BrowseNodes', 'BrowseNode', 'Name')) {
43
                $this->set($i, 'category', 'BrowseNodes', 'BrowseNode', 0, 'Name');
44
            }
45
            $this->set($i, 'sales_rank', 'SalesRank');
46
            $this->set($i, 'title', 'ItemAttributes', 'Title');
47
            $this->set($i, 'url', 'DetailPageURL');
48
            $this->set($i, 'manufacturer', 'ItemAttributes', 'Manufacturer');
49
            $this->set($i, 'large_image', 'LargeImage', 'URL');
50
            $this->set($i, 'medium_image', 'MediumImage', 'URL');
51
            $this->set($i, 'small_image', 'SmallImage', 'URL');
52
        }
53
54
        return [
55
            'items' => $this->data,
56
            'categories' => $this->categories
57
        ];
58
    }
59
60
    /**
61
     *
62
     * @param type $response
63
     * @return mixed
64
     */
65 View Code Duplication
    protected function getItems($response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
66
    {
67
        if (isset($response['Items']['Item']) AND is_array($response['Items']['Item'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
68
            return $this->items = $response['Items']['Item'];
69
        } else {
70
            return false;
71
        }
72
    }
73
74
    /**
75
     *
76
     * @param array $response
77
     */
78
    protected function getCategories($response)
79
    {
80
        if (isset($response['Items']['SearchBinSets']['SearchBinSet']['Bin'])) {
81
            foreach ($response['Items']['SearchBinSets']['SearchBinSet']['Bin'] as $bin) {
82
                if (isset($bin['BinParameter']['Name']) && $bin['BinParameter']['Name'] == 'SearchIndex') {
83
                    $this->categories[] = $this->categoryValue($bin['BinParameter']['Value']);
84
                }
85
                if (count($this->categories) >= 10) {
86
                    return;
87
                }
88
            }
89
        }
90
    }
91
92
    protected function categoryValue($value)
93
    {
94
        switch ($value) {
95
            case 'All':
96
                return ['key' => $value, 'value' => 'All Departments'];
97
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
98
            case 'ArtsAndCrafts':
99
                return ['key' => $value, 'value' => 'Arts, Crafts & Sewing'];
100
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
101
            case 'Collectibles':
102
                return ['key' => $value, 'value' => 'Collectibles & Fine Arts'];
103
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
104
            case 'Fashion':
105
                return ['key' => $value, 'value' => 'Clothing, Shoes & Jewelry'];
106
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
107
            case 'FashionBaby':
108
                return ['key' => $value, 'value' => 'Clothing, Shoes & Jewelry - Baby'];
109
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
110
            case 'FashionBoys':
111
                return ['key' => $value, 'value' => 'Clothing, Shoes & Jewelry - Boys'];
112
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
113
            case 'FashionGirls':
114
                return ['key' => $value, 'value' => 'Clothing, Shoes & Jewelry - Girls'];
115
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
116
            case 'FashionMen':
117
                return ['key' => $value, 'value' => 'Clothing, Shoes & Jewelry - Men'];
118
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
119
            case 'FashionWomen':
120
                return ['key' => $value, 'value' => 'Clothing, Shoes & Jewelry - Women'];
121
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
122
            case 'GiftCards':
123
                return ['key' => $value, 'value' => 'Gift Cards'];
124
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
125
            case 'Grocery':
126
                return ['key' => $value, 'value' => 'Grocery & Gourmet Food'];
127
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
128
            case 'HealthPersonalCare':
129
                return ['key' => $value, 'value' => 'Health & Personal Care'];
130
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
131
            case 'HomeGarden':
132
                return ['key' => $value, 'value' => 'Home & Kitchen'];
133
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
134
            case 'Industrial':
135
                return ['key' => $value, 'value' => 'Industrial & Scientific'];
136
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
137
            case 'KindleStore':
138
                return ['key' => $value, 'value' => 'Kindle Store'];
139
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
140
            case 'LawnAndGarden':
141
                return ['key' => $value, 'value' => 'Patio, Lawn & Garden'];
142
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
143
            case 'Luggage':
144
                return ['key' => $value, 'value' => 'Luggage & Travel Gear'];
145
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
146
            case 'Magazines':
147
                return ['key' => $value, 'value' => 'Magazine Subscriptions'];
148
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
149
            case 'MobileApps':
150
                return ['key' => $value, 'value' => 'Apps & Games'];
151
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
152
            case 'Movies':
153
                return ['key' => $value, 'value' => 'Movies & TV'];
154
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
155
            case 'MP3Downloads':
156
                return ['key' => $value, 'value' => 'Digital Music'];
157
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
158
            case 'Music':
159
                return ['key' => $value, 'value' => 'CDs & Vinyl'];
160
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
161
            case 'MusicalInstruments':
162
                return ['key' => $value, 'value' => 'Musical Instruments'];
163
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
164
            case 'OfficeProducts':
165
                return ['key' => $value, 'value' => 'Office Products'];
166
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
167
            case 'Pantry':
168
                return ['key' => $value, 'value' => 'Prime Pantry'];
169
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
170
            case 'PCHardware':
171
                return ['key' => $value, 'value' => 'Computers'];
172
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
173
            case 'PetSupplies':
174
                return ['key' => $value, 'value' => 'Pet Supplies'];
175
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
176
            case 'SportingGoods':
177
                return ['key' => $value, 'value' => 'Sports & Outdoors'];
178
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
179
            case 'Tools':
180
                return ['key' => $value, 'value' => 'Tools & Home Improvement'];
181
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
182
            case 'Toys':
183
                return ['key' => $value, 'value' => 'Toys & Games'];
184
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
185
            case 'UnboxVideo':
186
                return ['key' => $value, 'value' => 'Amazon Instant Video'];
187
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
188
            case 'VideoGames':
189
                return ['key' => $value, 'value' => 'Video Games'];
190
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
191
            case 'Wireless':
192
                return ['key' => $value, 'value' => 'Cell Phones & Accessories'];
193
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
194
            default:
195
                return ['key' => $value, 'value' => $value];
196
        }
197
    }
198
199
    /**
200
     *
201
     * @param int $i
202
     * @param string $data
203
     * @param string $key1
204
     * @param string $key2
205
     * @param string $key3
206
     * @param string $key4
207
     */
208
    protected function set($i, $data, $key1, $key2 = null, $key3 = null, $key4 = null)
209
    {
210
        if ($key4 !== null) {
211 View Code Duplication
            if (isset($this->items[$i][$key1][$key2][$key3][$key4])) {
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...
212
                $this->data[$i][$data] = $this->items[$i][$key1][$key2][$key3][$key4];
213
                return true;
214
            }
215 View Code Duplication
        } elseif ($key3 !== null) {
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...
216
            if (isset($this->items[$i][$key1][$key2][$key3])) {
217
                $this->data[$i][$data] = $this->items[$i][$key1][$key2][$key3];
218
                return true;
219
            }
220
        } elseif ($key2 !== null) {
221
            if (isset($this->items[$i][$key1][$key2])) {
222
                $this->data[$i][$data] = $this->items[$i][$key1][$key2];
223
                return true;
224
            }
225
        } else {
226
            if (isset($this->items[$i][$key1])) {
227
                $this->data[$i][$data] = $this->items[$i][$key1];
228
                return true;
229
            }
230
        }
231
        return false;
232
    }
233
}
234