Conditions | 21 |
Paths | 32 |
Total Lines | 76 |
Code Lines | 48 |
Lines | 45 |
Ratio | 59.21 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
18 | public function check($key, array $properties) : void |
||
19 | { |
||
20 | if (!array_key_exists('Version', $properties)) { |
||
21 | throw new \LogicException('Version property not found for key "' . $key . '"'); |
||
22 | } |
||
23 | |||
24 | if (!array_key_exists('Parent', $properties) && !in_array($key, ['DefaultProperties', '*'])) { |
||
25 | throw new \LogicException('Parent property is missing for key "' . $key . '"'); |
||
26 | } |
||
27 | |||
28 | if (!array_key_exists('Device_Type', $properties)) { |
||
29 | throw new \LogicException('property "Device_Type" is missing for key "' . $key . '"'); |
||
30 | } |
||
31 | |||
32 | if (!array_key_exists('isTablet', $properties)) { |
||
33 | throw new \LogicException('property "isTablet" is missing for key "' . $key . '"'); |
||
34 | } |
||
35 | |||
36 | if (!array_key_exists('isMobileDevice', $properties)) { |
||
37 | throw new \LogicException('property "isMobileDevice" is missing for key "' . $key . '"'); |
||
38 | } |
||
39 | |||
40 | switch ($properties['Device_Type']) { |
||
41 | View Code Duplication | case 'Tablet': |
|
|
|||
42 | if (true !== $properties['isTablet']) { |
||
43 | throw new \LogicException( |
||
44 | 'the device of type "' . $properties['Device_Type'] . '" is NOT marked as Tablet for key "' |
||
45 | . $key . '"' |
||
46 | ); |
||
47 | } |
||
48 | if (true !== $properties['isMobileDevice']) { |
||
49 | throw new \LogicException( |
||
50 | 'the device of type "' . $properties['Device_Type'] |
||
51 | . '" is NOT marked as Mobile Device for key "' . $key . '"' |
||
52 | ); |
||
53 | } |
||
54 | |||
55 | break; |
||
56 | case 'Mobile Phone': |
||
57 | case 'Mobile Device': |
||
58 | case 'Ebook Reader': |
||
59 | case 'Console': |
||
60 | View Code Duplication | case 'Digital Camera': |
|
61 | if (true === $properties['isTablet']) { |
||
62 | throw new \LogicException( |
||
63 | 'the device of type "' . $properties['Device_Type'] . '" is marked as Tablet for key "' |
||
64 | . $key . '"' |
||
65 | ); |
||
66 | } |
||
67 | if (true !== $properties['isMobileDevice']) { |
||
68 | throw new \LogicException( |
||
69 | 'the device of type "' . $properties['Device_Type'] |
||
70 | . '" is NOT marked as Mobile Device for key "' . $key . '"' |
||
71 | ); |
||
72 | } |
||
73 | |||
74 | break; |
||
75 | case 'TV Device': |
||
76 | case 'Desktop': |
||
77 | View Code Duplication | default: |
|
78 | if (true === $properties['isTablet']) { |
||
79 | throw new \LogicException( |
||
80 | 'the device of type "' . $properties['Device_Type'] . '" is marked as Tablet for key "' |
||
81 | . $key . '"' |
||
82 | ); |
||
83 | } |
||
84 | if (true === $properties['isMobileDevice']) { |
||
85 | throw new \LogicException( |
||
86 | 'the device of type "' . $properties['Device_Type'] . '" is marked as Mobile Device for key "' |
||
87 | . $key . '"' |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | break; |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 |
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.