| Conditions | 7 |
| Paths | 3 |
| Total Lines | 53 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 16 | function buildGanesha($storage) |
||
| 17 | { |
||
| 18 | switch ($storage) { |
||
| 19 | case 'redis': |
||
| 20 | $redis = new \Redis(); |
||
| 21 | $redis->connect(getenv('GANESHA_EXAMPLE_REDIS') ?: 'localhost'); |
||
| 22 | $adapter = new Ackintosh\Ganesha\Storage\Adapter\Redis($redis); |
||
| 23 | break; |
||
| 24 | case 'memcached': |
||
| 25 | $m = new \Memcached(); |
||
| 26 | $m->addServer(getenv('GANESHA_EXAMPLE_MEMCACHED') ?: 'localhost', 11211); |
||
| 27 | $adapter = new \Ackintosh\Ganesha\Storage\Adapter\Memcached($m); |
||
| 28 | break; |
||
| 29 | default: |
||
| 30 | throw new \InvalidArgumentException(); |
||
| 31 | break; |
||
| 32 | } |
||
| 33 | |||
| 34 | $ganesha = Builder::build([ |
||
| 35 | 'adapter' => $adapter, |
||
| 36 | 'timeWindow' => TIME_WINDOW, |
||
| 37 | 'failureRateThreshold' => FAILURE_RATE, |
||
| 38 | 'minimumRequests' => MINIMUM_REQUESTS, |
||
| 39 | 'intervalToHalfOpen' => INTERVAL_TO_HALF_OPEN, |
||
| 40 | ]); |
||
| 41 | |||
| 42 | $messageOnTripped = <<<__EOS__ |
||
| 43 | !!!!!!!!!!!!!!!!!!!!!!! |
||
| 44 | !!!!!!! TRIPPED !!!!!!! |
||
| 45 | !!!!!!!!!!!!!!!!!!!!!!! |
||
| 46 | |||
| 47 | __EOS__; |
||
| 48 | $messageOnCalmedDown = <<<__EOS__ |
||
| 49 | ======================= |
||
| 50 | ===== CALMED DOWN ===== |
||
| 51 | ======================= |
||
| 52 | |||
| 53 | __EOS__; |
||
| 54 | |||
| 55 | $ganesha->subscribe(function ($event, $service, $message) use ($messageOnTripped, $messageOnCalmedDown) { |
||
| 56 | switch ($event) { |
||
| 57 | case Ganesha::EVENT_TRIPPED: |
||
| 58 | echo $messageOnTripped; |
||
| 59 | break; |
||
| 60 | case Ganesha::EVENT_CALMED_DOWN: |
||
| 61 | echo $messageOnCalmedDown; |
||
| 62 | break; |
||
| 63 | default: |
||
| 64 | break; |
||
| 65 | } |
||
| 66 | }); |
||
| 67 | |||
| 68 | return $ganesha; |
||
| 69 | } |
||
| 70 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.