Eoi   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 29.4%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 78
ccs 5
cts 17
cp 0.294
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getOffset() 0 4 1
A getName() 0 4 1
A getValue() 0 4 1
A getGroups() 0 4 1
A getBytes() 0 4 1
A getLength() 0 4 1
A __toString() 0 4 1
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Lexer\Result;
11
12
/**
13
 * Class Eoi
14
 */
15
final class Eoi extends BaseToken
16
{
17
    /**
18
     * End of input token name
19
     */
20
    public const T_NAME = 'T_EOI';
21
22
    /**
23
     * @var int
24
     */
25
    private $offset;
26
27
    /**
28
     * Eoi constructor.
29
     * @param int $offset
30
     */
31 6
    public function __construct(int $offset)
32
    {
33 6
        $this->offset = $offset;
34 6
    }
35
36
    /**
37
     * @return int
38
     */
39
    public function getOffset(): int
40
    {
41
        return $this->offset;
42
    }
43
44
    /**
45
     * @return string
46
     */
47 6
    public function getName(): string
48
    {
49 6
        return static::T_NAME;
50
    }
51
52
    /**
53
     * @param int|null $offset
54
     * @return string
55
     */
56
    public function getValue(int $offset = null): string
57
    {
58
        return "\0";
59
    }
60
61
    /**
62
     * @return iterable|string[]
63
     */
64
    public function getGroups(): iterable
65
    {
66
        return [$this->getValue()];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array($this->getValue()); (string[]) is incompatible with the return type declared by the interface Railt\Lexer\TokenInterface::getGroups of type Railt\Lexer\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...
67
    }
68
69
    /**
70
     * @return int
71
     */
72
    public function getBytes(): int
73
    {
74
        return 0;
75
    }
76
77
    /**
78
     * @return int
79
     */
80
    public function getLength(): int
81
    {
82
        return 0;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function __toString(): string
89
    {
90
        return \sprintf('"%s" (%s)', '\0', self::T_NAME);
91
    }
92
}
93