Completed
Push — master ( 8d0ca7...0aa70f )
by Kirill
06:35
created

NativeStateless::exec()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 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\Io\Readable;
13
use Railt\Lexer\Driver\Common\PCRECompiler;
14
use Railt\Lexer\Stateless;
15
use Railt\Lexer\TokenInterface;
16
17
/**
18
 * Class NativeStateless
19
 */
20
class NativeStateless extends Lexer implements Stateless
21
{
22
    /**
23
     * @var PCRECompiler
24
     */
25
    private $pcre;
26
27
    /**
28
     * NativeStateless constructor.
29
     */
30 3
    public function __construct()
31
    {
32 3
        $this->pcre = new PCRECompiler();
33 3
    }
34
35
    /**
36
     * @param string $name
37
     * @param string $pcre
38
     * @param bool $skip
39
     * @return Stateless
40
     */
41 11
    public function add(string $name, string $pcre, bool $skip = false): Stateless
42
    {
43 11
        $this->pcre->add($name, $pcre);
44
45 11
        if ($skip) {
46 4
            $this->skip[] = $name;
47
        }
48
49 11
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Railt\Lexer\Driver\NativeStateless) is incompatible with the return type declared by the interface Railt\Lexer\Stateless::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...
50
    }
51
52
    /**
53
     * @param string $name
54
     * @return bool
55
     */
56 2
    public function has(string $name): bool
57
    {
58 2
        return \array_key_exists($name, $this->pcre->getTokens());
59
    }
60
61
    /**
62
     * @return iterable
63
     */
64 2
    public function getDefinedTokens(): iterable
65
    {
66 2
        return $this->pcre->getTokens();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->pcre->getTokens(); (array) is incompatible with the return type declared by the interface Railt\Lexer\Stateless::getDefinedTokens 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
     * @param Readable $file
71
     * @return \Traversable|TokenInterface
72
     */
73 8
    protected function exec(Readable $file): \Traversable
74
    {
75 8
        $lexer = new NativeStateful($this->pcre->compile(), $this->skip);
76
77 8
        return $lexer->lex($file, true);
78
    }
79
}
80