Completed
Pull Request — master (#26)
by X
02:15
created

RegexpSubstringParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
1
<?php
2
/**
3
 * parser for regexp match and then substring
4
 */
5
namespace xKerman\Restricted;
6
7
/**
8
 * Parser that first process regexp match, and then substring the result
9
 */
10
class RegexpSubstringParser implements ParserInterface
11
{
12
    /** @var string $regexp regexp for matching */
13
    private $regexp;
14
15
    /** @var integer $start start position of substring */
16
    private $start;
17
18
    /** @var integer $length length of substring (see PHP `substr` manual) */
19
    private $length;
20
21
    /**
22
     * constructor
23
     *
24
     * @param string  $regexp regexp for matching
25
     * @param integer $start  start position of substring
26
     * @param integer $length length of substring
27
     */
28 12
    public function __construct($regexp, $start, $length)
29
    {
30 12
        $this->regexp = $regexp;
31 12
        $this->start = $start;
32 12
        $this->length = $length;
33 12
    }
34
35
    /**
36
     * parse given `$source` and return substring result
37
     *
38
     * @param Source $source parser input
39
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
40
     * @throws UnserializeFailedException
41
     */
42 12
    public function parse(Source $source)
43
    {
44 12
        $result = $source->match($this->regexp);
45 11
        return substr($result, $this->start, $this->length);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return substr($result, $...>start, $this->length); (string) is incompatible with the return type declared by the interface xKerman\Restricted\ParserInterface::parse of type array.

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...
46
    }
47
}
48