| Conditions | 10 |
| Paths | 14 |
| Total Lines | 43 |
| Code Lines | 32 |
| 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 |
||
| 11 | public function run($request) |
||
| 12 | { |
||
| 13 | $cache = SS_Cache::factory('foo'); |
||
| 14 | $result = $cache->load('bar'); |
||
| 15 | if (!$result || isset($_GET['reload'])) { |
||
| 16 | $time = time(); |
||
| 17 | for ($i = 1; $i < $time; $i = $i + 75) { |
||
| 18 | $temp = $time / $time - rand(0, 10); |
||
| 19 | } |
||
| 20 | $result = date('Y-m-d H:i:s'); |
||
| 21 | ; |
||
| 22 | DB::alteration_message('not from cache: '.$result, 'deleted'); |
||
| 23 | $cache->save($result, 'bar'); |
||
| 24 | } else { |
||
| 25 | DB::alteration_message('from cache: '.$result, 'created'); |
||
| 26 | } |
||
| 27 | |||
| 28 | if (isset($_GET['setm'])) { |
||
| 29 | foreach (array('11211', '11212', '11213', '11214') as $port) { |
||
| 30 | echo "<h1>SETTING: $port</h1>"; |
||
| 31 | $memcache = new Memcache; |
||
| 32 | $cacheAvailable = $memcache->connect('127.0.0.1', $port); |
||
| 33 | if ($cacheAvailable) { |
||
| 34 | $memcache->set('test_memcache', 'set at: '.date('Y-m-d H:i:s')); |
||
| 35 | echo "SET"; |
||
| 36 | } else { |
||
| 37 | echo "NOT SET"; |
||
| 38 | } |
||
| 39 | } |
||
| 40 | } elseif (isset($_GET['getm'])) { |
||
| 41 | foreach (array('11211', '11212', '11213', '11214') as $port) { |
||
| 42 | echo "<h1>GETTING: $port</h1>"; |
||
| 43 | $memcache = new Memcache; |
||
| 44 | $cacheAvailable = $memcache->connect('127.0.0.1', $port); |
||
| 45 | if ($cacheAvailable) { |
||
| 46 | $outcome = $memcache->get('test_memcache'); |
||
| 47 | echo $outcome; |
||
| 48 | } else { |
||
| 49 | echo "COULD NOT GET"; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 |
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.