| Conditions | 20 |
| Paths | 101 |
| Total Lines | 107 |
| Code Lines | 65 |
| 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 |
||
| 196 | public function loadDomainData($domain) |
||
| 197 | { |
||
| 198 | $localeName = $this->locale->getIdentifier(); |
||
| 199 | $localeNameBases = Locale::getLookupPath($localeName); |
||
| 200 | |||
| 201 | if (!isset($this->domainPaths[$domain])) { |
||
| 202 | if (!$this->domainPathPattern) { |
||
| 203 | throw new AgaviException('Using domain "' . $domain . '" which has no path specified'); |
||
| 204 | } else { |
||
| 205 | $basePath = $this->domainPathPattern; |
||
| 206 | } |
||
| 207 | } else { |
||
| 208 | $basePath = $this->domainPaths[$domain]; |
||
| 209 | } |
||
| 210 | |||
| 211 | $basePath = Toolkit::expandVariables($basePath, array('domain' => $domain)); |
||
| 212 | |||
| 213 | $data = array(); |
||
| 214 | |||
| 215 | foreach ($localeNameBases as $localeNameBase) { |
||
| 216 | $fileName = Toolkit::expandVariables($basePath, array('locale' => $localeNameBase)); |
||
| 217 | if ($fileName === $basePath) { |
||
| 218 | // no replacing of $locale happened |
||
| 219 | $fileName = $basePath . '/' . $localeNameBase . '.mo'; |
||
| 220 | } |
||
| 221 | if (is_readable($fileName)) { |
||
| 222 | $fileData = GettextMoReader::readFile($fileName); |
||
| 223 | |||
| 224 | // instead of array_merge, which doesn't handle null bytes in keys properly. careful, the order matters here. |
||
| 225 | $data = $fileData + $data; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | $headers = array(); |
||
| 230 | |||
| 231 | if (count($data)) { |
||
| 232 | $headerData = str_replace("\r", '', $data['']); |
||
| 233 | $headerLines = explode("\n", $headerData); |
||
| 234 | foreach ($headerLines as $line) { |
||
| 235 | $values = explode(':', $line, 2); |
||
| 236 | // skip empty / invalid lines |
||
| 237 | if (count($values) == 2) { |
||
| 238 | $headers[$values[0]] = $values[1]; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | $this->pluralFormFunc = null; |
||
| 244 | if (isset($headers['Plural-Forms'])) { |
||
| 245 | $pf = $headers['Plural-Forms']; |
||
| 246 | if (preg_match('#nplurals=\d+;\s+plural=(.*)$#D', $pf, $match)) { |
||
| 247 | $funcCode = $match[1]; |
||
| 248 | $validOpChars = array(' ', 'n', '!', '&', '|', '<', '>', '(', ')', '?', ':', ';', '=', '+', '*', '/', '%', '-'); |
||
| 249 | if (preg_match('#[^\d' . preg_quote(implode('', $validOpChars), '#') . ']#', $funcCode, $errorMatch)) { |
||
| 250 | throw new AgaviException('Illegal character ' . $errorMatch[0] . ' in plural form ' . $funcCode); |
||
| 251 | } |
||
| 252 | |||
| 253 | // add parenthesis around all ternary expressions. This is done |
||
| 254 | // to make the ternary operator (?) have precedence over the delimiter (:) |
||
| 255 | // This will transform |
||
| 256 | // "a ? 1 : b ? c ? 3 : 4 : 2" to "(a ? 1 : (b ? (c ? 3 : 4) : 2))" and |
||
| 257 | // "a ? b ? c ? d ? 5 : 4 : 3 : 2 : 1" to "(a ? (b ? (c ? (d ? 5 : 4) : 3) : 2) : 1)" |
||
| 258 | // "a ? b ? c ? 4 : 3 : d ? 5 : 2 : 1" to "(a ? (b ? (c ? 4 : 3) : (d ? 5 : 2)) : 1)" |
||
| 259 | // "a ? b ? c ? 4 : 3 : d ? 5 : e ? 6 : 2 : 1" to "(a ? (b ? (c ? 4 : 3) : (d ? 5 : (e ? 6 : 2))) : 1)" |
||
| 260 | |||
| 261 | $funcCode = rtrim($funcCode, ';'); |
||
| 262 | $parts = preg_split('#(\?|\:)#', $funcCode, -1, PREG_SPLIT_DELIM_CAPTURE); |
||
| 263 | $parenthesisCount = 0; |
||
| 264 | $unclosedParenthesisCount = 0; |
||
| 265 | $firstParenthesis = true; |
||
| 266 | $funcCode = ''; |
||
| 267 | for ($i = 0, $c = count($parts); $i < $c; ++$i) { |
||
| 268 | $lastPart = $i > 0 ? $parts[$i - 1] : null; |
||
| 269 | $part = $parts[$i]; |
||
| 270 | $nextPart = $i + 1 < $c ? $parts[$i + 1] : null; |
||
| 271 | if ($nextPart == '?') { |
||
| 272 | if ($lastPart == ':') { |
||
| 273 | // keep track of parenthesis which need to be closed |
||
| 274 | // directly after this ternary expression |
||
| 275 | ++$unclosedParenthesisCount; |
||
| 276 | --$parenthesisCount; |
||
| 277 | } |
||
| 278 | $funcCode .= ' (' . $part; |
||
| 279 | ++$parenthesisCount; |
||
| 280 | } elseif ($lastPart == ':') { |
||
| 281 | $funcCode .= $part . ') '; |
||
| 282 | if ($unclosedParenthesisCount > 0) { |
||
| 283 | $funcCode .= str_repeat(')', $unclosedParenthesisCount); |
||
| 284 | $unclosedParenthesisCount = 0; |
||
| 285 | } |
||
| 286 | --$parenthesisCount; |
||
| 287 | } else { |
||
| 288 | $funcCode .= $part; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | if ($parenthesisCount > 0) { |
||
| 292 | // add the missing top level parenthesis |
||
| 293 | $funcCode .= str_repeat(')', $parenthesisCount); |
||
| 294 | } |
||
| 295 | $funcCode .= ';'; |
||
| 296 | $funcCode = 'return ' . str_replace('n', '$n', $funcCode); |
||
| 297 | $this->pluralFormFunc = create_function('$n', $funcCode); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | $this->domainData[$domain] = array('headers' => $headers, 'msgs' => $data); |
||
| 302 | } |
||
| 303 | } |
||
| 304 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: