SetsUpMarkdownConverter   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A registerPreProcessor() 0 4 3
A registerPostProcessor() 0 4 3
A enableDynamicExtensions() 0 8 3
A enableConfigDefinedExtensions() 0 4 2
A registerPostProcessors() 0 13 1
A mergeMarkdownConfiguration() 0 3 1
A initializeExtension() 0 3 1
A registerPreProcessors() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Concerns\Internal;
6
7
use Hyde\Facades\Config;
8
use Hyde\Markdown\Processing\BladeDownProcessor;
9
use Hyde\Markdown\Processing\ShortcodeProcessor;
10
use Hyde\Markdown\Processing\CodeblockFilepathProcessor;
11
use Hyde\Markdown\Processing\DynamicMarkdownLinkProcessor;
12
use Torchlight\Commonmark\V2\TorchlightExtension;
13
14
use function array_merge;
15
use function in_array;
16
17
/**
18
 * Sets up the Markdown converter for the Markdown service.
19
 *
20
 * @internal This trait is not covered by the backward compatibility promise.
21
 *
22
 * @see \Hyde\Framework\Services\MarkdownService
23
 */
24
trait SetsUpMarkdownConverter
25
{
26
    protected function enableDynamicExtensions(): void
27
    {
28
        if ($this->canEnableTorchlight()) {
0 ignored issues
show
Bug introduced by
It seems like canEnableTorchlight() 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

28
        if ($this->/** @scrutinizer ignore-call */ canEnableTorchlight()) {
Loading history...
29
            $this->addExtension(TorchlightExtension::class);
0 ignored issues
show
Bug introduced by
It seems like addExtension() 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

29
            $this->/** @scrutinizer ignore-call */ 
30
                   addExtension(TorchlightExtension::class);
Loading history...
30
        }
31
32
        if (Config::getBool('markdown.allow_html', false)) {
33
            $this->enableAllHtmlElements();
0 ignored issues
show
Bug introduced by
It seems like enableAllHtmlElements() 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

33
            $this->/** @scrutinizer ignore-call */ 
34
                   enableAllHtmlElements();
Loading history...
34
        }
35
    }
36
37
    protected function enableConfigDefinedExtensions(): void
38
    {
39
        foreach (Config::getArray('markdown.extensions', []) as $extensionClassName) {
40
            $this->addExtension($extensionClassName);
41
        }
42
    }
43
44
    protected function mergeMarkdownConfiguration(): void
45
    {
46
        $this->config = array_merge(Config::getArray('markdown.config', []), $this->config);
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
    }
48
49
    public function initializeExtension(string $extensionClassName): void
50
    {
51
        $this->converter->getEnvironment()->addExtension(new $extensionClassName());
52
    }
53
54
    protected function registerPreProcessors(): void
55
    {
56
        $this->registerPreProcessor(BladeDownProcessor::class, Config::getBool('markdown.enable_blade', false));
57
58
        $this->registerPreProcessor(ShortcodeProcessor::class);
59
        $this->registerPreProcessor(CodeblockFilepathProcessor::class);
60
    }
61
62
    protected function registerPostProcessors(): void
63
    {
64
        $this->registerPostProcessor(
65
            BladeDownProcessor::class,
66
            Config::getBool('markdown.enable_blade', false)
67
        );
68
69
        $this->registerPostProcessor(
70
            CodeblockFilepathProcessor::class,
71
            Config::getBool('markdown.features.codeblock_filepaths', true)
72
        );
73
74
        $this->registerPostProcessor(DynamicMarkdownLinkProcessor::class);
75
    }
76
77
    protected function registerPreProcessor(string $class, bool $when = true): void
78
    {
79
        if (! in_array($class, $this->preprocessors) && $when) {
80
            $this->preprocessors[] = $class;
0 ignored issues
show
Bug Best Practice introduced by
The property preprocessors does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
81
        }
82
    }
83
84
    protected function registerPostProcessor(string $class, bool $when = true): void
85
    {
86
        if (! in_array($class, $this->postprocessors) && $when) {
87
            $this->postprocessors[] = $class;
0 ignored issues
show
Bug Best Practice introduced by
The property postprocessors does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
88
        }
89
    }
90
}
91