Completed
Push — master ( 7e0874...14a89c )
by Vladimir
04:01 queued 01:49
created

TwigStakxBridgeFactory::createTwigEnvironment()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.3183

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 15
cts 24
cp 0.625
rs 8.6925
c 0
b 0
f 0
cc 5
nc 12
nop 3
crap 6.3183

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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