Completed
Push — master ( 8bb399...6d8696 )
by Randy
03:07
created

None::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Dgame\Optional;
4
5
use Exception;
6
7
/**
8
 * Class None
9
 * @package Dgame\Optional
10
 */
11
final class None extends AbstractOptional
12
{
13
    /**
14
     * @var None
15
     */
16
    private static $instance;
17
18
    /**
19
     * None constructor.
20
     */
21
    private function __construct()
22
    {
23
    }
24
25
    /**
26
     *
27
     */
28
    private function __clone()
29
    {
30
    }
31
32
    /**
33
     * @return None
34
     */
35
    public static function instance(): self
36
    {
37
        if (self::$instance === null) {
38
            self::$instance = new self();
39
        }
40
41
        return self::$instance;
42
    }
43
44
    /**
45
     * @param mixed $value
46
     *
47
     * @return bool
48
     */
49
    public function isSome(&$value = null): bool
50
    {
51
        $value = null;
52
53
        return false;
54
    }
55
56
    /**
57
     * @throws Exception
58
     */
59
    public function unwrap()
60
    {
61
        throw new Exception('Access to None value');
62
    }
63
64
    /**
65
     * @param callable $callback
66
     *
67
     * @return OptionalInterface
68
     */
69
    public function ensure(callable $callback): OptionalInterface
70
    {
71
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Dgame\Optional\None) is incompatible with the return type declared by the interface Dgame\Optional\OptionalInterface::ensure of type self.

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...
72
    }
73
74
    /**
75
     * @param $value
76
     *
77
     * @return bool
78
     */
79
    public function isEqualTo($value): bool
80
    {
81
        return $value == null;
82
    }
83
84
    /**
85
     * @param $value
86
     *
87
     * @return bool
88
     */
89
    public function isIdenticalTo($value): bool
90
    {
91
        return $value === null;
92
    }
93
}