Passed
Push — master ( c2231d...b4845c )
by Jesse
02:15
created

Ignore   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 80
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A the() 0 4 1
A has() 0 4 1
A hasThe() 0 4 1
A get() 0 4 1
A add() 0 7 2
A remove() 0 7 2
A removeThe() 0 4 1
A removeAllObjectsOfThe() 0 7 2
A idOf() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\IdentityMap;
5
6
/**
7
 * Ignore a certain class, for instance a value object.
8
 *
9
 * @author Stratadox
10
 */
11
final class Ignore implements MapsObjectsByIdentity
12
{
13
    private $ignoredClass;
14
    private $identityMap;
15
16
    private function __construct(string $ignoredClass, MapsObjectsByIdentity $identityMap)
17
    {
18
        $this->ignoredClass = $ignoredClass;
19
        $this->identityMap = $identityMap->removeAllObjectsOfThe($ignoredClass);
20
    }
21
22
    /**
23
     * Wraps the identity map with a decorator that ignores a certain class.
24
     *
25
     * @param string                $ignoredClass The class to be ignored.
26
     * @param MapsObjectsByIdentity $identityMap  The identity map to wrap.
27
     * @return Ignore                             The wrapped identity map.
28
     */
29
    public static function the(string $ignoredClass, MapsObjectsByIdentity $identityMap): MapsObjectsByIdentity
30
    {
31
        return new self($ignoredClass, $identityMap);
32
    }
33
34
    /** @inheritdoc */
35
    public function has(string $class, string $id): bool
36
    {
37
        return $this->identityMap->has($class, $id);
38
    }
39
40
    /** @inheritdoc */
41
    public function hasThe(object $object): bool
42
    {
43
        return $this->identityMap->hasThe($object);
44
    }
45
46
    /** @inheritdoc */
47
    public function get(string $class, string $id): object
48
    {
49
        return $this->identityMap->get($class, $id);
50
    }
51
52
    /** @inheritdoc */
53
    public function add(string $id, object $object): MapsObjectsByIdentity
54
    {
55
        if ($object instanceof $this->ignoredClass) {
56
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Stratadox\IdentityMap\Ignore) is incompatible with the return type declared by the interface Stratadox\IdentityMap\MapsObjectsByIdentity::add of type Stratadox\IdentityMap\IdentityMap.

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...
57
        }
58
        return new self($this->ignoredClass, $this->identityMap->add($id, $object));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new self($this->i...ap->add($id, $object)); (Stratadox\IdentityMap\Ignore) is incompatible with the return type declared by the interface Stratadox\IdentityMap\MapsObjectsByIdentity::add of type Stratadox\IdentityMap\IdentityMap.

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...
59
    }
60
61
    /** @inheritdoc */
62
    public function remove(string $class, string $id): MapsObjectsByIdentity
63
    {
64
        if ($class === $this->ignoredClass) {
65
            return $this;
66
        }
67
        return $this->identityMap->remove($class, $id);
68
    }
69
70
    /** @inheritdoc */
71
    public function removeThe(object $object): MapsObjectsByIdentity
72
    {
73
        return $this->identityMap->removeThe($object);
74
    }
75
76
    /** @inheritdoc */
77
    public function removeAllObjectsOfThe(string $class): MapsObjectsByIdentity
78
    {
79
        if ($class === $this->ignoredClass) {
80
            return $this;
81
        }
82
        return $this->identityMap->removeAllObjectsOfThe($class);
83
    }
84
85
    /** @inheritdoc */
86
    public function idOf(object $object): string
87
    {
88
        return $this->identityMap->idOf($object);
89
    }
90
}
91