| Conditions | 14 |
| Paths | 1 |
| Total Lines | 101 |
| 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 |
||
| 29 | public function register(ContainerInterface $app) |
||
| 30 | { |
||
| 31 | $app['db.default_options'] = [ |
||
| 32 | 'driver' => 'pdo_mysql', |
||
| 33 | 'dbname' => null, |
||
| 34 | 'host' => 'localhost', |
||
| 35 | 'user' => 'root', |
||
| 36 | 'password' => null, |
||
| 37 | ]; |
||
| 38 | |||
| 39 | $app['dbs.options.initializer'] = $app->protect(function () use ($app) { |
||
| 40 | static $initialized = false; |
||
| 41 | |||
| 42 | if ($initialized) { |
||
| 43 | return; |
||
| 44 | } |
||
| 45 | |||
| 46 | $initialized = true; |
||
| 47 | |||
| 48 | if (!isset($app['dbs.options'])) { |
||
| 49 | $app['dbs.options'] = ['default' => isset($app['db.options']) ? $app['db.options'] : []]; |
||
| 50 | } |
||
| 51 | |||
| 52 | $tmp = $app['dbs.options']; |
||
| 53 | foreach ($tmp as $name => &$options) { |
||
| 54 | $options = array_replace($app['db.default_options'], $options); |
||
| 55 | |||
| 56 | if (!isset($app['dbs.default'])) { |
||
| 57 | $app['dbs.default'] = $name; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | $app['dbs.options'] = $tmp; |
||
| 61 | }); |
||
| 62 | |||
| 63 | $app['dbs'] = function ($app) { |
||
| 64 | $app['dbs.options.initializer'](); |
||
| 65 | |||
| 66 | $dbs = new Container(); |
||
| 67 | foreach ($app['dbs.options'] as $name => $options) { |
||
| 68 | if ($app['dbs.default'] === $name) { |
||
| 69 | // we use shortcuts here in case the default has been overridden |
||
| 70 | $config = $app['db.config']; |
||
| 71 | $manager = $app['db.event_manager']; |
||
| 72 | } else { |
||
| 73 | $config = $app['dbs.config'][$name]; |
||
| 74 | $manager = $app['dbs.event_manager'][$name]; |
||
| 75 | } |
||
| 76 | |||
| 77 | $dbs[$name] = function ($dbs) use ($options, $config, $manager) { |
||
|
|
|||
| 78 | return DriverManager::getConnection($options, $config, $manager); |
||
| 79 | }; |
||
| 80 | } |
||
| 81 | |||
| 82 | return $dbs; |
||
| 83 | }; |
||
| 84 | |||
| 85 | $app['dbs.config'] = function ($app) { |
||
| 86 | $app['dbs.options.initializer'](); |
||
| 87 | |||
| 88 | $configs = new Container(); |
||
| 89 | $addLogger = isset($app['logger']) && null !== $app['logger'] && class_exists('Symfony\Bridge\Doctrine\Logger\DbalLogger'); |
||
| 90 | foreach ($app['dbs.options'] as $name => $options) { |
||
| 91 | $configs[$name] = new Configuration(); |
||
| 92 | if ($addLogger) { |
||
| 93 | $configs[$name]->setSQLLogger(new DbalLogger($app['logger'], isset($app['stopwatch']) ? $app['stopwatch'] : null)); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | return $configs; |
||
| 98 | }; |
||
| 99 | |||
| 100 | $app['dbs.event_manager'] = function ($app) { |
||
| 101 | $app['dbs.options.initializer'](); |
||
| 102 | |||
| 103 | $managers = new Container(); |
||
| 104 | foreach ($app['dbs.options'] as $name => $options) { |
||
| 105 | $managers[$name] = new EventManager(); |
||
| 106 | } |
||
| 107 | |||
| 108 | return $managers; |
||
| 109 | }; |
||
| 110 | |||
| 111 | // shortcuts for the "first" DB |
||
| 112 | $app['db'] = function ($app) { |
||
| 113 | $dbs = $app['dbs']; |
||
| 114 | |||
| 115 | return $dbs[$app['dbs.default']]; |
||
| 116 | }; |
||
| 117 | |||
| 118 | $app['db.config'] = function ($app) { |
||
| 119 | $dbs = $app['dbs.config']; |
||
| 120 | |||
| 121 | return $dbs[$app['dbs.default']]; |
||
| 122 | }; |
||
| 123 | |||
| 124 | $app['db.event_manager'] = function ($app) { |
||
| 125 | $dbs = $app['dbs.event_manager']; |
||
| 126 | |||
| 127 | return $dbs[$app['dbs.default']]; |
||
| 128 | }; |
||
| 129 | } |
||
| 130 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.