Conditions | 21 |
Paths | 67 |
Total Lines | 58 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
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 |
||
20 | public static function validationError($data, $options = 0) |
||
21 | { |
||
22 | if ($options === Uri::IS_URI_TEMPLATE) { |
||
23 | $opened = false; |
||
24 | for ($i = 0; $i < strlen($data); ++$i) { |
||
25 | if ($data[$i] === '{') { |
||
26 | if ($opened) { |
||
27 | return 'Invalid uri-template: unexpected "{"'; |
||
28 | } else { |
||
29 | $opened = true; |
||
30 | } |
||
31 | } elseif ($data[$i] === '}') { |
||
32 | if ($opened) { |
||
33 | $opened = false; |
||
34 | } else { |
||
35 | return 'Invalid uri-template: unexpected "}"'; |
||
36 | } |
||
37 | } |
||
38 | } |
||
39 | if ($opened) { |
||
40 | return 'Invalid uri-template: unexpected end of string'; |
||
41 | } |
||
42 | } |
||
43 | |||
44 | $uri = parse_url($data); |
||
45 | if (!$uri) { |
||
46 | return 'Malformed URI'; |
||
47 | } |
||
48 | if (($options & self::IS_SCHEME_REQUIRED) && (!isset($uri['scheme']) || $uri['scheme'] === '')) { |
||
49 | return 'Missing scheme in URI'; |
||
50 | } |
||
51 | if (isset($uri['host'])) { |
||
52 | $host = $uri['host']; |
||
53 | if (!preg_match(Format::HOSTNAME_REGEX, $host)) { |
||
54 | // stripping [ ] |
||
55 | if ($host[0] === '[' && $host[strlen($host) - 1] === ']') { |
||
56 | $host = substr($host, 1, -1); |
||
57 | } |
||
58 | if (!filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
||
59 | return 'Malformed host in URI: ' . $host; |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | |||
64 | if (isset($uri['path'])) { |
||
65 | if (strpos($uri['path'], '\\') !== false) { |
||
66 | return 'Invalid path: unescaped backslash'; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | if (isset($uri['fragment'])) { |
||
71 | if (strpos($uri['fragment'], '\\') !== false) { |
||
72 | return 'Invalid fragment: unescaped backslash'; |
||
73 | } |
||
74 | } |
||
75 | |||
76 | return null; |
||
77 | } |
||
78 | } |