setConfiguration()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\DefaultAttributes;
15
16
use League\CommonMark\Event\DocumentParsedEvent;
17
use League\CommonMark\Extension\Attributes\Util\AttributesHelper;
18
use League\Config\ConfigurationAwareInterface;
19
use League\Config\ConfigurationInterface;
20
21
final class ApplyDefaultAttributesProcessor implements ConfigurationAwareInterface
22
{
23
    private ConfigurationInterface $config;
24
25
    public function onDocumentParsed(DocumentParsedEvent $event): void
26
    {
27
        /** @var array<string, array<string, mixed>> $map */
28
        $map = $this->config->get('default_attributes');
29
30
        // Don't bother iterating if no default attributes are configured
31
        if (! $map) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $map of type array<string,array<string,mixed>> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
32
            return;
33
        }
34
35
        foreach ($event->getDocument()->iterator() as $node) {
36
            // Check to see if any default attributes were defined
37
            if (($attributesToApply = $map[\get_class($node)] ?? []) === []) {
38
                continue;
39
            }
40
41
            $newAttributes = [];
42
            foreach ($attributesToApply as $name => $value) {
43
                if (\is_callable($value)) {
44
                    $value = $value($node);
45
                    // Callables are allowed to return `null` indicating that no changes should be made
46
                    if ($value !== null) {
47
                        $newAttributes[$name] = $value;
48
                    }
49
                } else {
50
                    $newAttributes[$name] = $value;
51
                }
52
            }
53
54
            // Merge these attributes into the node
55
            if (\count($newAttributes) > 0) {
56
                $node->data->set('attributes', AttributesHelper::mergeAttributes($node, $newAttributes));
57
            }
58
        }
59
    }
60
61
    public function setConfiguration(ConfigurationInterface $configuration): void
62
    {
63
        $this->config = $configuration;
64
    }
65
}
66