Completed
Push — master ( 5ac91e...39483e )
by Kirill
38:30
created

BaseDirectivesContainer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 65
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getDirectives() 0 10 5
A hasDirective() 0 8 2
A getDirective() 0 8 2
A getNumberOfDirectives() 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\SDL\Base\Invocations\Directive;
11
12
use Railt\SDL\Contracts\Invocations\Directive\HasDirectives;
13
use Railt\SDL\Contracts\Invocations\DirectiveInvocation;
14
15
/**
16
 * Trait BaseDirectivesContainer
17
 *
18
 * @mixin HasDirectives
19
 */
20
trait BaseDirectivesContainer
21
{
22
    /**
23
     * Internal structure will look like this:
24
     * <code>
25
     *  protected $directives = [
26
     *      ['name' => DirectiveInvocation],
27
     *      ['name' => DirectiveInvocation],
28
     *      ['name' => DirectiveInvocation],
29
     *  ];
30
     * </code>
31
     *
32
     * @var array|DirectiveInvocation[]
33
     */
34
    protected $directives = [];
35
36
    /**
37
     * @param string|null $name
38
     * @return iterable|DirectiveInvocation[]
39
     */
40 179
    public function getDirectives(string $name = null): iterable
41
    {
42 179
        foreach ($this->directives as $definitions) {
43 90
            foreach ((array)$definitions as $found => $invocation) {
44 90
                if ($name === null || $name === $found) {
45 90
                    yield $found => $invocation;
46
                }
47
            }
48
        }
49 173
    }
50
51
    /**
52
     * @param string $name
53
     * @return bool
54
     */
55 30
    public function hasDirective(string $name): bool
56
    {
57 30
        foreach ($this->getDirectives($name) as $directive) {
58 24
            return true;
59
        }
60
61 30
        return false;
62
    }
63
64
    /**
65
     * @param string $name
66
     * @return null|DirectiveInvocation
67
     */
68 60
    public function getDirective(string $name): ?DirectiveInvocation
69
    {
70 60
        foreach ($this->getDirectives($name) as $directive) {
71 54
            return $directive;
72
        }
73
74 36
        return null;
75
    }
76
77
    /**
78
     * @return int
79
     */
80 48
    public function getNumberOfDirectives(): int
81
    {
82 48
        return \count($this->directives);
83
    }
84
}
85