Completed
Pull Request — master (#181)
by Jay
19:19 queued 09:34
created

PropertyHolder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 94.7%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 0
dl 0
loc 194
ccs 125
cts 132
cp 0.947
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getPropertyType() 0 95 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' => true,
62 6
            'Browser' => true,
63 6
            'Browser_Maker' => true,
64 6
            'Browser_Modus' => true,
65 6
            'Platform' => true,
66 6
            'Platform_Name' => true,
67 6
            'Platform_Description' => true,
68 6
            'Device_Name' => true,
69 6
            'Platform_Maker' => true,
70 6
            'Device_Code_Name' => true,
71 6
            'Device_Maker' => true,
72 6
            'Device_Brand_Name' => true,
73 6
            'RenderingEngine_Name' => true,
74 6
            'RenderingEngine_Description' => true,
75 6
            'RenderingEngine_Maker' => true,
76 6
            'Parent' => true,
77 6
            'PropertyName' => true,
78 6
            'CDF' => true,
79 6
        ];
80
81 6
        if (isset($stringProperties[$propertyName])) {
82 6
            return self::TYPE_STRING;
83
        }
84
85
        $arrayProperties = [
86 6
            'Browser_Type' => true,
87 6
            'Device_Type' => true,
88 6
            'Device_Pointing_Method' => true,
89 6
            'Browser_Bits' => true,
90 6
            'Platform_Bits' => true,
91 6
        ];
92
93 6
        if (isset($arrayProperties[$propertyName])) {
94 1
            return self::TYPE_IN_ARRAY;
95
        }
96
97
        $genericProperties = [
98 6
            'Platform_Version' => true,
99 6
            'RenderingEngine_Version' => true,
100 6
            'Released' => true,
101 6
            'Format' => true,
102 6
            'Type' => true,
103 6
        ];
104
105 6
        if (isset($genericProperties[$propertyName])) {
106 6
            return self::TYPE_GENERIC;
107
        }
108
109
        $numericProperties = [
110 6
            'Version' => true,
111 6
            'CssVersion' => true,
112 6
            'AolVersion' => true,
113 6
            'MajorVer' => true,
114 6
            'MinorVer' => true,
115 6
        ];
116
117 6
        if (isset($numericProperties[$propertyName])) {
118 6
            return self::TYPE_NUMBER;
119
        }
120
121
        $booleanProperties = [
122 6
            'Alpha' => true,
123 6
            'Beta' => true,
124 6
            'Win16' => true,
125 6
            'Win32' => true,
126 6
            'Win64' => true,
127 6
            'Frames' => true,
128 6
            'IFrames' => true,
129 6
            'Tables' => true,
130 6
            'Cookies' => true,
131 6
            'BackgroundSounds' => true,
132 6
            'JavaScript' => true,
133 6
            'VBScript' => true,
134 6
            'JavaApplets' => true,
135 6
            'ActiveXControls' => true,
136 6
            'isMobileDevice' => true,
137 6
            'isTablet' => true,
138 6
            'isSyndicationReader' => true,
139 6
            'Crawler' => true,
140 6
            'MasterParent' => true,
141 6
            'LiteMode' => true,
142 6
            'isFake' => true,
143 6
            'isAnonymized' => true,
144 6
            'isModified' => true,
145 6
        ];
146
147 6
        if (isset($booleanProperties[$propertyName])) {
148 6
            return self::TYPE_BOOLEAN;
149
        }
150
151
        throw new \InvalidArgumentException("Property {$propertyName} did not have a defined property type");
152
    }
153
154
    /**
155
     * @param string $property
156
     * @param string $value
157
     *
158
     * @throws \InvalidArgumentException
159
     *
160
     * @return string
161
     */
162 1
    public function checkValueInArray($property, $value)
163
    {
164
        switch ($property) {
165 1
            case 'Browser_Type':
166
                $allowedValues = [
167 1
                    'Useragent Anonymizer' => true,
168 1
                    'Browser' => true,
169 1
                    'Offline Browser' => true,
170 1
                    'Multimedia Player' => true,
171 1
                    'Library' => true,
172 1
                    'Feed Reader' => true,
173 1
                    'Email Client' => true,
174 1
                    'Bot/Crawler' => true,
175 1
                    'Application' => true,
176 1
                    'Tool' => true,
177 1
                    'unknown' => true,
178 1
                ];
179 1
                break;
180 1
            case 'Device_Type':
181
                $allowedValues = [
182 1
                    'Console' => true,
183 1
                    'TV Device' => true,
184 1
                    'Tablet' => true,
185 1
                    'Mobile Phone' => true,
186 1
                    'Smartphone' => true,    // actual mobile phone with touchscreen
187 1
                    'Feature Phone' => true, // older mobile phone
188 1
                    'Mobile Device' => true,
189 1
                    'FonePad' => true,       // Tablet sized device with the capability to make phone calls
190 1
                    'Desktop' => true,
191 1
                    'Ebook Reader' => true,
192 1
                    'Car Entertainment System' => true,
193 1
                    'Digital Camera' => true,
194 1
                    'unknown' => true,
195 1
                ];
196 1
                break;
197 1
            case 'Device_Pointing_Method':
198
                // This property is taken from http://www.scientiamobile.com/wurflCapability
199
                $allowedValues = [
200 1
                    'joystick' => true,
201 1
                    'stylus' => true,
202 1
                    'touchscreen' => true,
203 1
                    'clickwheel' => true,
204 1
                    'trackpad' => true,
205 1
                    'trackball' => true,
206 1
                    'mouse' => true,
207 1
                    'unknown' => true,
208 1
                ];
209 1
                break;
210 1
            case 'Browser_Bits':
211 1
            case 'Platform_Bits':
212
                $allowedValues = [
213 1
                    '0' => true,
214 1
                    '8' => true,
215 1
                    '16' => true,
216 1
                    '32' => true,
217 1
                    '64' => true,
218 1
                ];
219 1
                break;
220
            default:
221
                throw new \InvalidArgumentException('Property "' . $property . '" is not defined to be validated');
222
                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...
223
        }
224
225 1
        if (isset($allowedValues[$value])) {
226 1
            return $value;
227
        }
228
229
        throw new \InvalidArgumentException(
230
            'invalid value given for Property "' . $property . '": given value "' . (string) $value . '", allowed: '
231
            . json_encode($allowedValues)
232
        );
233
    }
234
}
235