Conditions | 37 |
Paths | 25 |
Total Lines | 159 |
Code Lines | 93 |
Lines | 69 |
Ratio | 43.4 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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#') |
|
|
|||
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#') |
|
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'])) { |
|
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'])) { |
|
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'])) { |
|
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) { |
|
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'])) { |
|
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#') |
|
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#') |
|
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'])) { |
|
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 |
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.