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

MemcachedTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 15.49 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 11
loc 71
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C setUp() 11 61 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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