Completed
Push — master ( e46319...8937f6 )
by Martin
19:10
created

CommonMarkTwig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Webuni\CommonMark\TwigRenderer;
4
5
use League\CommonMark\Converter;
6
use League\CommonMark\MarkdownConverter;
7
use Twig\Environment as Twig;
8
use Twig\Loader\ChainLoader;
9
use Twig\Loader\FilesystemLoader;
10
use Twig\Loader\LoaderInterface;
11
12
final class CommonMarkTwig
13
{
14
    private function __construct()
15
    {
16
    }
17
18
    public static function createTwigLoader(): LoaderInterface
19
    {
20
        return new FilesystemLoader([dirname(__DIR__) . '/templates']);
21
    }
22
23
    public static function configureTwig(Twig $twig): void
24
    {
25
        $loader = $twig->getLoader();
26
        if (!$loader instanceof ChainLoader) {
27
            $loader = new ChainLoader([$loader]);
28
            $twig->setLoader($loader);
29
        }
30
        $loader->addLoader(self::createTwigLoader());
31
32
        if (!$twig->hasExtension(CommonMarkExtension::class)) {
33
            $twig->addExtension(new CommonMarkExtension());
34
        }
35
    }
36
37
    public static function setTwigRenderer(MarkdownConverter $converter, Twig $twig = null): void
38
    {
39
        $twig = $twig ?? new Twig(new ChainLoader());
40
        self::configureTwig($twig);
41
42
        $renderer = new TwigRenderer($converter->getEnvironment(), $twig);
0 ignored issues
show
Compatibility introduced by
$converter->getEnvironment() of type object<League\CommonMark\EnvironmentInterface> is not a sub-type of object<League\CommonMark\Environment>. It seems like you assume a concrete implementation of the interface League\CommonMark\EnvironmentInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
43
        ConvertorModifier::setRenderer($converter, $renderer);
44
    }
45
46
}
47