Completed
Push — master ( 690c53...b92a26 )
by Kirill
03:02
created

HasInheritance::instanceOf()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 12
cts 12
cp 1
rs 9.1448
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
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\Definition\Behaviour;
11
12
use Railt\Reflection\Contracts\Definition\Behaviour\ProvidesInheritance;
13
use Railt\Reflection\Contracts\Definition\TypeDefinition;
14
use Railt\Reflection\Type;
15
16
/**
17
 * Trait HasInheritance
18
 */
19
trait HasInheritance
20
{
21
    /**
22
     * @var string|null
23
     */
24
    protected $extends;
25
26
    /**
27
     * @var string[]|array
28
     */
29
    protected $extendedBy = [];
30
31
    /**
32
     * @param string|TypeDefinition $type
33
     * @return TypeDefinition
34
     */
35
    abstract protected function fetch($type): TypeDefinition;
36
37
    /**
38
     * @return iterable|TypeDefinition[]
39
     */
40
    public function inheritedBy(): iterable
41
    {
42
        foreach ($this->extendedBy as $parent) {
43
            yield $this->fetch($parent);
44
        }
45
    }
46
47
    /**
48
     * @return null|TypeDefinition
49
     */
50 8
    public function getInheritedParent(): ?TypeDefinition
51
    {
52 8
        return $this->extends ? $this->fetch($this->extends) : null;
53
    }
54
55
    /**
56
     * @return bool
57
     */
58
    public function hasInheritance(): bool
59
    {
60
        return $this->extends !== null;
61
    }
62
63
    /**
64
     * @param TypeDefinition|string $definition
65
     * @return ProvidesInheritance|$this
66
     */
67 25
    public function extends($definition): ProvidesInheritance
68
    {
69 25
        $this->extends = $this->nameOf($definition);
0 ignored issues
show
Bug introduced by
It seems like nameOf() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
70
71 25
        if ($definition instanceof ProvidesInheritance) {
72
            /** @var HasInheritance $definition */
73 25
            $definition->extendedBy[] = $this->extends;
74
        }
75
76 25
        return $this;
77
    }
78
79
    /**
80
     * @param TypeDefinition|string $definition
81
     * @return ProvidesInheritance|$this
82
     */
83
    public function extendsBy($definition): ProvidesInheritance
84
    {
85
        /** @var HasInheritance $definition */
86
        $definition = $this->fetch($definition);
87
88
        $definition->extends($this);
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param TypeDefinition|string $type
95
     * @return bool
96
     */
97 8
    public function extendsOf($type): bool
98
    {
99 8
        return $this->extends === $this->nameOf($type);
0 ignored issues
show
Bug introduced by
It seems like nameOf() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
100
    }
101
102
    /**
103
     * @param TypeDefinition|string $type
104
     * @return bool
105
     */
106 8
    public function instanceOf($type): bool
107
    {
108
        /**
109
         * @var TypeDefinition $type
110
         * @var TypeDefinition $context
111
         */
112 8
        [$type, $context] = [$this->fetch($type), $this];
0 ignored issues
show
Bug introduced by
The variable $context seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
113
114 8
        if ($type::getType()->is(Type::ANY)) {
115 8
            return true;
116
        }
117
118 8
        if ($type === $context) {
0 ignored issues
show
Bug introduced by
The variable $context seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
119 8
            return true;
120
        }
121
122 8
        while ($context) {
0 ignored issues
show
Bug introduced by
The variable $context does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
123
            /** @var TypeDefinition $context */
124 8
            $context = $this->fetch($context);
125
126 8
            if ($context->extendsOf($type)) {
127 8
                return true;
128
            }
129
130 8
            $context = $context->getInheritedParent();
131
        }
132
133 8
        return false;
134
    }
135
}
136