Completed
Push — master ( 0f407e...32a4d0 )
by Mehmet
04:33
created

Text::normalize()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 0
dl 0
loc 16
rs 8.8571
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
10
class Text extends DataTypeAbstract implements DataTypeInterface
11
{
12
    const DATA_TYPE_ERROR   = 'Assertion failed for value "%s" for "%s" : INVALID_TYPE';
13
    const DATA_LENGTH       = 'Assertion failed for value "%s" for "%s" : INVALID_TEXT_LENGTH';
14
15
    protected static $defaults = [
16
        'default'   => '',
17
        'min'       => 0,
18
        'max'       => null,
19
        'pad'       => 'left',
20
        'left_pad'  => ' ',
21
        'right_pad' => ' '
22
    ];
23
24
    protected static $padOptions = [
25
        'left'  => STR_PAD_LEFT,
26
        'right' => STR_PAD_RIGHT
27
    ];
28
29
    /**
30
     * Boolean constructor.
31
     * @param string $key
32
     * @param mixed $datum
33
     * @param array $options
34
     * @throws validArgumentException
35
     */
36
    public function __construct(string $key, $datum, array $options = [])
37
    {
38
        $this->key = $key;
39
        $this->datum = $datum;
40
        $this->checkValidOptions($options);
41
        $this->options = array_merge(self::$defaults, $options);
42
    }
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function assert()
47
    {
48
        $this->isString();
49
        $this->checkLength();
50
        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...
51
    }
52
53
    private function isString()
54
    {
55
        try {
56
            Assertion::string($this->datum);
57
        } catch (AssertionFailedException $e) {
58
            $this->errorMessageTemplate = self::DATA_TYPE_ERROR;
59
            $this->throwException();
60
        }
61
        return true;
62
    }
63
64
    private function checkLength()
65
    {
66
        if ($this->options['max'] !== null) {
67
            return $this->checkLengthBetween();
68
        }
69
        try {
70
            Assertion::minLength($this->datum, (int) $this->options['min']);
71
        } catch (AssertionFailedException $e) {
72
            $this->errorMessageTemplate = self::DATA_LENGTH . 'MIN:' . $this->options['min'];
73
            $this->throwException();
74
        }
75
    }
76
    private function checkLengthBetween()
77
    {
78
        try {
79
            Assertion::betweenLength($this->datum, (int) $this->options['min'], (int) $this->options['max']);
80
        } catch (AssertionFailedException $e) {
81
            $this->errorMessageTemplate = self::DATA_LENGTH
82
                . 'MIN:' . $this->options['min']
83
                . 'MAX:' . $this->options['max'];
84
            $this->throwException();
85
        }
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function normalize()
92
    {
93
        if (null === $this->datum) {
94
            return $this->options['default'];
95
        }
96
        if ($this->options['max'] !== null) {
97
            $length = (int) $this->options['max'];
98
            return mb_substr($this->datum, 0, $length);
99
        }
100
        if (strlen($this->datum) < (int) $this->options['min']) {
101
            $padType = self::$padOptions[$this->options['pad']] ?: STR_PAD_RIGHT;
102
            $padding = $this->options[$this->options['pad'].'_pad'];
103
            return str_pad($this->datum, (int) $this->options['min'], $padding, $padType);
104
        }
105
        return $this->datum;
106
    }
107
}
108