PSR16CacheSuffixProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace BenTools\HostnameExtractor\SuffixProvider;
4
5
use Psr\SimpleCache\CacheInterface;
6
7
final class PSR16CacheSuffixProvider implements SuffixProviderInterface
8
{
9
    /**
10
     * @var CacheInterface
11
     */
12
    private $cache;
13
14
    /**
15
     * @var PublicSuffixProvider
16
     */
17
    private $suffixProvider;
18
19
    /**
20
     * @var string
21
     */
22
    private $key;
23
24
    /**
25
     *  constructor.
26
     * @param CacheInterface       $cache
27
     * @param PublicSuffixProvider $suffixProvider
28
     */
29
    public function __construct(
30
        CacheInterface $cache,
31
        SuffixProviderInterface $suffixProvider,
32
        string $key = 'suffix_list'
33
    ) {
34
        $this->cache = $cache;
35
        $this->suffixProvider = $suffixProvider;
0 ignored issues
show
Documentation Bug introduced by
$suffixProvider is of type object<BenTools\Hostname...uffixProviderInterface>, but the property $suffixProvider was declared to be of type object<BenTools\Hostname...r\PublicSuffixProvider>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
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->suffixProvider->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