MultistateLexer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 66
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A state() 0 10 2
A getTokenDefinitions() 0 10 2
A getTokenState() 0 4 1
A getNextState() 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\Driver;
11
12
use Railt\Lexer\Definition\MultistateTokenDefinition;
13
use Railt\Lexer\MultistateLexerInterface;
14
15
/**
16
 * Class MultistateLexer
17
 */
18
abstract class MultistateLexer extends SimpleLexer implements MultistateLexerInterface
19
{
20
    /**
21
     * @var int
22
     */
23
    protected const DEFAULT_STATE = 0x00;
24
25
    /**
26
     * @var array|int[]
27
     */
28
    protected $states = [];
29
30
    /**
31
     * @var array|int[]
32
     */
33
    protected $transitions = [];
34
35
    /**
36
     * @param string $token
37
     * @param int $state
38
     * @param int|null $nextState
39
     * @return MultistateLexerInterface
40
     */
41
    public function state(string $token, int $state, int $nextState = null): MultistateLexerInterface
42
    {
43
        $this->states[$token] = $state;
44
45
        if ($nextState !== null) {
46
            $this->transitions[$token] = $nextState;
47
        }
48
49
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Railt\Lexer\Driver\MultistateLexer) is incompatible with the return type declared by the interface Railt\Lexer\MultistateLexerInterface::state 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...
50
    }
51
52
    /**
53
     * @return iterable
54
     */
55
    public function getTokenDefinitions(): iterable
56
    {
57
        foreach ($this->tokens as $name => $pcre) {
58
            $keep = ! \in_array($name, $this->skipped, true);
59
            $state = $this->getTokenState($name);
60
            $next = $this->getNextState($name);
61
62
            yield new MultistateTokenDefinition($name, $pcre, $keep, $state, $next);
63
        }
64
    }
65
66
    /**
67
     * @param string $token
68
     * @return int
69
     */
70
    protected function getTokenState(string $token): int
71
    {
72
        return $this->states[$token] ?? static::DEFAULT_STATE;
73
    }
74
75
    /**
76
     * @param string $token
77
     * @return int
78
     */
79
    protected function getNextState(string $token): int
80
    {
81
        return $this->transitions[$this->getTokenState($token)] ?? static::DEFAULT_STATE;
82
    }
83
}
84