Completed
Push — master ( 9604dc...5404e5 )
by James
03:26
created

PropertyHolder::isOutputProperty()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 74
Code Lines 61

Duplication

Lines 7
Ratio 9.46 %

Code Coverage

Tests 10
CRAP Score 8.2964

Importance

Changes 0
Metric Value
dl 7
loc 74
ccs 10
cts 12
cp 0.8333
rs 6.2894
c 0
b 0
f 0
cc 8
eloc 61
nc 8
nop 2
crap 8.2964

How to fix   Long Method   

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
 * Copyright (c) 1998-2014 Browser Capabilities Project
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as
7
 * published by the Free Software Foundation, either version 3 of the
8
 * License, or (at your option) any later version.
9
 *
10
 * Refer to the LICENSE file distributed with this package.
11
 *
12
 * @category   Browscap
13
 * @copyright  1998-2014 Browser Capabilities Project
14
 * @license    MIT
15
 */
16
17
namespace Browscap\Data;
18
19
use Browscap\Writer\WriterInterface;
20
21
/**
22
 * Class PropertyHolder
23
 *
24
 * @category   Browscap
25
 * @author     Thomas Müller <[email protected]>
26
 */
27
class PropertyHolder
28
{
29
    const TYPE_STRING   = 'string';
30
    const TYPE_GENERIC  = 'generic';
31
    const TYPE_NUMBER   = 'number';
32
    const TYPE_BOOLEAN  = 'boolean';
33
    const TYPE_IN_ARRAY = 'in_array';
34
35
    /**
36
     * Get the type of a property
37
     *
38
     * @param  string     $propertyName
39
     * @throws \Exception
40
     * @return string
41
     */
42 183
    public function getPropertyType($propertyName)
43
    {
44
        $stringProperties = [
45 183
            'Comment' => 1,
46
            'Browser' => 1,
47
            'Browser_Maker' => 1,
48
            'Browser_Modus' => 1,
49
            'Platform' => 1,
50
            'Platform_Name' => 1,
51
            'Platform_Description' => 1,
52
            'Device_Name' => 1,
53
            'Platform_Maker' => 1,
54
            'Device_Code_Name' => 1,
55
            'Device_Maker' => 1,
56
            'Device_Brand_Name' => 1,
57
            'RenderingEngine_Name' => 1,
58
            'RenderingEngine_Description' => 1,
59
            'RenderingEngine_Maker' => 1,
60
            'Parent' => 1,
61
            'PropertyName' => 1,
62
            'PatternId' => 1,
63
        ];
64
65 183
        if (isset($stringProperties[$propertyName])) {
66 59
            return self::TYPE_STRING;
67
        }
68
69
        $arrayProperties = [
70 124
            'Browser_Type' => 1,
71
            'Device_Type' => 1,
72
            'Device_Pointing_Method' => 1,
73
            'Browser_Bits' => 1,
74
            'Platform_Bits' => 1,
75
        ];
76
77 124
        if (isset($arrayProperties[$propertyName])) {
78 18
            return self::TYPE_IN_ARRAY;
79
        }
80
81
        $genericProperties = [
82 106
            'Platform_Version' => 1,
83
            'RenderingEngine_Version' => 1,
84
        ];
85
86 106
        if (isset($genericProperties[$propertyName])) {
87 12
            return self::TYPE_GENERIC;
88
        }
89
90
        $numericProperties = [
91 94
            'Version' => 1,
92
            'CssVersion' => 1,
93
            'AolVersion' => 1,
94
            'MajorVer' => 1,
95
            'MinorVer' => 1,
96
        ];
97
98 94
        if (isset($numericProperties[$propertyName])) {
99 30
            return self::TYPE_NUMBER;
100
        }
101
102
        $booleanProperties = [
103 64
            'Alpha' => 1,
104
            'Beta' => 1,
105
            'Win16' => 1,
106
            'Win32' => 1,
107
            'Win64' => 1,
108
            'Frames' => 1,
109
            'IFrames' => 1,
110
            'Tables' => 1,
111
            'Cookies' => 1,
112
            'BackgroundSounds' => 1,
113
            'JavaScript' => 1,
114
            'VBScript' => 1,
115
            'JavaApplets' => 1,
116
            'ActiveXControls' => 1,
117
            'isMobileDevice' => 1,
118
            'isTablet' => 1,
119
            'isSyndicationReader' => 1,
120
            'Crawler' => 1,
121
            'MasterParent' => 1,
122
            'LiteMode' => 1,
123
            'isFake' => 1,
124
            'isAnonymized' => 1,
125
            'isModified' => 1,
126
        ];
127
128 64
        if (isset($booleanProperties[$propertyName])) {
129 63
            return self::TYPE_BOOLEAN;
130
        }
131
132 1
        throw new \InvalidArgumentException("Property {$propertyName} did not have a defined property type");
133
    }
134
135
    /**
136
     * Determine if the specified property is an property that should
137
     * be included in the "full" versions of the files only
138
     *
139
     * @param  string $propertyName
140
     * @return bool
141
     */
142 117
    public function isLiteModeProperty($propertyName)
143
    {
144
        $outputProperties = [
145 117
            'Parent' => 1,
146
            'Comment' => 1,
147
            'Browser' => 1,
148
            'Platform' => 1,
149
            'Version' => 1,
150
            'isMobileDevice' => 1,
151
            'isTablet' => 1,
152
            'Device_Type' => 1,
153
        ];
154
155 117
        return isset($outputProperties[$propertyName]);
156
    }
157
158
    /**
159
     * Determine if the specified property is an property that should
160
     * be included in the "full" versions of the files only
161
     *
162
     * @param  string                           $propertyName
163
     * @param  \Browscap\Writer\WriterInterface $writer
164
     * @return bool
165
     */
166 72
    public function isStandardModeProperty($propertyName, WriterInterface $writer = null)
167
    {
168
        $outputProperties = [
169 72
            'MajorVer' => 1,
170
            'MinorVer' => 1,
171
            'Crawler' => 1,
172
            'Browser_Maker' => 1,
173
            'Device_Pointing_Method' => 1,
174
        ];
175
176 72
        if (isset($outputProperties[$propertyName])) {
177 10
            return true;
178
        }
179
180 62 View Code Duplication
        if (null !== $writer && in_array($writer->getType(), ['csv', 'xml'])) {
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...
181 1
            $additionalProperties = ['PropertyName', 'MasterParent', 'LiteMode'];
182
183 1
            if (in_array($propertyName, $additionalProperties)) {
184 1
                return true;
185
            }
186
        }
187
188 61
        return false;
189
    }
190
191
    /**
192
     * Determine if the specified property is an "extra" property (that should
193
     * be included in the "full" versions of the files)
194
     *
195
     * @param  string                           $propertyName
196
     * @param  \Browscap\Writer\WriterInterface $writer
197
     * @return bool
198
     */
199 214
    public function isOutputProperty($propertyName, WriterInterface $writer = null)
200
    {
201
        $outputProperties = [
202 214
            'Comment' => 1,
203
            'Browser' => 1,
204
            'Browser_Maker' => 1,
205
            'Browser_Modus' => 1,
206
            'Platform' => 1,
207
            'Platform_Name' => 1,
208
            'Platform_Description' => 1,
209
            'Device_Name' => 1,
210
            'Platform_Maker' => 1,
211
            'Device_Code_Name' => 1,
212
            'Device_Maker' => 1,
213
            'Device_Brand_Name' => 1,
214
            'RenderingEngine_Name' => 1,
215
            'RenderingEngine_Description' => 1,
216
            'RenderingEngine_Maker' => 1,
217
            'Parent' => 1,
218
            'Browser_Type' => 1,
219
            'Device_Type' => 1,
220
            'Device_Pointing_Method' => 1,
221
            'Browser_Bits' => 1,
222
            'Platform_Bits' => 1,
223
            'Platform_Version' => 1,
224
            'RenderingEngine_Version' => 1,
225
            'Version' => 1,
226
            'CssVersion' => 1,
227
            'AolVersion' => 1,
228
            'MajorVer' => 1,
229
            'MinorVer' => 1,
230
            'Alpha' => 1,
231
            'Beta' => 1,
232
            'Win16' => 1,
233
            'Win32' => 1,
234
            'Win64' => 1,
235
            'Frames' => 1,
236
            'IFrames' => 1,
237
            'Tables' => 1,
238
            'Cookies' => 1,
239
            'BackgroundSounds' => 1,
240
            'JavaScript' => 1,
241
            'VBScript' => 1,
242
            'JavaApplets' => 1,
243
            'ActiveXControls' => 1,
244
            'isMobileDevice' => 1,
245
            'isTablet' => 1,
246
            'isSyndicationReader' => 1,
247
            'Crawler' => 1,
248
            'isFake' => 1,
249
            'isAnonymized' => 1,
250
            'isModified' => 1,
251
        ];
252
253 214
        if (isset($outputProperties[$propertyName])) {
254 193
            return true;
255
        }
256
257 21 View Code Duplication
        if (null !== $writer && in_array($writer->getType(), ['csv', 'xml'])) {
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...
258 1
            $additionalProperties = ['PropertyName', 'MasterParent', 'LiteMode'];
259
260 1
            if (in_array($propertyName, $additionalProperties)) {
261 1
                return true;
262
            }
263
        }
264
265 20
        if (null !== $writer && $writer->getType() === 'ini') {
266
            if ($propertyName === 'PatternId') {
267
                return true;
268
            }
269
        }
270
271 20
        return false;
272
    }
273
274
    /**
275
     * @param string $property
276
     * @param string $value
277
     *
278
     * @throws \InvalidArgumentException
279
     * @return string
280
     */
281 22
    public function checkValueInArray($property, $value)
282
    {
283
        switch ($property) {
284 22
            case 'Browser_Type':
285
                $allowedValues = [
286 7
                    'Useragent Anonymizer',
287
                    'Browser',
288
                    'Offline Browser',
289
                    'Multimedia Player',
290
                    'Library',
291
                    'Feed Reader',
292
                    'Email Client',
293
                    'Bot/Crawler',
294
                    'Application',
295
                    'Tool',
296
                    'unknown',
297
                ];
298 7
                break;
299 15
            case 'Device_Type':
300
                $allowedValues = [
301 6
                    'Console',
302
                    'TV Device',
303
                    'Tablet',
304
                    'Mobile Phone',
305
                    'Mobile Device',
306
                    'Desktop',
307
                    'Ebook Reader',
308
                    'Car Entertainment System',
309
                    'Digital Camera',
310
                    'unknown',
311
                ];
312 6
                break;
313 9
            case 'Device_Pointing_Method':
314
                // This property is taken from http://www.scientiamobile.com/wurflCapability
315
                $allowedValues = [
316 6
                    'joystick', 'stylus', 'touchscreen', 'clickwheel', 'trackpad', 'trackball', 'mouse', 'unknown',
317
                ];
318 6
                break;
319 3
            case 'Browser_Bits':
320 2
            case 'Platform_Bits':
321
                $allowedValues = [
322 2
                    '0', '8', '16', '32', '64',
323
                ];
324 2
                break;
325
            default:
326 1
                throw new \InvalidArgumentException('Property "' . $property . '" is not defined to be validated');
327
        }
328
329 21
        if (in_array($value, $allowedValues)) {
330 16
            return $value;
331
        }
332
333 5
        throw new \InvalidArgumentException(
334 5
            'invalid value given for Property "' . $property . '": given value "' . (string) $value . '", allowed: '
335 5
            . json_encode($allowedValues)
336
        );
337
    }
338
}
339