Passed
Push — master ( 0971b0...b7d7cf )
by Kirill
03:21
created

Reflection::wrap()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 6.28

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 2
cts 7
cp 0.2857
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 6.28
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Reflection;
11
12
use Railt\Reflection\Contracts\Definition\TypeDefinition;
13
use Railt\Reflection\Contracts\Dictionary;
14
use Railt\Reflection\Contracts\Document as DocumentInterface;
15
use Railt\Reflection\Contracts\Reflection as ReflectionInterface;
16
use Railt\Reflection\Dictionary\ProxyDictionary;
17
use Railt\Reflection\Dictionary\SimpleDictionary;
18
use Railt\Reflection\Exception\ReflectionException;
19
use Railt\Reflection\Introspection\IntrospectionDocument;
20
use Railt\Reflection\Stdlib\StdlibDocument;
21
22
/**
23
 * Class Reflection
24
 */
25
class Reflection extends ProxyDictionary implements ReflectionInterface
26
{
27
    /**
28
     * @var int
29
     */
30
    public const LOAD_STDLIB = 0x02;
31
32
    /**
33
     * @var int
34
     */
35
    public const LOAD_INTROSPECTION = 0x04;
36
37
    /**
38
     * @var array|DocumentInterface[]
39
     */
40
    protected $documents = [];
41
42
    /**
43
     * Reflection constructor.
44
     * @param Dictionary|null $parent
45
     * @param int $options
46
     * @throws Exception\TypeConflictException
47
     * @throws \Railt\Io\Exception\ExternalFileException
48
     */
49 9
    public function __construct(Dictionary $parent = null, int $options = self::LOAD_INTROSPECTION | self::LOAD_STDLIB)
50
    {
51 9
        parent::__construct($parent ?? new SimpleDictionary());
52
53 9
        $this->boot($options);
54 9
    }
55
56
    /**
57
     * @param int $options
58
     * @return void
59
     * @throws Exception\TypeConflictException
60
     * @throws \Railt\Io\Exception\ExternalFileException
61
     */
62 9
    private function boot(int $options): void
63
    {
64 9
        if ($options & self::LOAD_STDLIB) {
65 9
            $this->addDocument(new StdlibDocument($this));
66
        }
67
68 9
        if ($options & self::LOAD_INTROSPECTION) {
69 9
            $this->addDocument(new IntrospectionDocument($this));
70
        }
71 9
    }
72
73
    /**
74
     * @param DocumentInterface $document
75
     */
76 17
    public function addDocument(DocumentInterface $document): void
77
    {
78 17
        $this->documents[$document->getName()] = $document;
79 17
    }
80
81
    /**
82
     * @return iterable
83
     */
84 4
    public function getDocuments(): iterable
85
    {
86 4
        return \array_values($this->documents);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \array_values($this->documents); (array) is incompatible with the return type declared by the interface Railt\Reflection\Contrac...eflection::getDocuments of type Railt\Reflection\Contrac...on\Contracts\Document[].

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...
87
    }
88
89
    /**
90
     * @param TypeDefinition $type
91
     * @return Dictionary
92
     */
93 17
    public function add(TypeDefinition $type): Dictionary
94
    {
95 17
        $this->addDocument($type->getDocument());
96
97 17
        return parent::add($type);
98
    }
99
}
100