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

TwigStakxBridgeFactory::createTwigEnvironment()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 69
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5.675

Importance

Changes 0
Metric Value
dl 0
loc 69
ccs 21
cts 30
cp 0.7
rs 8.56
c 0
b 0
f 0
cc 5
eloc 40
nc 12
nop 8
crap 5.675

How to fix   Long Method    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * @copyright 2018 Vladimir Jimenez
4
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
5
 */
6
7
namespace allejo\stakx\Templating\Twig;
8
9
use allejo\stakx\Command\BuildableCommand;
10
use allejo\stakx\Configuration;
11
use allejo\stakx\Filesystem\FilesystemLoader as fs;
12
use allejo\stakx\Manager\CollectionManager;
13
use allejo\stakx\Manager\DataManager;
14
use allejo\stakx\Manager\MenuManager;
15
use allejo\stakx\Manager\PageManager;
16
use allejo\stakx\Service;
17
use allejo\stakx\Filesystem\Filesystem;
18
use Psr\Log\LoggerInterface;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Twig_Environment;
21
use Twig_Extension_Debug;
22
23
class TwigStakxBridgeFactory
24
{
25 12
    public static function createTwigEnvironment(
26
        Configuration $configuration,
27
        CollectionManager $collectionManager,
28
        DataManager $dataManager,
29
        PageManager $pageManager,
30
        MenuManager $menuManager,
31
        TwigExtension $twigExtension,
32
        EventDispatcherInterface $eventDispatcher,
0 ignored issues
show
Unused Code introduced by
The parameter $eventDispatcher is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
        LoggerInterface $logger
34
    ) {
35 12
        $loader = new TwigFileLoader(array(
36 12
            Service::getWorkingDirectory(),
37
        ));
38
39 12
        $theme = $configuration->getTheme();
40
41
        // Only load a theme if one is specified and actually exists
42 12
        if ($theme !== null)
43
        {
44
            try
45
            {
46
                $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...
47
            }
48
            catch (\Twig_Error_Loader $e)
49
            {
50
                $logger->error('The following theme could not be loaded: {theme}', array(
51
                    'theme' => $theme,
52
                ));
53
                $logger->error($e->getMessage());
54
            }
55
        }
56
57 12
        $twig = new Twig_Environment($loader, array(
58 12
            'autoescape'  => $configuration->getTwigAutoescape(),
59
            'auto_reload' => true,
60 12
            '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...
61
        ));
62
63
        // We'll use this to access the current file in our Twig filters
64 12
        $twig->addGlobal('__currentTemplate', '');
65
66
        // Global variables maintained by stakx
67 12
        $twig->addGlobal('site', $configuration->getConfiguration());
68 12
        $twig->addGlobal('data', $dataManager->getJailedDataItems());
69 12
        $twig->addGlobal('collections', $collectionManager->getJailedCollections());
70 12
        $twig->addGlobal('menu', $menuManager->getSiteMenu());
71 12
        $twig->addGlobal('pages', $pageManager->getJailedStaticPageViews());
72
73 12
        $twig->addExtension($twigExtension);
74
75 12
        $profiler = null;
76
77 12
        if (Service::getParameter(BuildableCommand::BUILD_PROFILE))
78
        {
79
            $profiler = new \Twig_Profiler_Profile();
80
            $twig->addExtension(new \Twig_Extension_Profiler($profiler));
81
        }
82
83 12
        if ($configuration->isDebug())
84
        {
85
            $twig->addExtension(new Twig_Extension_Debug());
86
            $twig->enableDebug();
87
        }
88
89 12
        $bridge = new TwigStakxBridge($twig);
90 12
        $bridge->setProfiler($profiler);
91
92 12
        return $bridge;
93
    }
94
}
95