Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
created

SeeAssembler::create()   D

Complexity

Conditions 10
Paths 4

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 20
nc 4
nop 1
dl 0
loc 33
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2014 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\Compiler\Linker\Linker;
15
use phpDocumentor\Descriptor\Builder\Reflector\AssemblerAbstract;
16
use phpDocumentor\Descriptor\Tag\SeeDescriptor;
17
use phpDocumentor\Reflection\DocBlock\Tag\SeeTag;
18
use phpDocumentor\Reflection\DocBlock\Type\Collection;
19
20
/**
21
 * Constructs a new Descriptor from a Reflector object for the `@see` tag.
22
 *
23
 * This class will gather the properties that were parsed by the Reflection mechanism for, specifically, an `@see` tag
24
 * and use that to create a SeeDescriptor that describes all properties that an `@see` tag may have.
25
 */
26
class SeeAssembler extends AssemblerAbstract
27
{
28
    /**
29
     * Creates a new Descriptor from the given Reflector.
30
     *
31
     * @param SeeTag $data
32
     *
33
     * @return SeeDescriptor
34
     */
35
    public function create($data)
36
    {
37
        $descriptor = new SeeDescriptor($data->getName());
38
        $descriptor->setDescription($data->getDescription());
39
40
        $reference = $data->getReference();
41
42
        if (substr($reference, 0, 7) !== 'http://'
43
            && substr($reference, 0, 8) !== 'https://'
44
            && $reference !== 'self'
45
            && $reference !== '$this'
46
        ) {
47
            // TODO: move this to the ReflectionDocBlock component
48
            // Expand FQCN part of the FQSEN
49
            $referenceParts = explode('::', $reference);
50
            if (count($referenceParts) > 1 && $reference[0] != '\\') {
51
                $type = current($referenceParts);
52
                $type = new Collection(
53
                    array($type),
54
                    $data->getDocBlock() ? $data->getDocBlock()->getContext() : null
55
                );
56
                $referenceParts[0] = $type;
57
            } elseif (isset($reference[0]) && $reference[0] != '\\') {
58
                array_unshift($referenceParts, Linker::CONTEXT_MARKER);
59
            }
60
61
            $reference = implode('::', $referenceParts);
62
        }
63
64
        $descriptor->setReference($reference);
65
66
        return $descriptor;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $descriptor; (phpDocumentor\Descriptor\Tag\SeeDescriptor) 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...
67
    }
68
}
69