Conditions | 10 |
Paths | 19 |
Total Lines | 61 |
Lines | 11 |
Ratio | 18.03 % |
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 |
||
23 | public function setUp() |
||
24 | { |
||
25 | if (!class_exists('Memcached')) |
||
26 | { |
||
27 | $this->markTestSkipped( |
||
28 | 'The Memcached class does not exist.' |
||
29 | ); |
||
30 | |||
31 | return; |
||
32 | } |
||
33 | |||
34 | // Parse the DSN details for the test server |
||
35 | $dsn = defined('JTEST_CACHE_MEMCACHED_DSN') ? JTEST_CACHE_MEMCACHED_DSN : getenv('JTEST_CACHE_MEMCACHED_DSN'); |
||
36 | |||
37 | if ($dsn) |
||
38 | { |
||
39 | $options = array(); |
||
40 | |||
41 | // First let's trim the memcached: part off the front of the DSN if it exists. |
||
42 | if (strpos($dsn, 'memcached:') === 0) |
||
43 | { |
||
44 | $dsn = substr($dsn, 10); |
||
45 | } |
||
46 | |||
47 | if (!is_array($options)) |
||
48 | { |
||
49 | $options = array($options); |
||
50 | } |
||
51 | |||
52 | // Split the DSN into its parts over semicolons. |
||
53 | $parts = explode(';', $dsn); |
||
54 | |||
55 | if (!isset($options['memcache.servers'])) |
||
56 | { |
||
57 | $server = new \stdClass; |
||
58 | |||
59 | // Parse each part and populate the options array. |
||
60 | View Code Duplication | foreach ($parts as $part) |
|
|
|||
61 | { |
||
62 | list ($k, $v) = explode('=', $part, 2); |
||
63 | switch ($k) |
||
64 | { |
||
65 | case 'host': |
||
66 | case 'port': |
||
67 | $server->$k = $v; |
||
68 | break; |
||
69 | } |
||
70 | } |
||
71 | $options['memcache.servers'] = array($server); |
||
72 | } |
||
73 | |||
74 | $this->cacheOptions = $options; |
||
75 | } |
||
76 | else |
||
77 | { |
||
78 | $this->markTestSkipped('No configuration for Memcached given'); |
||
79 | } |
||
80 | |||
81 | $this->cacheClass = 'Joomla\\Cache\\Memcached'; |
||
82 | parent::setUp(); |
||
83 | } |
||
84 | } |
||
85 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.