Conditions | 4 |
Paths | 1 |
Total Lines | 59 |
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['watchFunctions'] = function ($c) { |
||
93 | return new Xhgui_WatchFunctions($c['db']); |
||
94 | }; |
||
95 | |||
96 | $this['searcher.mongo'] = function ($c) { |
||
97 | return new Xhgui_Searcher_Mongo($c['db']); |
||
98 | }; |
||
99 | |||
100 | $this['saver'] = function($c) { |
||
101 | return Xhgui_Saver::factory($c['config']); |
||
102 | }; |
||
103 | |||
104 | $this['searcher.pdo'] = function ($c) { |
||
105 | return new Xhgui_Searcher_Pdo($c['saver'], $c['pdo'], $c['config']['pdo']['table']); |
||
106 | }; |
||
107 | |||
108 | $this['searcher'] = function ($c) { |
||
109 | $config = $c['config']; |
||
110 | |||
111 | switch ($config['save.handler']) { |
||
112 | case 'pdo': |
||
113 | return $c['searcher.pdo']; |
||
114 | |||
115 | case 'mongodb': |
||
116 | default: |
||
117 | return $c['searcher.mongo']; |
||
118 | } |
||
119 | }; |
||
120 | |||
121 | $this['saverMongo'] = function($c) { |
||
122 | $config = $c['config']; |
||
123 | $config['save.handler'] = 'mongodb'; |
||
124 | |||
125 | return Xhgui_Saver::factory($config); |
||
126 | }; |
||
127 | } |
||
128 | |||
156 |
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.