Completed
Pull Request — master (#75)
by Vladimir
02:42
created

TwigStakxBridgeFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 11
dl 0
loc 57
ccs 15
cts 24
cp 0.625
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B createTwigEnvironment() 0 54 5
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Templating\Twig;
9
10
use allejo\stakx\Configuration;
11
use allejo\stakx\Filesystem\FilesystemLoader as fs;
12
use allejo\stakx\RuntimeStatus;
13
use allejo\stakx\Service;
14
use Psr\Log\LoggerInterface;
15
use Twig_Environment;
16
use Twig_Extension_Debug;
17
18
class TwigStakxBridgeFactory
19
{
20 48
    public static function createTwigEnvironment(
21
        Configuration $configuration,
22
        TwigExtension $twigExtension,
23
        LoggerInterface $logger
24
    ) {
25 48
        $loader = new TwigFileLoader([
26 48
            Service::getWorkingDirectory(),
27
        ]);
28
29 48
        $theme = $configuration->getTheme();
30
31
        // Only load a theme if one is specified and actually exists
32 48
        if ($theme !== null)
33
        {
34
            try
35
            {
36
                $loader->addPath(fs::absolutePath('_themes', $theme), 'theme');
0 ignored issues
show
Documentation introduced by
'_themes' is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to FilesystemLoader::absolutePath() has too many arguments starting with $theme.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
37
            }
38
            catch (\Twig_Error_Loader $e)
39
            {
40
                $logger->error('The following theme could not be loaded: {theme}', [
41
                    'theme' => $theme,
42
                ]);
43
                $logger->error($e->getMessage());
44
            }
45
        }
46
47 48
        $twig = new Twig_Environment($loader, [
48 48
            'autoescape' => $configuration->getTwigAutoescape(),
49
            'auto_reload' => true,
50 48
            'cache' => fs::absolutePath('.stakx-cache/twig'),
0 ignored issues
show
Documentation introduced by
'.stakx-cache/twig' is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
        ]);
52
53 48
        $twig->addExtension($twigExtension);
54
55 48
        $profiler = null;
56
57 48
        if (Service::hasRunTimeFlag(RuntimeStatus::IN_PROFILE_MODE))
58
        {
59
            $profiler = new \Twig_Profiler_Profile();
60
            $twig->addExtension(new \Twig_Extension_Profiler($profiler));
61
        }
62
63 48
        if ($configuration->isDebug())
64
        {
65
            $twig->addExtension(new Twig_Extension_Debug());
66
            $twig->enableDebug();
67
        }
68
69 48
        $bridge = new TwigStakxBridge($twig);
70 48
        $bridge->setProfiler($profiler);
71
72 48
        return $bridge;
73
    }
74
}
75