Completed
Push — master ( 2135e4...0f407e )
by Mehmet
28:04 queued 13:46
created

Integer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 6
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Selami\Entity\DataType;
5
6
use Selami\Entity\Interfaces\DataTypeInterface;
7
use Assert\Assertion;
8
use Assert\AssertionFailedException;
9
use InvalidArgumentException;
10
11 View Code Duplication
class Integer extends DataTypeAbstract implements DataTypeInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
    const DATA_TYPE_ERROR   = 'Assertion failed for value "%s" for "%s" : INVALID_TYPE';
14
15
    protected static $defaults = [];
16
17
    /**
18
     * Boolean constructor.
19
     * @param string $key
20
     * @param mixed $datum
21
     * @param array $options
22
     */
23
    public function __construct(string $key, $datum, array $options = [])
24
    {
25
        $this->key = $key;
26
        $this->datum = $datum;
27
        $this->options = array_merge(self::$defaults, $options);
28
    }
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function assert()
33
    {
34
        try {
35
            Assertion::integer($this->datum);
36
        } catch (AssertionFailedException $e) {
37
            $this->errorMessageTemplate = self::DATA_TYPE_ERROR;
38
            $this->throwException();
39
        }
40
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type declared by the interface Selami\Entity\Interfaces\DataTypeInterface::assert of type Selami\Entity\Interfaces\true.

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...
41
    }
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function normalize()
46
    {
47
        try {
48
            $this->assert();
49
            return $this->datum;
50
        } catch (InvalidArgumentException $e) {
51
            return $this->options['default'];
52
        }
53
    }
54
}
55