DocBlockReducer::reduce()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of Properties 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 Serafim\Properties\Reducers;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Serafim\Properties\Attribute\Attribute;
14
use Serafim\Properties\Attribute\Matchable;
15
16
/**
17
 * Class DocBlockReducer
18
 */
19
class DocBlockReducer implements ReducerInterface
20
{
21
    /**
22
     * @param RuleInterface $rule
23
     * @return bool
24
     */
25
    public function match(RuleInterface $rule): bool
26
    {
27
        return $rule->getName() === 'DocBlock';
28
    }
29
30
    /**
31
     * @param RuleInterface $rule
32
     * @return mixed|string
33
     */
34
    public function reduce(RuleInterface $rule)
35
    {
36
        /** @var int $type */
37
        $type = yield $rule->first('DocBlockTitle');
38
39
        /** @var string $name */
40
        $name = yield $rule->first('DocBlockVariable');
41
42
        /** @var Matchable $hint */
43
        $hint = yield $rule->first('TypeHint');
44
45
        $attribute = new Attribute($name, $type);
46
47
        if ($hint === null) {
48
            return $attribute;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $attribute; (Serafim\Properties\Attribute\Attribute) is incompatible with the return type documented by Serafim\Properties\Reduc...DocBlockReducer::reduce of type Generator.

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...
49
        }
50
51
        return $attribute->addMatcher($hint);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $attribute->addMatcher($hint); (Serafim\Properties\Attribute\Matchable) is incompatible with the return type documented by Serafim\Properties\Reduc...DocBlockReducer::reduce of type Generator.

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...
52
    }
53
}
54