WP_Feed_Cache::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Feed API: WP_Feed_Cache class
4
 *
5
 * @package WordPress
6
 * @subpackage Feed
7
 * @since 4.7.0
8
 */
9
10
/**
11
 * Core class used to implement a feed cache.
12
 *
13
 * @since 2.8.0
14
 *
15
 * @see SimplePie_Cache
16
 */
17
class WP_Feed_Cache extends SimplePie_Cache {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
18
19
	/**
20
	 * Creates a new SimplePie_Cache object.
21
	 *
22
	 * @since 2.8.0
23
	 * @access public
24
	 *
25
	 * @param string $location  URL location (scheme is used to determine handler).
26
	 * @param string $filename  Unique identifier for cache object.
27
	 * @param string $extension 'spi' or 'spc'.
28
	 * @return WP_Feed_Cache_Transient Feed cache handler object that uses transients.
29
	 */
30
	public function create($location, $filename, $extension) {
31
		return new WP_Feed_Cache_Transient($location, $filename, $extension);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \WP_Feed_Cach...$filename, $extension); (WP_Feed_Cache_Transient) is incompatible with the return type of the parent method SimplePie_Cache::create of type SimplePie_Cache_Base.

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...
32
	}
33
}
34