| Conditions | 11 |
| Paths | 6 |
| Total Lines | 45 |
| 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 |
||
| 18 | public static function factory($config) |
||
| 19 | { |
||
| 20 | switch ($config['save.handler']) { |
||
| 21 | case 'file': |
||
| 22 | return new Xhgui_Saver_File( |
||
| 23 | $config['save.handler.path'], |
||
| 24 | !empty($config['save.handler.filename']) ? $config['save.handler.filename'] : null, |
||
| 25 | $config['save.handler.separate_meta'] |
||
| 26 | ); |
||
| 27 | |||
| 28 | case 'upload': |
||
| 29 | $timeout = 3; |
||
| 30 | if (isset($config['save.handler.upload.timeout'])) { |
||
| 31 | $timeout = $config['save.handler.upload.timeout']; |
||
| 32 | } |
||
| 33 | return new Xhgui_Saver_Upload( |
||
| 34 | $config['save.handler.upload.uri'], |
||
| 35 | $timeout |
||
| 36 | ); |
||
| 37 | |||
| 38 | case 'pdo': |
||
| 39 | return new Xhgui_Saver_Pdo( |
||
| 40 | $config['db.dsn'], |
||
| 41 | (!empty($config['db.user'])) ? $config['db.user'] : null, |
||
| 42 | (!empty($config['db.password'])) ? $config['db.password'] : null, |
||
| 43 | $config['db.options'] |
||
| 44 | ); |
||
| 45 | break; |
||
|
|
|||
| 46 | |||
| 47 | case 'mongodb': |
||
| 48 | default: |
||
| 49 | $mongo = new MongoClient( |
||
| 50 | $config['db.host'], |
||
| 51 | $config['db.options'] + |
||
| 52 | array( |
||
| 53 | 'username' => (!empty($config['db.user'])) ? $config['db.user'] : null, |
||
| 54 | 'password' => (!empty($config['db.password'])) ? $config['db.password'] : null, |
||
| 55 | ) |
||
| 56 | ); |
||
| 57 | |||
| 58 | $collection = $mongo->{$config['db.db']}->results; |
||
| 59 | $collection->findOne(); |
||
| 60 | return new Xhgui_Saver_Mongo($collection); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 77 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.