loadCacheEngines()   C
last analyzed

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