| Conditions | 8 |
| Paths | 1 |
| Total Lines | 89 |
| 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 |
||
| 94 | protected function _services() |
||
| 95 | { |
||
| 96 | $this['config'] = Config::all(); |
||
| 97 | |||
| 98 | $this['db'] = static function ($c) { |
||
| 99 | $config = $c['config']; |
||
| 100 | if (empty($config['db.options'])) { |
||
| 101 | $config['db.options'] = []; |
||
| 102 | } |
||
| 103 | if (empty($config['db.driverOptions'])) { |
||
| 104 | $config['db.driverOptions'] = []; |
||
| 105 | } |
||
| 106 | $mongo = new MongoClient($config['db.host'], $config['db.options'], $config['db.driverOptions']); |
||
| 107 | $mongo->{$config['db.db']}->results->findOne(); |
||
| 108 | |||
| 109 | return $mongo->{$config['db.db']}; |
||
| 110 | }; |
||
| 111 | |||
| 112 | $this['pdo'] = static function ($c) { |
||
| 113 | if (!class_exists(PDO::class)) { |
||
| 114 | throw new RuntimeException('Required extension ext-pdo is missing'); |
||
| 115 | } |
||
| 116 | |||
| 117 | $driver = explode(':', $c['config']['pdo']['dsn'], 2)[0]; |
||
| 118 | |||
| 119 | // check the PDO driver is available |
||
| 120 | if (!in_array($driver, PDO::getAvailableDrivers(), true)) { |
||
| 121 | $drivers = implode(',', PDO::getAvailableDrivers()) ?: '(none)'; |
||
| 122 | throw new RuntimeException("Required PDO driver $driver is missing, Available drivers: $drivers"); |
||
| 123 | } |
||
| 124 | |||
| 125 | $options = [ |
||
| 126 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
| 127 | ]; |
||
| 128 | |||
| 129 | if ($driver === 'mysql') { |
||
| 130 | $options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET SQL_MODE=ANSI_QUOTES;'; |
||
| 131 | } |
||
| 132 | |||
| 133 | var_dump($c['config']['pdo']); |
||
| 134 | return new PDO( |
||
| 135 | $c['config']['pdo']['dsn'], |
||
| 136 | $c['config']['pdo']['user'], |
||
| 137 | $c['config']['pdo']['pass'], |
||
| 138 | $options |
||
| 139 | ); |
||
| 140 | }; |
||
| 141 | |||
| 142 | $this[PdoRepository::class] = static function ($c) { |
||
| 143 | return new PdoRepository($c['pdo'], $c['config']['pdo']['table']); |
||
| 144 | }; |
||
| 145 | |||
| 146 | $this['searcher.mongodb'] = static function ($c) { |
||
| 147 | return new MongoSearcher($c['db']); |
||
| 148 | }; |
||
| 149 | |||
| 150 | $this['searcher.pdo'] = static function ($c) { |
||
| 151 | return new PdoSearcher($c[PdoRepository::class]); |
||
| 152 | }; |
||
| 153 | |||
| 154 | $this['searcher'] = static function ($c) { |
||
| 155 | $saver = $c['config']['save.handler']; |
||
| 156 | |||
| 157 | return $c["searcher.$saver"]; |
||
| 158 | }; |
||
| 159 | |||
| 160 | $this['saver.mongodb'] = static function ($c) { |
||
| 161 | $config = $c['config']; |
||
| 162 | |||
| 163 | if (!class_exists(Manager::class)) { |
||
| 164 | throw new RuntimeException('Required extension ext-mongodb missing'); |
||
| 165 | } |
||
| 166 | $mongo = new MongoClient($config['db.host'], $config['db.options'], $config['db.driverOptions']); |
||
| 167 | $collection = $mongo->{$config['db.db']}->results; |
||
| 168 | $collection->findOne(); |
||
| 169 | |||
| 170 | return new Saver\MongoSaver($collection); |
||
| 171 | }; |
||
| 172 | |||
| 173 | $this['saver.pdo'] = static function ($c) { |
||
| 174 | return new Saver\PdoSaver($c[PdoRepository::class]); |
||
| 175 | }; |
||
| 176 | |||
| 177 | $this['saver'] = static function ($c) { |
||
| 178 | $saver = $c['config']['save.handler']; |
||
| 179 | |||
| 180 | return new NormalizingSaver($c["saver.$saver"]); |
||
| 181 | }; |
||
| 182 | } |
||
| 183 | |||
| 214 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: