Passed
Push — development ( 8f3e46...8433eb )
by Spuds
01:17 queued 31s
created

loadCacheEngines()   C

Complexity

Conditions 13
Paths 134

Size

Total Lines 60
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 13.0412

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 28
dl 0
loc 60
rs 6.3333
c 1
b 1
f 0
cc 13
nc 134
nop 1
ccs 15
cts 16
cp 0.9375
crap 13.0412

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
/**
4
 * This file contains functions that deal with getting and setting cache values.
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * This file contains code covered by:
11
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
12
 *
13
 * @version 2.0 dev
14
 *
15
 */
16
17
use ElkArte\Cache\Cache;
18
use ElkArte\Cache\CacheMethod\AbstractCacheMethod;
19
20
/**
21
 * Try to retrieve a cache entry. On failure, call the appropriate function.
22
 * This callback is sent as $file to include, and $function to call, with
23
 * $params parameters.
24
 *
25
 * @param string $key cache entry key
26
 * @param string $file file to include
27
 * @param string $function function to call
28
 * @param array $params parameters sent to the function
29
 * @param int $level = 1
30
 *
31
 * @return mixed
32
 * @deprecated since 2.0
33
 *
34
 */
35
function cache_quick_get($key, $file, $function, $params, $level = 1)
36
{
37
	\ElkArte\Errors\Errors::instance()->log_deprecated('cache_quick_get()', '\\ElkArte\\Cache\\Cache::instance()->quick_get');
0 ignored issues
show
Bug introduced by
The type ElkArte\Errors\Errors was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
39
	return Cache::instance()->quick_get($key, $file, $function, $params, $level);
40
}
41
42
/**
43
 * Puts value in the cache under key for ttl seconds.
44
 *
45
 * - It may "miss" so shouldn't be depended on
46
 * - Uses the cache engine chosen in the ACP and saved in settings.php
47
 * - It supports Memcache, Memcached, APCu, Zend, Redis & Filebased engines
48
 *
49
 * @param string $key
50
 * @param string|int|array|null $value
51
 * @param int $ttl = 120
52
 * @deprecated since 2.0
53
 *
54
 */
55
function cache_put_data($key, $value, $ttl = 120)
56
{
57
	\ElkArte\Errors\Errors::instance()->log_deprecated('cache_put_data()', '\\ElkArte\\Cache\\Cache::instance()->put');
58
	Cache::instance()->put($key, $value, $ttl);
59
}
60
61
/**
62
 * Gets the value from the cache specified by key, so long as it is not older than ttl seconds.
63
 *
64
 * - It may often "miss", so shouldn't be depended on.
65
 * - It supports the same as \ElkArte\Cache\Cache::instance()->put().
66
 *
67
 * @param string $key
68
 * @param int $ttl = 120
69
 *
70
 * @return bool|null
71
 * @deprecated since 2.0
72
 *
73
 */
74
function cache_get_data($key, $ttl = 120)
75
{
76
	\ElkArte\Errors\Errors::instance()->log_deprecated('cache_get_data()', '\\ElkArte\\Cache\\Cache::instance()->get');
77
78
	return Cache::instance()->get($key, $ttl);
79
}
80
81
/**
82
 * Empty out the cache in use as best it can
83
 *
84
 * It may only remove the files of a certain type (if the $type parameter is given)
85
 * Type can be user, data or left blank
86
 *  - user clears out user data
87
 *  - data clears out system / opcode data
88
 *  - If no type is specified will perform a complete cache clearing
89
 * For cache engines that do not distinguish on types, a full cache flush will be done
90
 *
91
 * @param string $type = ''
92
 * @deprecated since 2.0
93
 *
94
 */
95
function clean_cache($type = '')
96
{
97
	\ElkArte\Errors\Errors::instance()->log_deprecated('clean_cache()', '\\ElkArte\\Cache\\Cache::instance()->clean');
98
	Cache::instance()->clean($type);
99
}
100
101
/**
102
 * Finds all the caching engines available and loads some details depending on
103
 * parameters.
104
 *
105
 * - Caching engines must follow the naming convention of CacheName.php and
106
 * have a class name of CacheName that extends AbstractCacheMethod
107
 *
108
 * @param bool $supported_only If true, for each engine supported by the server
109
 *             an array with 'title' and 'version' is returned.
110
 *             If false, for each engine available an array with 'title' (string)
111
 *             and 'supported' (bool) is returned.
112
 *
113
 * @return array
114
 */
115
function loadCacheEngines($supported_only = true)
116
{
117
	global $cache_servers, $cache_uid, $cache_password;
118
119
	$engines = [];
120
121
	$classes = new GlobIterator(SOURCEDIR . '/ElkArte/Cache/CacheMethod/*.php', FilesystemIterator::SKIP_DOTS);
122
123
	$current = '';
124 2
	$cache = Cache::instance();
125
	if ($cache->isEnabled())
126 2
	{
127
		$current = $cache->getAccelerator();
128 2
	}
129
130
	foreach ($classes as $file_path)
131 2
	{
132 2
		// Get the engine name from the file name
133 2
		$parts = explode('.', $file_path->getBasename());
134
		$engine_name = $parts[0];
135 2
		if (in_array($engine_name, ['AbstractCacheMethod', 'CacheMethodInterface.php']))
136
		{
137 2
		   	continue;
138
		}
139
		$class = '\\ElkArte\\Cache\\CacheMethod\\' . $engine_name;
140 2
141
		// Validate the class name exists
142 2
		if (class_exists($class))
143 2
		{
144
			$options = [
145 2
				'servers' => empty($cache_servers) ? [] : explode(',', $cache_servers),
146
				'cache_uid' => empty($cache_uid) ? '' : $cache_uid,
147
				'cache_password' => empty($cache_password) ? '' : $cache_password,
148
			];
149 2
150
			// Use the current Cache object if its been enabled
151 2
			if ($engine_name === $current)
152
			{
153
				$obj = $cache->getCacheEngine();
154
			}
155
			else
156
			{
157 2
				$obj = new $class($options);
158
			}
159
160
			if ($obj instanceof AbstractCacheMethod)
161
			{
162
				if ($supported_only && $obj->isAvailable())
163
				{
164
					$engines[strtolower($engine_name)] = $obj->details();
165
				}
166
				elseif ($supported_only === false)
167
				{
168
					$engines[strtolower($engine_name)] = $obj;
169
				}
170
			}
171
		}
172
	}
173
174
	return $engines;
175
}
176