Test Failed
Push — master ( b7dc5c...538cc6 )
by Kirill
05:24 queued 01:27
created

Lexer::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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\Io\Readable;
13
use Railt\Lexer\LexerInterface;
14
use Railt\Lexer\TokenInterface;
15
16
/**
17
 * Class BaseLexer
18
 */
19
abstract class Lexer implements LexerInterface
20
{
21
    /**
22
     * @var array|string[]
23
     */
24
    protected $skipped = [];
25
26
    /**
27
     * @var array|string[]
28
     */
29
    protected $tokens = [];
30
31
    /**
32
     * @param Readable $input
33
     * @return \Traversable|TokenInterface[]
34
     */
35 6
    public function lex(Readable $input): \Traversable
36
    {
37 6
        foreach ($this->exec($input) as $token) {
38 6
            if (! \in_array($token->getName(), $this->skipped, true)) {
39 6
                yield $token;
40
            }
41
        }
42 6
    }
43
44
    /**
45
     * @param string $token
46
     * @param string $pcre
47
     * @return LexerInterface
48
     */
49
    public function add(string $token, string $pcre): LexerInterface
50
    {
51
        $this->tokens[$token] = $pcre;
52
53
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Railt\Lexer\Driver\Lexer) is incompatible with the return type declared by the interface Railt\Lexer\LexerInterface::add 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...
54
    }
55
56
    /**
57
     * @param string $name
58
     * @return LexerInterface
59
     */
60 1
    public function skip(string $name): LexerInterface
61
    {
62 1
        $this->skipped[] = $name;
63
64 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Railt\Lexer\Driver\Lexer) is incompatible with the return type declared by the interface Railt\Lexer\LexerInterface::skip 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...
65
    }
66
67
    /**
68
     * @param Readable $file
69
     * @return \Traversable|TokenInterface[]
70
     */
71
    abstract protected function exec(Readable $file): \Traversable;
72
}
73