Completed
Pull Request — master (#1596)
by Thomas
35:27
created

CheckProperties::check()   C

Complexity

Conditions 21
Paths 32

Size

Total Lines 76
Code Lines 48

Duplication

Lines 45
Ratio 59.21 %

Importance

Changes 0
Metric Value
dl 45
loc 76
rs 5.2051
c 0
b 0
f 0
cc 21
eloc 48
nc 32
nop 2

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
declare(strict_types = 1);
3
namespace Browscap\Data\Helper;
4
5
/**
6
 * Class DataCollection
7
 */
8
class CheckProperties
9
{
10
    /**
11
     * @param string $key
12
     * @param array  $properties
13
     *
14
     * @throws \LogicException
15
     *
16
     * @return void
17
     */
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':
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...
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':
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...
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:
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...
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