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

MemcachedTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 14.47 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
C setUp() 11 66 11

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 = $this->cacheOptions;
40
41
			if (!$options)
0 ignored issues
show
Bug Best Practice introduced by
The expression $options of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
			{
43
				$options = array();
44
			}
45
46
			// First let's trim the memcached: part off the front of the DSN if it exists.
47
			if (strpos($dsn, 'memcached:') === 0)
48
			{
49
				$dsn = substr($dsn, 10);
50
			}
51
52
			if (!is_array($options))
53
			{
54
				$options = array($options);
55
			}
56
57
			// Split the DSN into its parts over semicolons.
58
			$parts = explode(';', $dsn);
59
60
			if (!isset($options['memcache.servers']))
61
			{
62
				$server = new \stdClass;
63
64
				// Parse each part and populate the options array.
65 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...
66
				{
67
					list ($k, $v) = explode('=', $part, 2);
68
					switch ($k)
69
					{
70
						case 'host':
71
						case 'port':
72
							$server->$k = $v;
73
							break;
74
					}
75
				}
76
				$options['memcache.servers'] = array($server);
77
			}
78
79
			$this->cacheOptions = $options;
80
		}
81
		else
82
		{
83
			$this->markTestSkipped('No configuration for Memcached given');
84
		}
85
86
		$this->cacheClass = 'Joomla\\Cache\\Memcached';
87
		parent::setUp();
88
	}
89
}
90