| Conditions | 4 |
| Paths | 1 |
| Total Lines | 55 |
| 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 |
||
| 69 | protected function _services() |
||
| 70 | { |
||
| 71 | $this['config'] = Xhgui_Config::all(); |
||
| 72 | |||
| 73 | $this['db'] = $this->share(function ($c) { |
||
| 74 | $config = $c['config']; |
||
| 75 | if (empty($config['db.options'])) { |
||
| 76 | $config['db.options'] = array(); |
||
| 77 | } |
||
| 78 | $mongo = new MongoClient($config['db.host'], $config['db.options']); |
||
| 79 | $mongo->{$config['db.db']}->results->findOne(); |
||
| 80 | |||
| 81 | return $mongo->{$config['db.db']}; |
||
| 82 | }); |
||
| 83 | |||
| 84 | $this['pdo'] = $this->share(function ($c) { |
||
| 85 | return new PDO( |
||
| 86 | $c['config']['pdo']['dsn'], |
||
| 87 | $c['config']['pdo']['pass'], |
||
| 88 | $c['config']['pdo']['user'] |
||
| 89 | ); |
||
| 90 | }); |
||
| 91 | |||
| 92 | $this['searcher.mongo'] = function ($c) { |
||
| 93 | return new Xhgui_Searcher_Mongo($c['db']); |
||
| 94 | }; |
||
| 95 | |||
| 96 | $this['searcher.pdo'] = function ($c) { |
||
| 97 | return new Xhgui_Searcher_Pdo($c['pdo'], $c['config']['pdo']['table']); |
||
| 98 | }; |
||
| 99 | |||
| 100 | $this['searcher'] = function ($c) { |
||
| 101 | $config = $c['config']; |
||
| 102 | |||
| 103 | switch ($config['save.handler']) { |
||
| 104 | case 'pdo': |
||
| 105 | return $c['searcher.pdo']; |
||
| 106 | |||
| 107 | case 'mongodb': |
||
| 108 | default: |
||
| 109 | return $c['searcher.mongo']; |
||
| 110 | } |
||
| 111 | }; |
||
| 112 | |||
| 113 | $this['saver.mongo'] = function($c) { |
||
| 114 | $config = $c['config']; |
||
| 115 | $config['save.handler'] = 'mongodb'; |
||
| 116 | |||
| 117 | return Xhgui_Saver::factory($config); |
||
| 118 | }; |
||
| 119 | |||
| 120 | $this['saver'] = function($c) { |
||
| 121 | return Xhgui_Saver::factory($c['config']); |
||
| 122 | }; |
||
| 123 | } |
||
| 124 | |||
| 152 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.