DocBlock   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A matches() 0 4 1
A create() 0 18 4
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
 * @copyright 2010-2018 Mike van Riel<[email protected]>
11
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
12
 * @link      http://phpdoc.org
13
 */
14
15
namespace phpDocumentor\Reflection\Php\Factory;
16
17
use InvalidArgumentException;
18
use phpDocumentor\Reflection\DocBlock as DocBlockDescriptor;
19
use phpDocumentor\Reflection\DocBlockFactoryInterface;
20
use phpDocumentor\Reflection\Location;
21
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
22
use phpDocumentor\Reflection\Php\StrategyContainer;
23
use phpDocumentor\Reflection\Types\Context;
24
use PhpParser\Comment\Doc;
25
26
/**
27
 * Strategy as wrapper around the DocBlockFactoryInterface.
28
 * @see DocBlockFactoryInterface
29
 * @see DocBlockDescriptor
30
 */
31
final class DocBlock implements ProjectFactoryStrategy
32
{
33
    /**
34
     * Wrapped DocBlock factory
35
     * @var DocBlockFactoryInterface
36
     */
37
    private $docblockFactory;
38
39
    /**
40
     * Initializes the object with a DocBlockFactory implementation.
41
     */
42 1
    public function __construct(DocBlockFactoryInterface $docBlockFactory)
43
    {
44 1
        $this->docblockFactory = $docBlockFactory;
45 1
    }
46
47 1
    public function matches($object): bool
48
    {
49 1
        return $object instanceof Doc;
50
    }
51
52
    /**
53
     * Creates an Element out of the given object.
54
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
55
     * used to create nested Elements.
56
     *
57
     * @param Doc $object object to convert to an Element
58
     * @param StrategyContainer $strategies used to convert nested objects.
59
     * @param Context $context of the created object
60
     * @return null|DocBlockDescriptor
61
     */
62 3
    public function create($object, StrategyContainer $strategies, ?Context $context = null): ?DocBlockDescriptor
63
    {
64 3
        if ($object === null) {
65 1
            return null;
66
        }
67
68 2
        if (!$this->matches($object)) {
69 1
            throw new InvalidArgumentException(
70 1
                sprintf(
71 1
                    '%s cannot handle objects with the type %s',
72 1
                    __CLASS__,
73 1
                    is_object($object) ? get_class($object) : gettype($object)
74
                )
75
            );
76
        }
77
78 1
        return $this->docblockFactory->create($object->getText(), $context, new Location($object->getLine()));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->docblockFa...n($object->getLine())); (phpDocumentor\Reflection\DocBlock) is incompatible with the return type declared by the interface phpDocumentor\Reflection...FactoryStrategy::create of type phpDocumentor\Reflection\Element.

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...
79
    }
80
}
81