Test Failed
Push — master ( 97fe68...3f86cd )
by Kirill
42s
created

HasDefinitions   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 73.68%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 73
ccs 14
cts 19
cp 0.7368
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
fetch() 0 1 ?
A getDefinitions() 0 6 2
A hasDefinition() 0 4 1
A getDefinition() 0 8 2
A withDefinition() 0 8 2
A withoutDefinition() 0 10 3
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\ProvidesTypeDefinitions;
13
use Railt\Reflection\Contracts\Definition\TypeDefinition;
14
15
/**
16
 * Trait HasDefinitions
17
 */
18
trait HasDefinitions
19
{
20
    /**
21
     * @var array|string[]
22
     */
23
    protected $types = [];
24
25
    /**
26
     * @param string|TypeDefinition $type
27
     * @return TypeDefinition
28
     */
29
    abstract protected function fetch($type): TypeDefinition;
30
31
    /**
32
     * @return iterable|TypeDefinition[]
33
     */
34 3
    public function getDefinitions(): iterable
35
    {
36 3
        foreach ($this->types as $type) {
37 2
            yield $this->fetch($type);
38
        }
39 3
    }
40
41
    /**
42
     * @param string $name
43
     * @return bool
44
     */
45 5
    public function hasDefinition(string $name): bool
46
    {
47 5
        return \in_array($name, $this->types, true);
48
    }
49
50
    /**
51
     * @param string $name
52
     * @return null|TypeDefinition
53
     */
54 3
    public function getDefinition(string $name): ?TypeDefinition
55
    {
56 3
        if (! \in_array($name, $this->types, true)) {
57 1
            return null;
58
        }
59
60 2
        return $this->fetch($name);
61
    }
62
63
    /**
64
     * @param string|TypeDefinition ...$types
65
     * @return ProvidesTypeDefinitions|$this
66
     */
67 17
    public function withDefinition(...$types): ProvidesTypeDefinitions
68
    {
69 17
        foreach ($types as $type) {
70 17
            $this->types[] = $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...
71
        }
72
73 17
        return $this;
74
    }
75
76
    /**
77
     * @param string|TypeDefinition ...$types
78
     * @return ProvidesTypeDefinitions|$this
79
     */
80
    public function withoutDefinition(...$types): ProvidesTypeDefinitions
81
    {
82
        foreach ($types as $type) {
83
            if ($this->hasDefinition($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...
84
                array_splice($this->types, array_search($this->types, $this->nameOf($type)), 1);
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...
85
            }
86
        }
87
88
        return $this;
89
    }
90
}
91