| Conditions | 78 |
| Paths | > 20000 |
| Total Lines | 278 |
| Code Lines | 157 |
| Lines | 40 |
| Ratio | 14.39 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 216 | private function parseDefinition($id, $service, $file) |
||
| 217 | { |
||
| 218 | // nette |
||
| 219 | if ($service instanceof Entity) { |
||
| 220 | $value = $service->value; |
||
| 221 | $service = ['arguments' => $service->attributes]; |
||
| 222 | if (false === strpos($value, ':')) { |
||
| 223 | $service['class'] = $value; |
||
| 224 | } else { |
||
| 225 | $service['factory'] = $value; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | // nette |
||
| 230 | if (preg_match('#^(\S+)\s+<\s+(\S+)\z#', $id, $matches)) { |
||
| 231 | View Code Duplication | if (isset($service['parent']) && $matches[2] !== $service['parent']) { |
|
| 232 | throw new InvalidArgumentException(sprintf('Two parent services "%s" and "%s" are defined for service "%s" in "%s". Check your NEON syntax.', $service['parent'], $matches[2], $matches[1], $file)); |
||
| 233 | } |
||
| 234 | |||
| 235 | $id = $matches[1]; |
||
| 236 | $parent = $matches[2]; |
||
| 237 | } |
||
| 238 | |||
| 239 | // nette |
||
| 240 | if (is_string($service) && false !== strpos($service, ':')) { |
||
| 241 | $service = ['factory' => $this->parseFactory($service, $file)]; |
||
| 242 | } elseif (is_string($service) && 0 === strpos($service, '@')) { |
||
| 243 | $this->container->setAlias($id, substr($service, 1)); |
||
| 244 | |||
| 245 | return; |
||
| 246 | } elseif (is_string($service)) { |
||
| 247 | $service = ['class' => $service]; |
||
| 248 | } |
||
| 249 | |||
| 250 | if (!is_array($service)) { |
||
| 251 | throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" or a NEON entity but %s found for service "%s" in %s. Check your NEON syntax.', gettype($service), $id, $file)); |
||
| 252 | } |
||
| 253 | |||
| 254 | self::checkDefinition($id, $service, $file); |
||
| 255 | |||
| 256 | if (isset($service['alias'])) { |
||
| 257 | $public = !array_key_exists('public', $service) || (bool) $service['public']; |
||
| 258 | $this->container->setAlias($id, new Alias($service['alias'], $public)); |
||
| 259 | |||
| 260 | foreach ($service as $key => $value) { |
||
| 261 | if (!in_array($key, ['alias', 'public'])) { |
||
| 262 | throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for alias definition "%s" in "%s". Allowed configuration keys are "alias" and "public".', $key, $id, $file)); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | return; |
||
| 267 | } |
||
| 268 | |||
| 269 | // nette |
||
| 270 | if (isset($parent)) { |
||
| 271 | $service['parent'] = $parent; |
||
| 272 | } |
||
| 273 | |||
| 274 | if (isset($service['parent'])) { |
||
| 275 | $definition = new DefinitionDecorator($service['parent']); |
||
| 276 | } else { |
||
| 277 | $definition = new Definition(); |
||
| 278 | } |
||
| 279 | |||
| 280 | View Code Duplication | if (isset($service['class'])) { |
|
| 281 | $class = $service['class']; |
||
| 282 | |||
| 283 | // nette |
||
| 284 | if ($class instanceof Entity) { |
||
| 285 | if (isset($service['arguments']) && !empty($class->attributes)) { |
||
| 286 | throw new InvalidArgumentException(sprintf('Duplicated definition of arguments for service "%s" in "%s". Check you NEON syntax.', $id, $file)); |
||
| 287 | } |
||
| 288 | |||
| 289 | $service['arguments'] = $class->attributes; |
||
| 290 | $class = $class->value; |
||
| 291 | } |
||
| 292 | |||
| 293 | $definition->setClass($class); |
||
| 294 | } |
||
| 295 | |||
| 296 | if (isset($service['shared'])) { |
||
| 297 | $definition->setShared($service['shared']); |
||
| 298 | } |
||
| 299 | |||
| 300 | if (isset($service['synthetic'])) { |
||
| 301 | $definition->setSynthetic($service['synthetic']); |
||
| 302 | } |
||
| 303 | |||
| 304 | if (isset($service['lazy'])) { |
||
| 305 | $definition->setLazy($service['lazy']); |
||
| 306 | } |
||
| 307 | |||
| 308 | if (isset($service['public'])) { |
||
| 309 | $definition->setPublic($service['public']); |
||
| 310 | } |
||
| 311 | |||
| 312 | if (isset($service['abstract'])) { |
||
| 313 | $definition->setAbstract($service['abstract']); |
||
| 314 | } |
||
| 315 | |||
| 316 | if (array_key_exists('deprecated', $service)) { |
||
| 317 | $definition->setDeprecated(true, $service['deprecated']); |
||
| 318 | } |
||
| 319 | |||
| 320 | View Code Duplication | if (isset($service['factory'])) { |
|
| 321 | $factory = $service['factory']; |
||
| 322 | |||
| 323 | //nette |
||
| 324 | if ($factory instanceof Entity) { |
||
| 325 | if (isset($service['arguments']) && !empty($factory->attributes)) { |
||
| 326 | throw new InvalidArgumentException(sprintf('Duplicated definition of arguments for service "%s" in "%s". Check you NEON syntax.', $id, $file)); |
||
| 327 | } |
||
| 328 | |||
| 329 | $service['arguments'] = $factory->attributes; |
||
| 330 | $factory = $factory->value; |
||
| 331 | } |
||
| 332 | |||
| 333 | $definition->setFactory($this->parseFactory($factory, $file)); |
||
| 334 | } |
||
| 335 | |||
| 336 | if (isset($service['file'])) { |
||
| 337 | $definition->setFile($service['file']); |
||
| 338 | } |
||
| 339 | |||
| 340 | if (isset($service['arguments'])) { |
||
| 341 | $autowired = false; |
||
| 342 | array_walk($service['arguments'], function (&$value) use (&$autowired) { |
||
| 343 | if ('...' === $value) { |
||
| 344 | $value = ''; |
||
| 345 | $autowired = true; |
||
| 346 | } |
||
| 347 | |||
| 348 | return $value; |
||
| 349 | }); |
||
| 350 | |||
| 351 | $definition->setAutowired($autowired); |
||
| 352 | $definition->setArguments($this->resolveServices($service['arguments'], $file)); |
||
| 353 | } |
||
| 354 | |||
| 355 | // nette |
||
| 356 | if (isset($service['setup'])) { |
||
| 357 | foreach ($service['setup'] as $setup) { |
||
| 358 | if ($setup instanceof Entity) { |
||
| 359 | $name = $setup->value; |
||
| 360 | $args = $setup->attributes; |
||
| 361 | } elseif (is_array($setup)) { |
||
| 362 | $name = $setup[0]; |
||
| 363 | $args = isset($setup[1]) ? $setup[1] : []; |
||
| 364 | } else { |
||
| 365 | $name = $setup; |
||
| 366 | $args = []; |
||
| 367 | } |
||
| 368 | |||
| 369 | if ('$' === $name[0]) { |
||
| 370 | $service['properties'][substr($name, 1)] = $args; |
||
| 371 | } else { |
||
| 372 | $service['calls'][] = [$name, $args]; |
||
| 373 | } |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | if (isset($service['properties'])) { |
||
| 378 | $definition->setProperties($this->resolveServices($service['properties'], $file)); |
||
| 379 | } |
||
| 380 | |||
| 381 | if (isset($service['configurator'])) { |
||
| 382 | if (is_string($service['configurator'])) { |
||
| 383 | $definition->setConfigurator($service['configurator']); |
||
| 384 | } else { |
||
| 385 | $definition->setConfigurator([$this->resolveServices($service['configurator'][0], $file), $service['configurator'][1]]); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | if (isset($service['calls'])) { |
||
| 390 | if (!is_array($service['calls'])) { |
||
| 391 | throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your NEON syntax.', $id, $file)); |
||
| 392 | } |
||
| 393 | |||
| 394 | foreach ($service['calls'] as $call) { |
||
| 395 | if ($call instanceof Entity) { // nette |
||
| 396 | $method = $call->value; |
||
| 397 | $args = $this->resolveServices($call->attributes, $file); |
||
| 398 | } elseif (isset($call['method'])) { |
||
| 399 | $method = $call['method']; |
||
| 400 | $args = isset($call['arguments']) ? $this->resolveServices($call['arguments'], $file) : []; |
||
| 401 | } elseif (is_array($call)) { |
||
| 402 | $method = $call[0]; |
||
| 403 | $args = isset($call[1]) ? $this->resolveServices($call[1], $file) : []; |
||
| 404 | } else { // nette |
||
| 405 | $method = $call; |
||
| 406 | $args = []; |
||
| 407 | } |
||
| 408 | |||
| 409 | $definition->addMethodCall($method, $args); |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | if (isset($service['tags'])) { |
||
| 414 | if (!is_array($service['tags'])) { |
||
| 415 | throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your NEON syntax.', $id, $file)); |
||
| 416 | } |
||
| 417 | |||
| 418 | foreach ($service['tags'] as $tag) { |
||
| 419 | if ($tag instanceof Entity) { |
||
| 420 | $tag = ['name' => $tag->value] + $tag->attributes; |
||
| 421 | } elseif (is_string($tag)) { |
||
| 422 | $tag = ['name' => $tag]; |
||
| 423 | } |
||
| 424 | |||
| 425 | if (!is_array($tag)) { |
||
| 426 | throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your NEON syntax.', $id, $file)); |
||
| 427 | } |
||
| 428 | |||
| 429 | if (!isset($tag['name'])) { |
||
| 430 | throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file)); |
||
| 431 | } |
||
| 432 | |||
| 433 | if (!is_string($tag['name']) || '' === $tag['name']) { |
||
| 434 | throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file)); |
||
| 435 | } |
||
| 436 | |||
| 437 | $name = $tag['name']; |
||
| 438 | unset($tag['name']); |
||
| 439 | |||
| 440 | foreach ($tag as $attribute => $value) { |
||
| 441 | if (!is_scalar($value) && null !== $value) { |
||
| 442 | throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your NEON syntax.', $id, $name, $attribute, $file)); |
||
| 443 | } |
||
| 444 | } |
||
| 445 | |||
| 446 | $definition->addTag($name, $tag); |
||
| 447 | } |
||
| 448 | } |
||
| 449 | |||
| 450 | if (isset($service['decorates'])) { |
||
| 451 | $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null; |
||
| 452 | $priority = isset($service['decoration_priority']) ? $service['decoration_priority'] : 0; |
||
| 453 | $definition->setDecoratedService($service['decorates'], $renameId, $priority); |
||
| 454 | } |
||
| 455 | |||
| 456 | // nette |
||
| 457 | View Code Duplication | if (isset($service['autowired'])) { |
|
| 458 | if (isset($service['autowire']) && $service['autowire'] !== $service['autowired']) { |
||
| 459 | throw new InvalidArgumentException(sprintf('Contradictory definition of autowiring for service "%s" in "%s". Check you NEON syntax.', $id, $file)); |
||
| 460 | } |
||
| 461 | |||
| 462 | $service['autowire'] = $service['autowired']; |
||
| 463 | } |
||
| 464 | |||
| 465 | if (isset($service['autowire'])) { |
||
| 466 | // nette |
||
| 467 | if ($definition->isAutowired() && !$service['autowire']) { |
||
| 468 | throw new InvalidArgumentException(sprintf('Contradictory definition of autowiring for service "%s" in "%s". Check you NEON syntax.', $id, $file)); |
||
| 469 | } |
||
| 470 | |||
| 471 | $definition->setAutowired($service['autowire']); |
||
| 472 | } |
||
| 473 | |||
| 474 | if (isset($service['autowiring_types'])) { |
||
| 475 | if (is_string($service['autowiring_types'])) { |
||
| 476 | $definition->addAutowiringType($service['autowiring_types']); |
||
| 477 | } else { |
||
| 478 | if (!is_array($service['autowiring_types'])) { |
||
| 479 | throw new InvalidArgumentException(sprintf('Parameter "autowiring_types" must be a string or an array for service "%s" in %s. Check your NEON syntax.', $id, $file)); |
||
| 480 | } |
||
| 481 | |||
| 482 | foreach ($service['autowiring_types'] as $autowiringType) { |
||
| 483 | if (!is_string($autowiringType)) { |
||
| 484 | throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. Check your NEON syntax.', $id, $file)); |
||
| 485 | } |
||
| 486 | |||
| 487 | $definition->addAutowiringType($autowiringType); |
||
| 488 | } |
||
| 489 | } |
||
| 490 | } |
||
| 491 | |||
| 492 | $this->container->setDefinition($id, $definition); |
||
| 493 | } |
||
| 494 | |||
| 589 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: