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

RenderableEnvironmentTrait::getRenderersForClass()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 22
rs 9.6111
cc 5
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji\Traits;
6
7
use UnicornFail\Emoji\Renderer\NodeRendererInterface;
8
use UnicornFail\Emoji\Util\PrioritizedList;
9
10
trait RenderableEnvironmentTrait
11
{
12
    /**
13
     * @var array<string, PrioritizedList<NodeRendererInterface>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, Prioritize...NodeRendererInterface>> at position 4 could not be parsed: Expected '>' at position 4, but found 'PrioritizedList'.
Loading history...
14
     *
15
     * @psalm-readonly-allow-private-mutation
16
     */
17
    private $renderersByClass = [];
18
19
    /**
20
     * {@inheritDoc}
21
     */
22
    public function addRenderer(string $nodeClass, NodeRendererInterface $renderer, int $priority = 0)
23
    {
24
        $this->assertUninitialized('Failed to add renderer.');
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

24
        $this->/** @scrutinizer ignore-call */ 
25
               assertUninitialized('Failed to add renderer.');
Loading history...
25
26
        if (! isset($this->renderersByClass[$nodeClass])) {
27
            /** @var PrioritizedList<NodeRendererInterface> $renderers */
28
            $renderers = new PrioritizedList();
29
30
            $this->renderersByClass[$nodeClass] = $renderers;
31
        }
32
33
        $this->renderersByClass[$nodeClass]->add($renderer, $priority);
34
        $this->injectEnvironmentAndConfigurationIfNeeded($renderer);
0 ignored issues
show
Bug introduced by
It seems like injectEnvironmentAndConfigurationIfNeeded() 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

34
        $this->/** @scrutinizer ignore-call */ 
35
               injectEnvironmentAndConfigurationIfNeeded($renderer);
Loading history...
35
36
        return $this;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     *
42
     * @return PrioritizedList<NodeRendererInterface>
43
     */
44
    public function getRenderersForClass(string $nodeClass): iterable
45
    {
46
        $this->initialize();
0 ignored issues
show
Bug introduced by
It seems like initialize() 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

46
        $this->/** @scrutinizer ignore-call */ 
47
               initialize();
Loading history...
47
48
        // If renderers are defined for this specific class, return them immediately
49
        if (isset($this->renderersByClass[$nodeClass])) {
50
            return $this->renderersByClass[$nodeClass];
51
        }
52
53
        while (\class_exists($parent = $parent ?? $nodeClass) && $parent = \get_parent_class($parent)) {
54
            if (! isset($this->renderersByClass[$parent])) {
55
                continue;
56
            }
57
58
            // "Cache" this result to avoid future loops
59
            return $this->renderersByClass[$nodeClass] = $this->renderersByClass[$parent];
60
        }
61
62
        /** @var PrioritizedList<NodeRendererInterface> $renderers */
63
        $renderers = new PrioritizedList();
64
65
        return $renderers;
66
    }
67
}
68