Passed
Branch main (a52c5c)
by Sammy
03:30 queued 01:56
created

LeMarchand::get()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 48
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 48
rs 7.6666
c 0
b 0
f 0
cc 10
nc 10
nop 1

How to fix   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
namespace HexMakina\LeMarchand;
4
5
use \Psr\Container\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Container\ContainerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
class LeMarchand implements ContainerInterface
8
{
9
  private $configurations = [];
10
  
11
  public function __construct($settings)
12
  {
13
    $this->configurations['settings'] = $settings;
14
  }
15
  
16
  public function __debugInfo() : array
17
  {
18
    $dbg = get_object_vars($this);
19
    $dbg['configurations']['template_engine'] = isset($dbg['configurations']['template_engine'])? get_class($dbg['configurations']['template_engine']) : 'NOT SET';
20
21
    return $dbg;
22
  }
23
  
24
  public function register($configuration, $instance)
25
  {
26
    $this->configurations[$configuration] = $instance;
27
  }
28
  
29
  public function has($configuration)
30
  {
31
    return isset($this->configurations[$configuration]);
32
  }
33
  
34
  public function get($configuration)
35
  {
36
    
37
    if(!is_string($configuration))
38
      throw new LamentException($configuration);
39
40
    if($this->has($configuration))
41
      return $this->configurations[$configuration];
42
43
    
44
    // fallbacks
45
    if(preg_match('/^settings\./', $configuration, $m) === 1)
46
    {
47
      $ret = $this->configurations;
48
      foreach(explode('.', $configuration) as $k)
49
      {
50
        if(!isset($ret[$k]))
51
          throw new ConfigurationException($configuration);
52
        $ret = $ret[$k];
53
      }
54
55
      return $ret;
56
    }
57
    elseif(class_exists($configuration)) // auto create instances
58
    {
59
      
60
      $rc = new \ReflectionClass($configuration);
61
62
      $construction_args = [];
63
      $instance = null;
64
      if(!is_null($rc->getConstructor()))
65
      {
66
        foreach($rc->getConstructor()->getParameters() as $param)
67
        {
68
          $construction_args []= $this->get($param->getType().'');
69
        }
70
        $instance = $rc->newInstanceArgs($construction_args);
71
      }
72
      else 
73
        $instance = $rc->newInstanceArgs();
74
75
      if($rc->hasMethod('set_container'))
76
        $instance->set_container($this);
77
      
78
      return $instance;
79
    }
80
81
    throw new ConfigurationException($configuration);
82
  }
83
84
}
85
86
87