Completed
Push — master ( 427b6b...310e20 )
by BENOIT
02:59
created

PSR16CacheSuffixProvider::getSuffixes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace BenTools\HostnameExtractor\SuffixProvider;
4
5
use Psr\SimpleCache\CacheInterface;
6
7
class PSR16CacheSuffixProvider implements SuffixProviderInterface
8
{
9
    /**
10
     * @var CacheInterface
11
     */
12
    private $cache;
13
14
    /**
15
     * @var PublicSuffixProvider
16
     */
17
    private $publicSuffixProvider;
18
19
    /**
20
     * @var string
21
     */
22
    private $key;
23
24
    /**
25
     *  constructor.
26
     * @param CacheInterface       $cache
27
     * @param PublicSuffixProvider $publicSuffixProvider
28
     */
29
    public function __construct(
30
        CacheInterface $cache,
31
        PublicSuffixProvider $publicSuffixProvider,
32
        string $key = 'suffix_list'
33
    ) {
34
        $this->cache = $cache;
35
        $this->publicSuffixProvider = $publicSuffixProvider;
36
        $this->key = $key;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public function getSuffixes(): iterable
43
    {
44
        if (!$this->cache->has($this->key)) {
45
            $suffixes = iterable_to_array($this->publicSuffixProvider->getSuffixes());
46
            $this->cache->set($this->key, $suffixes);
47
            return $suffixes;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $suffixes; (array) is incompatible with the return type declared by the interface BenTools\HostnameExtract...rInterface::getSuffixes of type BenTools\HostnameExtractor\SuffixProvider\iterable.

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...
48
        }
49
        return $this->cache->get($this->key);
50
    }
51
}
52