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

HasDefinitions::withoutDefinition()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
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