Completed
Push — master ( 96b2f8...da2c9c )
by Kirill
05:58
created

HasInterfaces   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 64
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A instanceOfInterface() 0 10 3
A getInterfaces() 0 6 2
A hasInterface() 0 4 1
A getInterface() 0 9 2
A addInterface() 0 4 1
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\ProvidesInterfaces;
13
use Railt\Reflection\Contracts\Definition\InterfaceDefinition;
14
use Railt\Reflection\Contracts\Definition\TypeDefinition;
15
use Railt\Reflection\Exception\TypeNotFoundException;
16
17
/**
18
 * Trait HasInterfaces
19
 * @mixin ProvidesInterfaces
20
 */
21
trait HasInterfaces
22
{
23
    /**
24
     * @var array|string[]
25
     */
26
    protected $interfaces = [];
27
28
    /**
29
     * @param TypeDefinition $definition
30
     * @return bool
31
     */
32
    protected function instanceOfInterface(TypeDefinition $definition): bool
33
    {
34
        foreach ($this->getInterfaces() as $interface) {
35
            if ($interface->instanceOf($definition)) {
36
                return true;
37
            }
38
        }
39
40
        return false;
41
    }
42
43
    /**
44
     * @return iterable|InterfaceDefinition[]
45
     */
46
    public function getInterfaces(): iterable
47
    {
48
        foreach ($this->interfaces as $interface) {
49
            yield $this->fetch($interface);
0 ignored issues
show
Bug introduced by
It seems like fetch() 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...
50
        }
51
    }
52
53
    /**
54
     * @param string $name
55
     * @return bool
56
     */
57
    public function hasInterface(string $name): bool
58
    {
59
        return \in_array($name, $this->interfaces, true);
60
    }
61
62
    /**
63
     * @param string $name
64
     * @return InterfaceDefinition
65
     * @throws TypeNotFoundException
66
     */
67
    public function getInterface(string $name): InterfaceDefinition
68
    {
69
        if (! \in_array($name, $this->interfaces, true)) {
70
            $error = \sprintf('%s does not contain an interface named "%s"', $this, $name);
71
            throw new TypeNotFoundException($error);
72
        }
73
74
        return $this->fetch($name);
0 ignored issues
show
Bug introduced by
It seems like fetch() 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...
75
    }
76
77
    /**
78
     * @param InterfaceDefinition $definition
79
     */
80
    public function addInterface(InterfaceDefinition $definition): void
81
    {
82
        $this->interfaces[] = $definition->getName();
83
    }
84
}
85
86