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

UseragentData::validate()   D

Complexity

Conditions 37
Paths 25

Size

Total Lines 159
Code Lines 93

Duplication

Lines 69
Ratio 43.4 %

Importance

Changes 0
Metric Value
dl 69
loc 159
rs 4.2495
c 0
b 0
f 0
cc 37
eloc 93
nc 25
nop 5

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\Validator;
4
5
use Browscap\Data\Helper\CheckDeviceData;
6
use Browscap\Data\Helper\CheckEngineData;
7
use Browscap\Data\Helper\CheckPlatformData;
8
9
/**
10
 * Class UseragentData
11
 *
12
 * @author     Thomas Müller <[email protected]>
13
 */
14
class UseragentData
15
{
16
    /**
17
     * @var \Browscap\Data\Helper\CheckDeviceData
18
     */
19
    private $checkDeviceData;
20
21
    /**
22
     * @var \Browscap\Data\Helper\CheckEngineData
23
     */
24
    private $checkEngineData;
25
26
    /**
27
     * @var \Browscap\Data\Helper\CheckPlatformData
28
     */
29
    private $checkPlatformData;
30
31
    public function __construct()
32
    {
33
        $this->checkDeviceData   = new CheckDeviceData();
34
        $this->checkEngineData   = new CheckEngineData();
35
        $this->checkPlatformData = new CheckPlatformData();
36
    }
37
38
    /**
39
     * @param array  $useragentData
40
     * @param array  $versions
41
     * @param array  $allDivisions
42
     * @param bool   $isCore
43
     * @param string $filename
44
     *
45
     * @throws \LogicException
46
     *
47
     * @return void
48
     */
49
    public function validate(
50
        array $useragentData,
51
        array $versions,
52
        array $allDivisions,
53
        bool $isCore,
54
        string $filename
55
    ) : void {
56
        if (!array_key_exists('userAgent', $useragentData)) {
57
            throw new \LogicException('Name for Division is missing in file "' . $filename . '"');
58
        }
59
60
        if (!is_string($useragentData['userAgent'])) {
61
            throw new \LogicException('Name of Division has to be a string in file "' . $filename . '"');
62
        }
63
64
        if (preg_match('/[\[\]]/', $useragentData['userAgent'])) {
65
            throw new \LogicException(
66
                'Name of Division "' . $useragentData['userAgent'] . '" includes invalid characters in file "' . $filename . '"'
67
            );
68
        }
69
70
        if (false === mb_strpos($useragentData['userAgent'], '#')
71
            && in_array($useragentData['userAgent'], $allDivisions)
72
        ) {
73
            throw new \LogicException('Division "' . $useragentData['userAgent'] . '" is defined twice in file "' . $filename . '"');
74
        }
75
76 View Code Duplication
        if ((false !== mb_strpos($useragentData['userAgent'], '#MAJORVER#')
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...
77
                || false !== mb_strpos($useragentData['userAgent'], '#MINORVER#'))
78
            && ['0.0'] === $versions
79
        ) {
80
            throw new \LogicException(
81
                'Division "' . $useragentData['userAgent']
82
                . '" is defined with version placeholders, but no versions are set in file "' . $filename . '"'
83
            );
84
        }
85
86 View Code Duplication
        if (false === mb_strpos($useragentData['userAgent'], '#MAJORVER#')
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...
87
            && false === mb_strpos($useragentData['userAgent'], '#MINORVER#')
88
            && ['0.0'] !== $versions
89
            && 1 < count($versions)
90
        ) {
91
            throw new \LogicException(
92
                'Division "' . $useragentData['userAgent']
93
                . '" is defined without version placeholders, but there are versions set in file "' . $filename . '"'
94
            );
95
        }
96
97
        if (!array_key_exists('properties', $useragentData)) {
98
            throw new \LogicException(
99
                'the properties entry is missing for key "' . $useragentData['userAgent'] . '" in file "' . $filename . '"'
100
            );
101
        }
102
103 View Code Duplication
        if (!is_array($useragentData['properties']) || empty($useragentData['properties'])) {
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...
104
            throw new \LogicException(
105
                'the properties entry has to be an non-empty array for key "' . $useragentData['userAgent'] . '" in file "' . $filename . '"'
106
            );
107
        }
108
109
        if (!$isCore && !isset($useragentData['properties']['Parent'])) {
110
            throw new \LogicException(
111
                'the "Parent" property is missing for key "' . $useragentData['userAgent'] . '" in file "' . $filename . '"'
112
            );
113
        }
114
115
        if (!$isCore && 'DefaultProperties' !== $useragentData['properties']['Parent']) {
116
            throw new \LogicException(
117
                'the "Parent" property is not linked to the "DefaultProperties" for key "'
118
                . $useragentData['userAgent'] . '" in file "' . $filename . '"'
119
            );
120
        }
121
122 View Code Duplication
        if (!array_key_exists('Comment', $useragentData['properties'])) {
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...
123
            throw new \LogicException(
124
                'the "Comment" property is missing for key "' . $useragentData['userAgent'] . '" in file "' . $filename . '"'
125
            );
126
        }
127
128 View Code Duplication
        if (!is_string($useragentData['properties']['Comment'])) {
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...
129
            throw new \LogicException(
130
                'the "Comment" property has to be a string for key "' . $useragentData['userAgent'] . '" in file "' . $filename . '"'
131
            );
132
        }
133
134 View Code Duplication
        if (!array_key_exists('Version', $useragentData['properties']) && ['0.0'] !== $versions) {
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...
135
            throw new \LogicException(
136
                'the "Version" property is missing for key "' . $useragentData['userAgent'] . '" in file "' . $filename
137
                . '", but there are defined versions'
138
            );
139
        }
140
141
        if (!$isCore) {
142
            if (array_key_exists('Version', $useragentData['properties'])) {
143 View Code Duplication
                if (!is_string($useragentData['properties']['Version'])) {
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...
144
                    throw new \LogicException(
145
                        'the "Version" property has to be a string for key "' . $useragentData['userAgent'] . '" in file "' . $filename
146
                        . '"'
147
                    );
148
                }
149
150 View Code Duplication
                if ((false !== mb_strpos($useragentData['properties']['Version'], '#MAJORVER#')
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...
151
                        || false !== mb_strpos($useragentData['properties']['Version'], '#MINORVER#'))
152
                    && ['0.0'] === $versions) {
153
                    throw new \LogicException(
154
                        'the "Version" property has version placeholders for key "' . $useragentData['userAgent'] . '" in file "' . $filename
155
                        . '", but no versions are defined'
156
                    );
157
                }
158
159 View Code Duplication
                if (false === mb_strpos($useragentData['properties']['Version'], '#MAJORVER#')
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...
160
                    && false === mb_strpos($useragentData['properties']['Version'], '#MINORVER#')
161
                    && ['0.0'] !== $versions
162
                    && 1 < count($versions)
163
                ) {
164
                    throw new \LogicException(
165
                        'the "Version" property has no version placeholders for key "' . $useragentData['userAgent'] . '" in file "' . $filename
166
                        . '", but versions are defined'
167
                    );
168
                }
169
            }
170
171
            if (!array_key_exists('children', $useragentData)) {
172
                throw new \LogicException(
173
                    'the children property is missing for key "' . $useragentData['userAgent'] . '" in file "' . $filename . '"'
174
                );
175
            }
176
177
            if (!is_array($useragentData['children'])) {
178
                throw new \LogicException(
179
                    'the children property has to be an array for key "' . $useragentData['userAgent'] . '" in file "' . $filename . '"'
180
                );
181
            }
182
183 View Code Duplication
            if (array_key_exists('match', $useragentData['children'])) {
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...
184
                throw new \LogicException(
185
                    'the children property shall not have the "match" entry for key "' . $useragentData['userAgent'] . '" in file "' . $filename . '"'
186
                );
187
            }
188
189
            $this->checkPlatformData->check(
190
                $useragentData['properties'],
191
                'the properties array contains platform data for key "' . $useragentData['userAgent']
192
                . '", please use the "platform" keyword'
193
            );
194
195
            $this->checkEngineData->check(
196
                $useragentData['properties'],
197
                'the properties array contains engine data for key "' . $useragentData['userAgent']
198
                . '", please use the "engine" keyword'
199
            );
200
201
            $this->checkDeviceData->check(
202
                $useragentData['properties'],
203
                'the properties array contains device data for key "' . $useragentData['userAgent']
204
                . '", please use the "device" keyword'
205
            );
206
        }
207
    }
208
}
209