Passed
Pull Request — latest (#3)
by Mark
35:03
created

ExtensibleEnvironmentTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 17
c 1
b 0
f 0
dl 0
loc 69
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeExtensions() 0 15 4
A addExtension() 0 8 1
A defaultExtensions() 0 3 1
A getExtensions() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji\Traits;
6
7
use UnicornFail\Emoji\Extension\ExtensionInterface;
8
9
trait ExtensibleEnvironmentTrait
10
{
11
    /**
12
     * @var ExtensionInterface[]
13
     *
14
     * @psalm-readonly-allow-private-mutation
15
     */
16
    private $extensions = [];
17
18
    /**
19
     * @var bool
20
     *
21
     * @psalm-readonly-allow-private-mutation
22
     */
23
    private $extensionsInitialized = false;
24
25
    /**
26
     * @var ExtensionInterface[]
27
     *
28
     * @psalm-readonly-allow-private-mutation
29
     */
30
    private $uninitializedExtensions = [];
31
32
    /**
33
     * @return iterable<ExtensionInterface>
34
     */
35
    protected static function defaultExtensions(): iterable
36
    {
37
        return [];
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function addExtension(ExtensionInterface $extension)
44
    {
45
        $this->assertUninitialized('Failed to add extension.');
0 ignored issues
show
Bug introduced by
It seems like assertUninitialized() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        $this->/** @scrutinizer ignore-call */ 
46
               assertUninitialized('Failed to add extension.');
Loading history...
46
47
        $this->extensions[]              = $extension;
48
        $this->uninitializedExtensions[] = $extension;
49
50
        return $this;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     *
56
     * @return ExtensionInterface[]
57
     */
58
    public function getExtensions(): iterable
59
    {
60
        return $this->extensions;
61
    }
62
63
    protected function initializeExtensions(): void
64
    {
65
        if ($this->extensionsInitialized) {
66
            return;
67
        }
68
69
        // Ask all extensions to register their components.
70
        while (\count($this->uninitializedExtensions) > 0) {
71
            foreach ($this->uninitializedExtensions as $i => $extension) {
72
                $extension->register($this);
0 ignored issues
show
Bug introduced by
$this of type UnicornFail\Emoji\Traits...ensibleEnvironmentTrait is incompatible with the type UnicornFail\Emoji\Environment\EnvironmentInterface expected by parameter $environment of UnicornFail\Emoji\Extens...onInterface::register(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
                $extension->register(/** @scrutinizer ignore-type */ $this);
Loading history...
73
                unset($this->uninitializedExtensions[$i]);
74
            }
75
        }
76
77
        $this->extensionsInitialized = true;
78
    }
79
}
80