Completed
Pull Request — master (#186)
by Jay
05:38
created

PropertyHolder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 86.49%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 0
dl 0
loc 199
ccs 32
cts 37
cp 0.8649
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getPropertyType() 0 100 6
C checkValueInArray() 0 72 7
1
<?php
2
/**
3
 * Copyright (c) 1998-2015 Browser Capabilities Project.
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included
13
 * in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category   Browscap-PHP
24
 *
25
 * @copyright  1998-2015 Browser Capabilities Project
26
 * @license    http://www.opensource.org/licenses/MIT MIT License
27
 *
28
 * @link       https://github.com/browscap/browscap-php/
29
 * @since      added with version 3.0
30
 */
31
32
namespace BrowscapPHP\Data;
33
34
/**
35
 * Class PropertyHolder.
36
 *
37
 * @category   Browscap
38
 *
39
 * @author     Thomas Müller <[email protected]>
40
 */
41
class PropertyHolder
42
{
43
    const TYPE_STRING   = 'string';
44
    const TYPE_GENERIC  = 'generic';
45
    const TYPE_NUMBER   = 'number';
46
    const TYPE_BOOLEAN  = 'boolean';
47
    const TYPE_IN_ARRAY = 'in_array';
48
49
    /**
50
     * Get the type of a property.
51
     *
52
     * @param string $propertyName
53
     *
54
     * @throws \Exception
55
     *
56
     * @return string
57
     */
58 6
    public function getPropertyType($propertyName)
59
    {
60
        $stringProperties = [
61 6
            'Comment' => 1,
62
            'Browser' => 1,
63
            'Browser_Maker' => 1,
64
            'Browser_Modus' => 1,
65
            'Platform' => 1,
66
            'Platform_Name' => 1,
67
            'Platform_Description' => 1,
68
            'Device_Name' => 1,
69
            'Platform_Maker' => 1,
70
            'Device_Code_Name' => 1,
71
            'Device_Maker' => 1,
72
            'Device_Brand_Name' => 1,
73
            'RenderingEngine_Name' => 1,
74
            'RenderingEngine_Description' => 1,
75
            'RenderingEngine_Maker' => 1,
76
            'Parent' => 1,
77
            'PropertyName' => 1,
78
            'CDF' => 1,
79
            'PatternId' => 1,
80
        ];
81
82 6
        if (array_key_exists($propertyName, $stringProperties)) {
83 6
            return self::TYPE_STRING;
84
        }
85
86
        $arrayProperties = [
87 6
            'Browser_Type' => 1,
88
            'Device_Type' => 1,
89
            'Device_Pointing_Method' => 1,
90
            'Browser_Bits' => 1,
91
            'Platform_Bits' => 1,
92
        ];
93
94 6
        if (array_key_exists($propertyName, $arrayProperties)) {
95 1
            return self::TYPE_IN_ARRAY;
96
        }
97
98
        $genericProperties = [
99 6
            'Platform_Version' => 1,
100
            'RenderingEngine_Version' => 1,
101
            'Released' => 1,
102
            'Format' => 1,
103
            'Type' => 1,
104
        ];
105
106 6
        if (array_key_exists($propertyName, $genericProperties)) {
107 6
            return self::TYPE_GENERIC;
108
        }
109
110
        $numericProperties = [
111 6
            'Version' => 1,
112
            'CssVersion' => 1,
113
            'AolVersion' => 1,
114
            'MajorVer' => 1,
115
            'MinorVer' => 1,
116
            'aolVersion' => 1,
117
        ];
118
119 6
        if (array_key_exists($propertyName, $numericProperties)) {
120 6
            return self::TYPE_NUMBER;
121
        }
122
123
        $booleanProperties = [
124 6
            'Alpha' => 1,
125
            'Beta' => 1,
126
            'Win16' => 1,
127
            'Win32' => 1,
128
            'Win64' => 1,
129
            'Frames' => 1,
130
            'IFrames' => 1,
131
            'Tables' => 1,
132
            'Cookies' => 1,
133
            'BackgroundSounds' => 1,
134
            'JavaScript' => 1,
135
            'VBScript' => 1,
136
            'JavaApplets' => 1,
137
            'ActiveXControls' => 1,
138
            'isMobileDevice' => 1,
139
            'isTablet' => 1,
140
            'isSyndicationReader' => 1,
141
            'Crawler' => 1,
142
            'MasterParent' => 1,
143
            'LiteMode' => 1,
144
            'isFake' => 1,
145
            'isAnonymized' => 1,
146
            'isModified' => 1,
147
            'isBanned' => 1,
148
            'supportsCSS' => 1,
149
            'AOL' => 1,
150
        ];
151
152 6
        if (array_key_exists($propertyName, $booleanProperties)) {
153 6
            return self::TYPE_BOOLEAN;
154
        }
155
156
        throw new \InvalidArgumentException("Property {$propertyName} did not have a defined property type");
157
    }
158
159
    /**
160
     * @param string $property
161
     * @param string $value
162
     *
163
     * @throws \InvalidArgumentException
164
     *
165
     * @return string
166
     */
167 1
    public function checkValueInArray($property, $value)
168
    {
169
        switch ($property) {
170 1
            case 'Browser_Type':
171
                $allowedValues = [
172 1
                    'Useragent Anonymizer' => 1,
173
                    'Browser' => 1,
174
                    'Offline Browser' => 1,
175
                    'Multimedia Player' => 1,
176
                    'Library' => 1,
177
                    'Feed Reader' => 1,
178
                    'Email Client' => 1,
179
                    'Bot/Crawler' => 1,
180
                    'Application' => 1,
181
                    'Tool' => 1,
182
                    'unknown' => 1,
183
                ];
184 1
                break;
185 1
            case 'Device_Type':
186
                $allowedValues = [
187 1
                    'Console' => 1,
188
                    'TV Device' => 1,
189
                    'Tablet' => 1,
190
                    'Mobile Phone' => 1,
191
                    'Smartphone' => 1,    // actual mobile phone with touchscreen
192
                    'Feature Phone' => 1, // older mobile phone
193
                    'Mobile Device' => 1,
194
                    'FonePad' => 1,       // Tablet sized device with the capability to make phone calls
195
                    'Desktop' => 1,
196
                    'Ebook Reader' => 1,
197
                    'Car Entertainment System' => 1,
198
                    'Digital Camera' => 1,
199
                    'unknown' => 1,
200
                ];
201 1
                break;
202 1
            case 'Device_Pointing_Method':
203
                // This property is taken from http://www.scientiamobile.com/wurflCapability
204
                $allowedValues = [
205 1
                    'joystick' => 1,
206
                    'stylus' => 1,
207
                    'touchscreen' => 1,
208
                    'clickwheel' => 1,
209
                    'trackpad' => 1,
210
                    'trackball' => 1,
211
                    'mouse' => 1,
212
                    'unknown' => 1,
213
                ];
214 1
                break;
215 1
            case 'Browser_Bits':
216 1
            case 'Platform_Bits':
217
                $allowedValues = [
218 1
                    '0' => 1,
219
                    '8' => 1,
220
                    '16' => 1,
221
                    '32' => 1,
222
                    '64' => 1,
223
                ];
224 1
                break;
225
            default:
226
                throw new \InvalidArgumentException('Property "' . $property . '" is not defined to be validated');
227
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
228
        }
229
230 1
        if (array_key_exists($value, $allowedValues)) {
231 1
            return $value;
232
        }
233
234
        throw new \InvalidArgumentException(
235
            'invalid value given for Property "' . $property . '": given value "' . (string) $value . '", allowed: '
236
            . json_encode($allowedValues)
237
        );
238
    }
239
}
240