Conditions | 13 |
Paths | 80 |
Total Lines | 46 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
110 | public static function validate(array $options = ['allowCredentials' => false, 'maxAge' => 86400, 'allowedHeaders' => []]): bool |
||
111 | { |
||
112 | $flag = false; |
||
113 | |||
114 | if (isset($_SERVER['HTTP_ORIGIN'])) { |
||
115 | foreach (static::$allowedDomains as $domain) { |
||
116 | if (preg_match_all('/' . $domain['domain'] . '/i', parse_url($_SERVER['HTTP_ORIGIN'])['path']) > 0) { |
||
117 | self::sendHeader('Access-Control-Allow-Origin', $_SERVER['HTTP_ORIGIN']); |
||
118 | self::sendHeader('Access-Control-Allow-Credentials', $domain['allowCredentials'] ? 'true' : 'false'); |
||
119 | self::sendHeader('Access-Control-Max-Age', $domain['maxAge'] ?? $options['maxAge']); |
||
120 | $flag = true; |
||
121 | break; |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | |||
126 | if (isset($_SERVER['REMOTE_ADDR'])) { |
||
127 | foreach (static::$allowedIps as $ip) { |
||
128 | if ($_SERVER['REMOTE_ADDR'] == $ip) { |
||
129 | $flag = true; |
||
130 | break; |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | |||
135 | if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { |
||
136 | if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) { |
||
137 | self::sendHeader('Access-Control-Allow-Methods', $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']); |
||
138 | } |
||
139 | |||
140 | if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) { |
||
141 | $allowedHeaders = []; |
||
142 | |||
143 | foreach (explode(',', $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']) as $header) { |
||
144 | if (in_array($header, $options['allowedHeaders'])) { |
||
145 | $allowedHeaders[] = $header; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | self::sendHeader('Access-Control-Allow-Headers', implode(',', $allowedHeaders)); |
||
150 | } |
||
151 | |||
152 | return true; |
||
153 | } |
||
154 | |||
155 | return $flag; |
||
156 | } |
||
173 | } |