Completed
Push — master ( 611e0d...3b7232 )
by Kirill
02:21
created

HasDirectives::hasDirective()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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 4
    public function hasDirective(string $name): bool
31
    {
32 4
        return \iterator_count($this->getDirectives($name)) > 0;
33
    }
34
35
    /**
36
     * @param string|null $name
37
     * @return iterable|DirectiveInvocation[]|\Generator
38
     */
39 4
    public function getDirectives(string $name = null): iterable
40
    {
41 4
        foreach ($this->directives as $directive) {
42 1
            if ($name === null || $name === $directive->getDefinition()->getName()) {
43 1
                yield $directive;
44
            }
45
        }
46 4
    }
47
48
    /**
49
     * @param DirectiveInvocation $invocation
50
     * @return ProvidesDirectives
51
     */
52 1
    public function withDirective(DirectiveInvocation $invocation): ProvidesDirectives
53
    {
54 1
        $this->directives[] = $invocation;
55
56 1
        return $this;
57
    }
58
}
59