Completed
Push — develop ( a89061...54507c )
by Jaap
08:53
created

Builder/Reflector/Tags/DeprecatedAssembler.php (2 issues)

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
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Descriptor\Builder\Reflector\Tags;
13
14
use phpDocumentor\Descriptor\Builder\Reflector\AssemblerAbstract;
15
use phpDocumentor\Descriptor\Tag\DeprecatedDescriptor;
16
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
17
18
/**
19
 * Constructs a new descriptor from the Reflector for an `@deprecated` tag.
20
 *
21
 * This object will read the reflected information for the `@deprecated` tag and create a {@see DeprecatedDescriptor}
22
 * object that can be used in the rest of the application and templates.
23
 */
24
class DeprecatedAssembler extends AssemblerAbstract
25
{
26
    /**
27
     * Creates a new Descriptor from the given Reflector.
28
     *
29
     * @param Deprecated $data
30
     *
31
     * @return DeprecatedDescriptor
32
     */
33
    public function create($data)
34
    {
35
        $descriptor = new DeprecatedDescriptor($data->getName());
36
        $descriptor->setDescription($data->getDescription());
0 ignored issues
show
$data->getDescription() is of type object<phpDocumentor\Ref...Block\Description>|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
        $descriptor->setVersion($data->getVersion());
38
39
        return $descriptor;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $descriptor; (phpDocumentor\Descriptor\Tag\DeprecatedDescriptor) is incompatible with the return type declared by the interface phpDocumentor\Descriptor...emblerInterface::create of type phpDocumentor\Descriptor...r\Descriptor\Collection.

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...
40
    }
41
}
42