Completed
Push — 7.x-1.x ( 64022a...8b8e48 )
by Fabian
05:21
created

Container::get()   F

Complexity

Conditions 31
Paths 508

Size

Total Lines 134
Code Lines 98

Duplication

Lines 3
Ratio 2.24 %
Metric Value
dl 3
loc 134
rs 2.8319
cc 31
eloc 98
nc 508
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Container::underscore() 0 3 1

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * @file
4
 * Contains \Drupal\service_container\DependencyInjection\Container
5
 */
6
7
namespace Drupal\service_container\DependencyInjection;
8
9
use Drupal\Component\DependencyInjection\PhpArrayContainer;
10
use ReflectionClass;
11
use RuntimeException;
12
use Symfony\Component\DependencyInjection\ScopeInterface;
13
14
/**
15
 * Container is a DI container that provides services to users of the class.
16
 *
17
 * @ingroup dic
18
 */
19
class Container extends PhpArrayContainer implements ContainerInterface {
20
21
  /**
22
   * {@inheritdoc}
23
   */
24
  public function createInstance($plugin_id, $service_definition) {
25
    $temporary_name = 'plugin_' . $plugin_id;
26
    $this->serviceDefinitions[$temporary_name] = $service_definition;
27
28
    $plugin = $this->get($temporary_name);
29
    unset($this->serviceDefinitions[$temporary_name]);
30
    unset($this->services[$temporary_name]);
31
32
    return $plugin;
33
  }
34
35
  /**
36
   * {@inheritdoc}
37
   */
38
  public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
39
    $definition = isset($this->serviceDefinitions[$plugin_id]) ? $this->serviceDefinitions[$plugin_id] : NULL;
40
41
    if (!$definition && $exception_on_invalid) {
42
      throw new RuntimeException(sprintf('The "%s" service definition does not exist.', $plugin_id));
43
    }
44
45
    return $definition;
46
  }
47
48
  /**
49
   * {@inheritdoc}
50
   */
51
  public function getDefinitions() {
52
    return $this->serviceDefinitions;
53
  }
54
55
  /**
56
   * {@inheritdoc}
57
   */
58
  public function hasDefinition($plugin_id) {
59
    return isset($this->serviceDefinitions[$plugin_id]);
60
  }
61
62
  /**
63
   * Camelizes a string.
64
   *
65
   * @param $name
66
   *   The string to camelize.
67
   *
68
   * @return string
69
   *   The camelized string.
70
   *
71
   */
72
  public static function camelize($name) {
73
    return strtr(ucwords(strtr($name, array('_' => ' ', '\\' => '_ '))), array(' ' => ''));
74
  }
75
76
  /**
77
   * Un-camelizes a string.
78
   *
79
   * @param $name
80
   *   The string to underscore.
81
   *
82
   * @return string
83
   *   The underscored string.
84
   *
85
   */
86
  public static function underscore($name) {
87
    return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), $name));
88
  }
89
}
90