Completed
Push — develop ( 0b8425...c51867 )
by Jaap
15s queued 11s
created

Builder/Reflector/Tags/ExampleAssembler.php (3 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
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Descriptor\Builder\Reflector\Tags;
17
18
use InvalidArgumentException;
19
use phpDocumentor\Descriptor\Builder\Reflector\AssemblerAbstract;
20
use phpDocumentor\Descriptor\Example\Finder;
21
use phpDocumentor\Descriptor\Tag\ExampleDescriptor;
22
use phpDocumentor\Reflection\DocBlock\ExampleFinder;
23
use phpDocumentor\Reflection\DocBlock\Tags\Example;
24
use Webmozart\Assert\Assert;
25
26
/**
27
 * This class collects data from the example tag definition of the Reflection library, tries to find the correlating
28
 * example file on disk and creates a complete Descriptor from that.
29
 */
30
class ExampleAssembler extends AssemblerAbstract
31
{
32
    /** @var Finder */
33
    private $finder;
34
35
    /**
36
     * Initializes this assembler with the means to find the example file on disk.
37
     */
38 1
    public function __construct(ExampleFinder $finder)
39
    {
40 1
        $this->finder = $finder;
0 ignored issues
show
Documentation Bug introduced by
It seems like $finder of type object<phpDocumentor\Ref...DocBlock\ExampleFinder> is incompatible with the declared type object<phpDocumentor\Descriptor\Example\Finder> of property $finder.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41 1
    }
42
43
    /**
44
     * Creates a new Descriptor from the given Reflector.
45
     *
46
     * @param Example $data
47
     *
48
     * @throws InvalidArgumentException if the provided parameter is not of type ExampleTag; the interface won't let
49
     *   up typehint the signature.
50
     *
51
     * @return ExampleDescriptor
52
     */
53 2
    public function create($data)
54
    {
55 2
        Assert::isInstanceOf($data, Example::class);
56 1
        $descriptor = new ExampleDescriptor($data->getName());
57 1
        $descriptor->setFilePath((string) $data->getFilePath());
58 1
        $descriptor->setStartingLine($data->getStartingLine());
59 1
        $descriptor->setLineCount($data->getLineCount());
60 1
        $descriptor->setDescription($data->getDescription());
61 1
        $descriptor->setExample($this->finder->find($data));
0 ignored issues
show
$data is of type object<phpDocumentor\Ref...\DocBlock\Tags\Example>, but the function expects a object<phpDocumentor\Des...\Tag\ExampleDescriptor>.

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...
62
63 1
        return $descriptor;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $descriptor; (phpDocumentor\Descriptor\Tag\ExampleDescriptor) 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...
64
    }
65
}
66