Completed
Pull Request — master (#31)
by
unknown
01:19
created

MemcachedTest::setUp()   C

Complexity

Conditions 10
Paths 19

Size

Total Lines 61

Duplication

Lines 11
Ratio 18.03 %

Importance

Changes 0
Metric Value
dl 11
loc 61
rs 6.9842
c 0
b 0
f 0
cc 10
nc 19
nop 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
4
 * @license    GNU General Public License version 2 or later; see LICENSE
5
 */
6
7
namespace Joomla\Cache\Tests;
8
9
/**
10
 * Tests for the Joomla\Cache\Memcached class.
11
 *
12
 * @since  1.0
13
 */
14
class MemcachedTest extends CacheTest
15
{
16
	/**
17
	 * Setup the tests.
18
	 *
19
	 * @return  void
20
	 *
21
	 * @since   1.0
22
	 */
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)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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