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.

CI3Cache   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 34.48 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 5
c 2
b 1
f 1
lcom 1
cbo 0
dl 20
loc 58
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 10 10 2
A set() 10 10 2

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 namespace Myth\Bay;
2
3
class CI3Cache implements CacheInterface {
4
5
	protected $ci;
6
7
	//--------------------------------------------------------------------
8
9
	public function __construct()
10
	{
11
		$this->ci =& get_instance();
12
	}
13
14
	//--------------------------------------------------------------------
15
16
17
	/**
18
	 * Provides a way to retrieve a single item from
19
	 * the cache.
20
	 *
21
	 * Used by the Bay to get the latest cached version of
22
	 * the view.
23
	 *
24
	 * @param $key
25
	 * @return string|false
26
	 */
27 View Code Duplication
	public function get($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...
28
	{
29
		// Does CI even have a cache engine loaded?
30
		if (! is_object($this->ci->cache))
31
		{
32
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Myth\Bay\CacheInterface::get of type string|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
33
		}
34
35
		return $this->ci->cache->get($key);
36
	}
37
38
	//--------------------------------------------------------------------
39
40
	/**
41
	 * Provides a way to set a single cache item.
42
	 *
43
	 * @param string $key
44
	 * @param string $content
45
	 * @param int $ttl          // Time in _minutes_
46
	 * @return mixed
47
	 */
48 View Code Duplication
	public function set($key, $content, $ttl)
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...
49
	{
50
		// Does CI even have a cache engine loaded?
51
		if (! is_object($this->ci->cache))
52
		{
53
			return true;
54
		}
55
56
		return $this->ci->cache->save($key, $content, $ttl * 60);
57
	}
58
59
	//--------------------------------------------------------------------
60
}