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

HasDirectives   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 45
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasDirective() 0 4 1
A getDirectives() 0 8 4
A getNumberOfDirectives() 0 4 1
A addDirective() 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\Invocation\Behaviour;
11
12
use Railt\Reflection\Contracts\Invocation\Behaviour\ProvidesDirectives;
13
use Railt\Reflection\Contracts\Invocation\DirectiveInvocation;
14
15
/**
16
 * Trait HasDirectives
17
 * @mixin ProvidesDirectives
18
 */
19
trait HasDirectives
20
{
21
    /**
22
     * @var array|DirectiveInvocation[]
23
     */
24
    protected $directives = [];
25
26
    /**
27
     * @param string $name
28
     * @return bool
29
     */
30
    public function hasDirective(string $name): bool
31
    {
32
        return \iterator_count($this->getDirectives($name)) > 0;
33
    }
34
35
    /**
36
     * @param string|null $name
37
     * @return iterable|DirectiveInvocation[]|\Generator
38
     */
39
    public function getDirectives(string $name = null): iterable
40
    {
41
        foreach ($this->directives as $directive) {
42
            if ($name === null || $name === $directive->getTypeDefinition()->getName()) {
43
                yield $directive;
44
            }
45
        }
46
    }
47
48
    /**
49
     * @return int
50
     */
51
    public function getNumberOfDirectives(): int
52
    {
53
        return \count($this->directives);
54
    }
55
56
    /**
57
     * @param DirectiveInvocation $invocation
58
     */
59
    protected function addDirective(DirectiveInvocation $invocation): void
60
    {
61
        $this->directives[] = $invocation;
62
    }
63
}
64