Completed
Push — release-2.1 ( 4bdd41...11eb43 )
by Mathias
07:36
created

memcached_cache   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 135
Duplicated Lines 31.11 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 42
loc 135
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isSupported() 10 10 4
C connect() 5 38 8
A getData() 11 11 2
A putData() 0 6 1
A cleanCache() 0 7 1
A quit() 0 4 1
A cacheSettings() 16 16 2
A getVersion() 0 4 1

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
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines http://www.simplemachines.org
8
 * @copyright 2017 Simple Machines and individual contributors
9
 * @license http://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 Beta 4
12
 */
13
14
if (!defined('SMF'))
15
	die('Hacking attempt...');
16
17
/**
18
 * Our Cache API class
19
 * @package cacheAPI
20
 */
21
class memcached_cache extends cache_api
22
{
23
	/**
24
	 * @var \Memcached The memcache instance.
25
	 */
26
	private $memcached = null;
27
28
	/**
29
	 * {@inheritDoc}
30
	 */
31 View Code Duplication
	public function isSupported($test = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
32
	{
33
		global $cache_memcached;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
34
35
		$supported = class_exists('memcached');
36
37
		if ($test)
38
			return $supported;
39
		return parent::isSupported() && $supported && !empty($cache_memcached);
40
	}
41
42
	/**
43
	 * {@inheritDoc}
44
	 */
45
	public function connect()
46
	{
47
		global $cache_memcached;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
48
49
		$servers = explode(',', $cache_memcached);
50
51
		// memcached does not remove servers from the list upon completing the script under modes like FastCGI. So check to see if servers exist or not.
52
		$this->memcached = new Memcached;
53
		$currentServers = $this->memcached->getServerList();
54
		foreach ($servers as $server)
55
		{
56
			if (strpos($server,'/') !== false)
57
				$tempServer = array($server, 0);
58 View Code Duplication
			else
59
			{
60
				$server = explode(':', $server);
61
				$tempServer = array($server[0], isset($server[1]) ? $server[1] : 11211);
62
			}
63
64
			// Figure out if we have this server or not
65
			$foundServer = false;
66
			foreach ($currentServers as $currentServer)
67
			{
68
				if ($tempServer[0] == $currentServer['host'] && $tempServer[1] == $currentServer['port'])
69
				{
70
					$foundServer = true;
71
					break;
72
				}
73
			}
74
75
			// Found it?
76
			if (empty($foundServer))
77
				$this->memcached->addServer($tempServer[0], $tempServer[1]);
78
		}
79
80
		// Best guess is this worked.
81
		return true;
82
	}
83
84
	/**
85
	 * {@inheritDoc}
86
	 */
87 View Code Duplication
	public function getData($key, $ttl = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
88
	{
89
		$key = $this->prefix . strtr($key, ':/', '-_');
90
91
		$value = $this->memcached->get($key);
92
93
		// $value should return either data or false (from failure, key not found or empty array).
94
		if ($value === false)
95
			return null;
96
		return $value;
97
	}
98
99
	/**
100
	 * {@inheritDoc}
101
	 */
102
	public function putData($key, $value, $ttl = null)
103
	{
104
		$key = $this->prefix . strtr($key, ':/', '-_');
105
106
		return $this->memcached->set($key, $value, $ttl);
107
	}
108
109
	/**
110
	 * {@inheritDoc}
111
	 */
112
	public function cleanCache($type = '')
113
	{
114
		$this->invalidateCache();
115
116
		// Memcached accepts a delay parameter, always use 0 (instant).
117
		return $this->memcached->flush(0);
118
	}
119
120
	/**
121
	 * {@inheritDoc}
122
	 */
123
	public function quit()
124
	{
125
		return $this->memcached->quit();
126
	}
127
128
	/**
129
	 * {@inheritDoc}
130
	 */
131 View Code Duplication
	public function cacheSettings(array &$config_vars)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
132
	{
133
		global $context, $txt;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
134
135
		$config_vars[] = $txt['cache_memcache_settings'];
136
		$config_vars[] = array('cache_memcached', $txt['cache_memcache_servers'], 'file', 'text', 0, 'cache_memcached', 'postinput' => '<br><div class="smalltext"><em>' . $txt['cache_memcache_servers_subtext'] . '</em></div>');
137
138
		if (!isset($context['settings_post_javascript']))
139
			$context['settings_post_javascript'] = '';
140
141
		$context['settings_post_javascript'] .= '
142
			$("#cache_accelerator").change(function (e) {
143
				var cache_type = e.currentTarget.value;
144
				$("#cache_memcached").prop("disabled", cache_type != "memcached");
145
			});';
146
	}
147
148
	/**
149
	 * {@inheritDoc}
150
	 */
151
	public function getVersion()
152
	{
153
		return $this->memcached->getVersion();
154
	}
155
}
156
157
?>