| Conditions | 13 |
| Paths | 2 |
| Total Lines | 84 |
| Code Lines | 48 |
| 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 |
||
| 77 | public function register(Container $app) |
||
| 78 | { |
||
| 79 | $ns = $this->namespace; |
||
| 80 | $mns = $this->multiNamespace; |
||
| 81 | |||
| 82 | $app[$ns.'.default_options'] = isset($app[$ns.'.default_options']) ? $app[$ns.'.default_options'] : []; |
||
| 83 | $app[$ns.'.default_options'] += self::$defaultOptions; |
||
| 84 | |||
| 85 | $app[$ns.'.factory'] = $app->protect( |
||
| 86 | function (array $options = []) use ($app, $ns) { |
||
| 87 | $options += $app[$ns.'.default_options']; |
||
| 88 | $options += self::$defaultOptions; |
||
| 89 | |||
| 90 | if (empty($options['uri']) && !empty($options['host'])) { |
||
| 91 | |||
| 92 | $options['uri'] = $this->assembleUri( |
||
| 93 | $options['host'], |
||
| 94 | $options['port'], |
||
| 95 | $options['database'], |
||
| 96 | $options['username'], |
||
| 97 | $options['password'] |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | |||
| 101 | return new Client($options['uri'], $options['uri_options'], $options['driver_options']); |
||
| 102 | } |
||
| 103 | ); |
||
| 104 | |||
| 105 | $app[$mns.'.options.init'] = $app->protect( |
||
| 106 | function () use ($app, $ns, $mns) { |
||
| 107 | static $done = false; |
||
| 108 | |||
| 109 | if ($done) { |
||
| 110 | return; |
||
| 111 | } |
||
| 112 | $done = true; |
||
| 113 | |||
| 114 | if (isset($app[$ns.'.options']) && isset($app[$mns.'.options'])) { |
||
| 115 | throw new \LogicException("Illegal configuration - choose either single or multi connection setup", 1); |
||
| 116 | |||
| 117 | } |
||
| 118 | |||
| 119 | if (!isset($app[$mns.'.options'])) { |
||
| 120 | $singleOptions = isset($app[$ns.'.options']) ? $app[$ns.'.options'] : []; |
||
| 121 | $singleOptions += $app[$ns.'.default_options']; |
||
| 122 | |||
| 123 | $app[$mns.'.options'] = [ |
||
| 124 | 'default' => $singleOptions, |
||
| 125 | ]; |
||
| 126 | } |
||
| 127 | |||
| 128 | if (isset($app[$mns.'.options']['default'])) { |
||
| 129 | $app[$mns.'.default'] = 'default'; |
||
| 130 | } elseif (!isset($app[$mns.'.default'])) { |
||
| 131 | $tmp = $app[$mns.'.options']; |
||
| 132 | reset($tmp); |
||
| 133 | $app[$mns.'.default'] = key($tmp); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | ); |
||
| 137 | |||
| 138 | $app[$mns] = function ($app) use ($ns, $mns) { |
||
| 139 | $app[$mns.'.options.init'](); |
||
| 140 | |||
| 141 | $mongos = new Container(); |
||
| 142 | foreach ($app[$mns.'.options'] as $name => $options) { |
||
| 143 | $mongos[$name] = function () use ($app, $options, $ns) { |
||
| 144 | return $app[$ns.'.factory']($options); |
||
| 145 | }; |
||
| 146 | } |
||
| 147 | |||
| 148 | if (!isset($mongos['default'])) { |
||
| 149 | $mongos['default'] = $mongos[$app[$mns.'.default']]; |
||
| 150 | } |
||
| 151 | |||
| 152 | return $mongos; |
||
| 153 | }; |
||
| 154 | |||
| 155 | $app[$ns] = function ($app) use ($mns) { |
||
| 156 | $dbs = $app[$mns]; |
||
| 157 | |||
| 158 | return $dbs[$app[$mns.'.default']]; |
||
| 159 | }; |
||
| 160 | } |
||
| 161 | |||
| 186 |