Completed
Pull Request — master (#2657)
by
unknown
10:57
created

Reflector/Docblock/DescriptionAssemblerReducer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace phpDocumentor\Descriptor\Builder\Reflector\Docblock;
6
7
use phpDocumentor\Descriptor\Builder\AssemblerAbstract;
8
use phpDocumentor\Descriptor\Builder\AssemblerReducer;
9
use phpDocumentor\Descriptor\Descriptor;
10
use phpDocumentor\Descriptor\DocBlock\DescriptionDescriptor;
11
use phpDocumentor\Descriptor\TagDescriptor;
12
use phpDocumentor\Reflection\DocBlock\Tag;
13
14
/**
15
 * @template TDescriptor of \phpDocumentor\Descriptor\DescriptorAbstract
16
 * @template TInput of object
17
 * @extends AssemblerAbstract<TDescriptor, TInput>
18
 */
19
final class DescriptionAssemblerReducer extends AssemblerAbstract implements AssemblerReducer
20
{
21
    /**
22
     * @param TInput $data
23
     * @param TDescriptor $descriptor
24
     *
25
     * @return TDescriptor
26
     */
27 2
    public function create(object $data, ?Descriptor $descriptor = null) : ?Descriptor
28
    {
29 2
        if ($descriptor === null) {
30 1
            return null;
31
        }
32
33 1
        $description = new DescriptionDescriptor(
34 1
            $data->getDescription(),
35 1
            $data->getDescription() !== null ? $this->createTags($data->getDescription()->getTags()) : []
36
        );
37
38 1
        $descriptor->setDescription($description);
39
40 1
        return $descriptor;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $descriptor; (phpDocumentor\Descriptor...or\Docblock\TDescriptor) is incompatible with the return type declared by the interface phpDocumentor\Descriptor...ssemblerReducer::create of type phpDocumentor\Descriptor\Builder\T|null.

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...
41
    }
42
43
    /**
44
     * @param Tag[] $tags
45
     *
46
     * @return list<TagDescriptor|null>
47
     */
48 1
    private function createTags(array $tags) : array
49
    {
50 1
        $result = [];
51 1
        foreach ($tags as $tag) {
52 1
            $result[] = $this->builder->buildDescriptor($tag, TagDescriptor::class);
53
        }
54
55 1
        return $result;
56
    }
57
}
58