GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 92c79f...f4bfda )
by gyeong-won
06:35
created

CacheHandler::CacheHandler()   C

Complexity

Conditions 15
Paths 68

Size

Total Lines 64

Duplication

Lines 33
Ratio 51.56 %

Importance

Changes 0
Metric Value
cc 15
nc 68
nop 3
dl 33
loc 64
rs 5.9166
c 0
b 0
f 0

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
/* Copyright (C) NAVER <http://www.navercorp.com> */
3
4
/**
5
 * CacheHandler
6
 *
7
 * @author NAVER ([email protected])
8
 */
9
class CacheHandler extends Handler
10
{
11
	/**
12
	 * instance of cache handler
13
	 * @var CacheBase
14
	 */
15
	var $handler = null;
16
17
	/**
18
	 * Version of key group
19
	 * @var int
20
	 */
21
	var $keyGroupVersions = null;
22
23
	/**
24
	 * Get a instance of CacheHandler(for singleton)
25
	 *
26
	 * @param string $target type of cache (object|template)
27
	 * @param object $info info. of DB
28
	 * @param boolean $always_use_file If set true, use a file cache always
29
	 * @return CacheHandler
30
	 */
31
	function &getInstance($target = 'object', $info = null, $always_use_file = false)
32
	{
33
		$cache_handler_key = $target . ($always_use_file ? '_file' : '');
34
		if(!$GLOBALS['__XE_CACHE_HANDLER__'][$cache_handler_key])
35
		{
36
			$GLOBALS['__XE_CACHE_HANDLER__'][$cache_handler_key] = new CacheHandler($target, $info, $always_use_file);
37
		}
38
		return $GLOBALS['__XE_CACHE_HANDLER__'][$cache_handler_key];
39
	}
40
41
	/**
42
	 * Constructor.
43
	 *
44
	 * Do not use this directly. You can use getInstance() instead.
45
	 *
46
	 * @see CacheHandler::getInstance
47
	 * @param string $target type of cache (object|template)
48
	 * @param object $info info. of DB
49
	 * @param boolean $always_use_file If set true, use a file cache always
50
	 * @return CacheHandler
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
51
	 */
52
	function __construct($target, $info = null, $always_use_file = false)
53
	{
54
		if(!$info)
55
		{
56
			$info = Context::getDBInfo();
57
		}
58
59
		if($info)
60
		{
61
			if($target == 'object')
62
			{
63
				if($info->use_object_cache == 'apc')
64
				{
65
					$type = 'apc';
66
				}
67 View Code Duplication
				else if(substr($info->use_object_cache, 0, 8) == 'memcache')
68
				{
69
					$type = 'memcache';
70
					$url = $info->use_object_cache;
71
				}
72
				else if($info->use_object_cache == 'wincache')
73
				{
74
					$type = 'wincache';
75
				}
76
				else if($info->use_object_cache == 'file')
77
				{
78
					$type = 'file';
79
				}
80
				else if($always_use_file)
81
				{
82
					$type = 'file';
83
				}
84
			}
85 View Code Duplication
			else if($target == 'template')
86
			{
87
				if($info->use_template_cache == 'apc')
88
				{
89
					$type = 'apc';
90
				}
91
				else if(substr($info->use_template_cache, 0, 8) == 'memcache')
92
				{
93
					$type = 'memcache';
94
					$url = $info->use_template_cache;
95
				}
96
				else if($info->use_template_cache == 'wincache')
97
				{
98
					$type = 'wincache';
99
				}
100
			}
101
102
			if($type)
103
			{
104
				$class = 'Cache' . ucfirst($type);
0 ignored issues
show
Bug introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
105
				include_once sprintf('%sclasses/cache/%s.class.php', _XE_PATH_, $class);
106
				$this->handler = call_user_func(array($class, 'getInstance'), $url);
0 ignored issues
show
Bug introduced by
The variable $url does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
107
				$this->keyGroupVersions = $this->handler->get('key_group_versions', 0);
108
				if(!$this->keyGroupVersions)
109
				{
110
					$this->keyGroupVersions = array();
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type integer of property $keyGroupVersions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
111
					$this->handler->put('key_group_versions', $this->keyGroupVersions, 0);
112
				}
113
			}
114
		}
115
	}
116
117
	/**
118
	 * Return whether support or not support cache
119
	 *
120
	 * @return boolean
121
	 */
122
	function isSupport()
123
	{
124
		if($this->handler && $this->handler->isSupport())
125
		{
126
			return true;
127
		}
128
129
		return false;
130
	}
131
132
	/**
133
	 * Get cache name by key
134
	 *
135
	 * @param string $key The key that will be associated with the item.
136
	 * @return string Returns cache name
137
	 */
138
	function getCacheKey($key)
139
	{
140
		$key = str_replace('/', ':', $key);
141
142
		return __XE_VERSION__ . ':' . $key;
143
	}
144
145
	/**
146
	 * Get cached data
147
	 *
148
	 * @param string $key Cache key
149
	 * @param int $modified_time 	Unix time of data modified.
150
	 * 								If stored time is older then modified time, return false.
151
	 * @return false|mixed Return false on failure or older then modified time. Return the string associated with the $key on success.
152
	 */
153 View Code Duplication
	function get($key, $modified_time = 0)
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...
154
	{
155
		if(!$this->handler)
156
		{
157
			return false;
158
		}
159
160
		$key = $this->getCacheKey($key);
161
162
		return $this->handler->get($key, $modified_time);
163
	}
164
165
	/**
166
	 * Put data into cache
167
	 *
168
	 * @param string $key Cache key
169
	 * @param mixed $obj	Value of a variable to store. $value supports all data types except resources, such as file handlers.
170
	 * @param int $valid_time	Time for the variable to live in the cache in seconds.
171
	 * 							After the value specified in ttl has passed the stored variable will be deleted from the cache.
172
	 * 							If no ttl is supplied, use the default valid time.
173
	 * @return bool|void Returns true on success or false on failure. If use CacheFile, returns void.
174
	 */
175
	function put($key, $obj, $valid_time = 0)
176
	{
177
		if(!$this->handler && !$key)
178
		{
179
			return false;
180
		}
181
182
		$key = $this->getCacheKey($key);
183
184
		return $this->handler->put($key, $obj, $valid_time);
185
	}
186
187
	/**
188
	 * Delete Cache
189
	 *
190
	 * @param string $key Cache key
191
	 * @return void
192
	 */
193 View Code Duplication
	function delete($key)
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...
194
	{
195
		if(!$this->handler)
196
		{
197
			return false;
198
		}
199
200
		$key = $this->getCacheKey($key);
201
202
		return $this->handler->delete($key);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class CacheBase as the method delete() does only exist in the following sub-classes of CacheBase: CacheApc, CacheFile, CacheMemcache, CacheWincache. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
203
	}
204
205
	/**
206
	 * Return whether cache is valid or invalid
207
	 *
208
	 * @param string $key Cache key
209
	 * @param int $modified_time 	Unix time of data modified.
210
	 * 								If stored time is older then modified time, the data is invalid.
211
	 * @return bool Return true on valid or false on invalid.
212
	 */
213 View Code Duplication
	function isValid($key, $modified_time = 0)
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...
214
	{
215
		if(!$this->handler)
216
		{
217
			return false;
218
		}
219
220
		$key = $this->getCacheKey($key);
221
222
		return $this->handler->isValid($key, $modified_time);
223
	}
224
225
	/**
226
	 * Truncate all cache
227
	 *
228
	 * @return bool|void Returns true on success or false on failure. If use CacheFile, returns void.
229
	 */
230
	function truncate()
231
	{
232
		if(!$this->handler)
233
		{
234
			return false;
235
		}
236
237
		return $this->handler->truncate();
238
	}
239
240
	/**
241
	 * Function used for generating keys for similar objects.
242
	 *
243
	 * Ex: 1:document:123
244
	 *     1:document:777
245
	 *
246
	 * This allows easily removing all object of type "document"
247
	 * from cache by simply invalidating the group key.
248
	 *
249
	 * The new key will be 2:document:123, thus forcing the document
250
	 * to be reloaded from the database.
251
	 *
252
	 * @param string $keyGroupName Group name
253
	 * @param string $key Cache key
254
	 * @return string
255
	 */
256
	function getGroupKey($keyGroupName, $key)
257
	{
258
		if(!$this->keyGroupVersions[$keyGroupName])
259
		{
260
			$this->keyGroupVersions[$keyGroupName] = 1;
261
			$this->handler->put('key_group_versions', $this->keyGroupVersions, 0);
262
		}
263
264
		return 'cache_group_' . $this->keyGroupVersions[$keyGroupName] . ':' . $keyGroupName . ':' . $key;
265
	}
266
267
	/**
268
	 * Make invalid group key (like delete group key)
269
	 *
270
	 * @param string $keyGroupName Group name
271
	 * @return void
272
	 */
273
	function invalidateGroupKey($keyGroupName)
274
	{
275
		$this->keyGroupVersions[$keyGroupName]++;
276
		$this->handler->put('key_group_versions', $this->keyGroupVersions, 0);
277
	}
278
279
}
280
281
/**
282
 * Base class of Cache
283
 *
284
 * @author NAVER ([email protected])
285
 */
286
class CacheBase
287
{
288
	/**
289
	 * Default valid time
290
	 * @var int
291
	 */
292
	var $valid_time = 36000;
293
294
	/**
295
	 * Get cached data
296
	 *
297
	 * @param string $key Cache key
298
	 * @param int $modified_time 	Unix time of data modified.
299
	 * 								If stored time is older then modified time, return false.
300
	 * @return false|mixed Return false on failure or older then modified time. Return the string associated with the $key on success.
301
	 */
302
	function get($key, $modified_time = 0)
303
	{
304
		return false;
305
	}
306
307
	/**
308
	 * Put data into cache
309
	 *
310
	 * @param string $key Cache key
311
	 * @param mixed $obj	Value of a variable to store. $value supports all data types except resources, such as file handlers.
312
	 * @param int $valid_time	Time for the variable to live in the cache in seconds.
313
	 * 							After the value specified in ttl has passed the stored variable will be deleted from the cache.
314
	 * 							If no ttl is supplied, use the default valid time.
315
	 * @return bool|void Returns true on success or false on failure. If use CacheFile, returns void.
316
	 */
317
	function put($key, $obj, $valid_time = 0)
318
	{
319
		return false;
320
	}
321
322
	/**
323
	 * Return whether cache is valid or invalid
324
	 *
325
	 * @param string $key Cache key
326
	 * @param int $modified_time 	Unix time of data modified.
327
	 * 								If stored time is older then modified time, the data is invalid.
328
	 * @return bool Return true on valid or false on invalid.
329
	 */
330
	function isValid($key, $modified_time = 0)
331
	{
332
		return false;
333
	}
334
335
	/**
336
	 * Return whether support or not support cache
337
	 *
338
	 * @return boolean
339
	 */
340
	function isSupport()
341
	{
342
		return false;
343
	}
344
345
	/**
346
	 * Truncate all cache
347
	 *
348
	 * @return bool|void Returns true on success or false on failure. If use CacheFile, returns void.
349
	 */
350
	function truncate()
351
	{
352
		return false;
353
	}
354
355
}
356
/* End of file CacheHandler.class.php */
357
/* Location: ./classes/cache/CacheHandler.class.php */
358