| Conditions | 5 |
| Paths | 1 |
| Total Lines | 58 |
| 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 |
||
| 70 | protected function _services() |
||
| 71 | { |
||
| 72 | $this['config'] = Xhgui_Config::all(); |
||
| 73 | |||
| 74 | $this['db'] = $this->share(function ($c) { |
||
| 75 | $config = $c['config']; |
||
| 76 | if (empty($config['db.options'])) { |
||
| 77 | $config['db.options'] = array(); |
||
| 78 | } |
||
| 79 | if (empty($config['db.driverOptions'])) { |
||
| 80 | $config['db.driverOptions'] = array(); |
||
| 81 | } |
||
| 82 | $mongo = new MongoClient($config['db.host'], $config['db.options'], $config['db.driverOptions']); |
||
| 83 | $mongo->{$config['db.db']}->results->findOne(); |
||
| 84 | |||
| 85 | return $mongo->{$config['db.db']}; |
||
| 86 | }); |
||
| 87 | |||
| 88 | $this['pdo'] = $this->share(function ($c) { |
||
| 89 | return new PDO( |
||
| 90 | $c['config']['pdo']['dsn'], |
||
| 91 | $c['config']['pdo']['pass'], |
||
| 92 | $c['config']['pdo']['user'] |
||
| 93 | ); |
||
| 94 | }); |
||
| 95 | |||
| 96 | $this['searcher.mongo'] = function ($c) { |
||
| 97 | return new Xhgui_Searcher_Mongo($c['db']); |
||
| 98 | }; |
||
| 99 | |||
| 100 | $this['searcher.pdo'] = function ($c) { |
||
| 101 | return new Xhgui_Searcher_Pdo($c['pdo'], $c['config']['pdo']['table']); |
||
| 102 | }; |
||
| 103 | |||
| 104 | $this['searcher'] = function ($c) { |
||
| 105 | $config = $c['config']; |
||
| 106 | |||
| 107 | switch ($config['save.handler']) { |
||
| 108 | case 'pdo': |
||
| 109 | return $c['searcher.pdo']; |
||
| 110 | |||
| 111 | case 'mongodb': |
||
| 112 | default: |
||
| 113 | return $c['searcher.mongo']; |
||
| 114 | } |
||
| 115 | }; |
||
| 116 | |||
| 117 | $this['saver.mongo'] = function ($c) { |
||
| 118 | $config = $c['config']; |
||
| 119 | $config['save.handler'] = 'mongodb'; |
||
| 120 | |||
| 121 | return Xhgui_Saver::factory($config); |
||
| 122 | }; |
||
| 123 | |||
| 124 | $this['saver'] = function ($c) { |
||
| 125 | return Xhgui_Saver::factory($c['config']); |
||
| 126 | }; |
||
| 127 | } |
||
| 128 | |||
| 156 |
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: