| Conditions | 5 |
| Paths | 1 |
| Total Lines | 86 |
| 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 |
||
| 92 | protected function _services() |
||
| 93 | { |
||
| 94 | $this['config'] = Config::all(); |
||
| 95 | |||
| 96 | $this['db'] = static function ($c) { |
||
| 97 | $config = $c['config']; |
||
| 98 | if (empty($config['db.options'])) { |
||
| 99 | $config['db.options'] = []; |
||
| 100 | } |
||
| 101 | if (empty($config['db.driverOptions'])) { |
||
| 102 | $config['db.driverOptions'] = []; |
||
| 103 | } |
||
| 104 | $mongo = new MongoClient($config['db.host'], $config['db.options'], $config['db.driverOptions']); |
||
| 105 | $mongo->{$config['db.db']}->results->findOne(); |
||
| 106 | |||
| 107 | return $mongo->{$config['db.db']}; |
||
| 108 | }; |
||
| 109 | |||
| 110 | $this['pdo'] = static function ($c) { |
||
| 111 | $options = [ |
||
| 112 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
| 113 | ]; |
||
| 114 | |||
| 115 | return new PDO( |
||
| 116 | $c['config']['pdo']['dsn'], |
||
| 117 | $c['config']['pdo']['user'], |
||
| 118 | $c['config']['pdo']['pass'], |
||
| 119 | $options |
||
| 120 | ); |
||
| 121 | }; |
||
| 122 | |||
| 123 | $this['searcher.mongodb'] = static function ($c) { |
||
| 124 | return new MongoSearcher($c['db']); |
||
| 125 | }; |
||
| 126 | |||
| 127 | $this['searcher.pdo'] = static function ($c) { |
||
| 128 | return new PdoSearcher($c['pdo'], $c['config']['pdo']['table']); |
||
| 129 | }; |
||
| 130 | |||
| 131 | $this['searcher'] = static function ($c) { |
||
| 132 | $saver = $c['config']['save.handler']; |
||
| 133 | |||
| 134 | return $c["searcher.$saver"]; |
||
| 135 | }; |
||
| 136 | |||
| 137 | $this['saver.mongodb'] = static function ($c) { |
||
| 138 | $config = $c['config']; |
||
| 139 | |||
| 140 | if (!class_exists(Manager::class)) { |
||
| 141 | throw new RuntimeException("Required extension ext-mongodb missing"); |
||
| 142 | } |
||
| 143 | $mongo = new MongoClient($config['db.host'], $config['db.options'], $config['db.driverOptions']); |
||
| 144 | $collection = $mongo->{$config['db.db']}->results; |
||
| 145 | $collection->findOne(); |
||
| 146 | |||
| 147 | return new Saver\MongoSaver($collection); |
||
| 148 | }; |
||
| 149 | |||
| 150 | $this['saver.pdo'] = static function ($c) { |
||
| 151 | $config = $c['config']; |
||
| 152 | |||
| 153 | if (!class_exists(PDO::class)) { |
||
| 154 | throw new RuntimeException("Required extension ext-pdo is missing"); |
||
| 155 | } |
||
| 156 | |||
| 157 | $options = [ |
||
| 158 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
| 159 | ]; |
||
| 160 | |||
| 161 | return new Saver\PdoSaver( |
||
| 162 | new PDO( |
||
| 163 | $config['pdo']['dsn'], |
||
| 164 | $config['pdo']['user'], |
||
| 165 | $config['pdo']['pass'], |
||
| 166 | $options |
||
| 167 | ), |
||
| 168 | $config['pdo']['table'] |
||
| 169 | ); |
||
| 170 | }; |
||
| 171 | |||
| 172 | $this['saver'] = static function ($c) { |
||
| 173 | $saver = $c['config']['save.handler']; |
||
| 174 | |||
| 175 | return $c["saver.$saver"]; |
||
| 176 | }; |
||
| 177 | } |
||
| 178 | |||
| 209 |
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: