Completed
Pull Request — master (#139)
by Thomas
10:06
created

PropertyHolder::getPropertyType()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 95
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 72
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 95
ccs 72
cts 73
cp 0.9863
rs 8.2079
cc 6
eloc 73
nc 6
nop 1
crap 6

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-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
 * @package    Data
25
 * @copyright  1998-2015 Browser Capabilities Project
26
 * @license    http://www.opensource.org/licenses/MIT MIT License
27
 * @link       https://github.com/browscap/browscap-php/
28
 * @since      added with version 3.0
29
 */
30
31
namespace BrowscapPHP\Data;
32
33
/**
34
 * Class PropertyHolder
35
 *
36
 * @category   Browscap
37
 * @package    Data
38
 * @author     Thomas Müller <[email protected]>
39
 */
40
class PropertyHolder
41
{
42
    const TYPE_STRING   = 'string';
43
    const TYPE_GENERIC  = 'generic';
44
    const TYPE_NUMBER   = 'number';
45
    const TYPE_BOOLEAN  = 'boolean';
46
    const TYPE_IN_ARRAY = 'in_array';
47
48
    /**
49
     * Get the type of a property
50
     *
51
     * @param  string     $propertyName
52
     * @throws \Exception
53
     * @return string
54
     */
55 6
    public function getPropertyType($propertyName)
56
    {
57
        $stringProperties = array(
58 6
            'Comment',
59 6
            'Browser',
60 6
            'Browser_Maker',
61 6
            'Browser_Modus',
62 6
            'Platform',
63 6
            'Platform_Name',
64 6
            'Platform_Description',
65 6
            'Device_Name',
66 6
            'Platform_Maker',
67 6
            'Device_Code_Name',
68 6
            'Device_Maker',
69 6
            'Device_Brand_Name',
70 6
            'RenderingEngine_Name',
71 6
            'RenderingEngine_Description',
72 6
            'RenderingEngine_Maker',
73 6
            'Parent',
74 6
            'PropertyName',
75 6
            'CDF',
76 6
        );
77
78 6
        if (in_array($propertyName, $stringProperties)) {
79 6
            return self::TYPE_STRING;
80
        }
81
82
        $arrayProperties = array(
83 6
            'Browser_Type',
84 6
            'Device_Type',
85 6
            'Device_Pointing_Method',
86 6
            'Browser_Bits',
87 6
            'Platform_Bits',
88 6
        );
89
90 6
        if (in_array($propertyName, $arrayProperties)) {
91 1
            return self::TYPE_IN_ARRAY;
92
        }
93
94
        $genericProperties = array(
95 6
            'Platform_Version',
96 6
            'RenderingEngine_Version',
97 6
            'Released',
98 6
            'Format',
99 6
            'Type',
100 6
        );
101
102 6
        if (in_array($propertyName, $genericProperties)) {
103 6
            return self::TYPE_GENERIC;
104
        }
105
106
        $numericProperties = array(
107 6
            'Version',
108 6
            'CssVersion',
109 6
            'AolVersion',
110 6
            'MajorVer',
111 6
            'MinorVer',
112 6
        );
113
114 6
        if (in_array($propertyName, $numericProperties)) {
115 6
            return self::TYPE_NUMBER;
116
        }
117
118
        $booleanProperties = array(
119 6
            'Alpha',
120 6
            'Beta',
121 6
            'Win16',
122 6
            'Win32',
123 6
            'Win64',
124 6
            'Frames',
125 6
            'IFrames',
126 6
            'Tables',
127 6
            'Cookies',
128 6
            'BackgroundSounds',
129 6
            'JavaScript',
130 6
            'VBScript',
131 6
            'JavaApplets',
132 6
            'ActiveXControls',
133 6
            'isMobileDevice',
134 6
            'isTablet',
135 6
            'isSyndicationReader',
136 6
            'Crawler',
137 6
            'MasterParent',
138 6
            'LiteMode',
139 6
            'isFake',
140 6
            'isAnonymized',
141 6
            'isModified',
142 6
        );
143
144 6
        if (in_array($propertyName, $booleanProperties)) {
145 6
            return self::TYPE_BOOLEAN;
146
        }
147
148
        throw new \InvalidArgumentException("Property {$propertyName} did not have a defined property type");
149
    }
150
151
    /**
152
     * @param string $property
153
     * @param string $value
154
     *
155
     * @throws \InvalidArgumentException
156
     * @return string
157
     */
158 1
    public function checkValueInArray($property, $value)
159
    {
160
        switch ($property) {
161 1
            case 'Browser_Type':
162
                $allowedValues = array(
163 1
                    'Useragent Anonymizer',
164 1
                    'Browser',
165 1
                    'Offline Browser',
166 1
                    'Multimedia Player',
167 1
                    'Library',
168 1
                    'Feed Reader',
169 1
                    'Email Client',
170 1
                    'Bot/Crawler',
171 1
                    'Application',
172 1
                    'Tool',
173 1
                    'unknown',
174 1
                );
175 1
                break;
176 1
            case 'Device_Type':
177
                $allowedValues = array(
178 1
                    'Console',
179 1
                    'TV Device',
180 1
                    'Tablet',
181 1
                    'Mobile Phone',
182 1
                    'Smartphone',    // actual mobile phone with touchscreen
183 1
                    'Feature Phone', // older mobile phone
184 1
                    'Mobile Device',
185 1
                    'FonePad',       // Tablet sized device with the capability to make phone calls
186 1
                    'Desktop',
187 1
                    'Ebook Reader',
188 1
                    'Car Entertainment System',
189 1
                    'Digital Camera',
190 1
                    'unknown',
191 1
                );
192 1
                break;
193 1
            case 'Device_Pointing_Method':
194
                // This property is taken from http://www.scientiamobile.com/wurflCapability
195
                $allowedValues = array(
196 1
                    'joystick', 'stylus', 'touchscreen', 'clickwheel', 'trackpad', 'trackball', 'mouse', 'unknown'
197 1
                );
198 1
                break;
199 1
            case 'Browser_Bits':
200 1
            case 'Platform_Bits':
201
                $allowedValues = array(
202 1
                    '0', '8', '16', '32', '64'
203 1
                );
204 1
                break;
205
            default:
206
                throw new \InvalidArgumentException('Property "' . $property . '" is not defined to be validated');
207
                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...
208
        }
209
210 1
        if (in_array($value, $allowedValues)) {
211 1
            return $value;
212
        }
213
214
        throw new \InvalidArgumentException(
215
            'invalid value given for Property "'.$property.'": given value "'.(string) $value.'", allowed: '
216
            .json_encode($allowedValues)
217
        );
218
    }
219
}
220