PlatesExtension::register()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 2.0011

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 64
ccs 43
cts 46
cp 0.9348
crap 2.0011
rs 8.7853
c 0
b 0
f 0

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
namespace League\Plates;
4
5
final class PlatesExtension implements Extension
6
{
7 24
    public function register(Engine $plates) {
8 24
        $c = $plates->getContainer();
9
10 24
        $c->add('config', [
11 24
            'validate_paths' => true,
12
            'php_extensions' => ['php', 'phtml'],
13
            'image_extensions' => ['png', 'jpg'],
14
        ]);
15
        $c->addComposed('compose', function() { return []; });
16 24
        $c->add('fileExists', function($c) {
0 ignored issues
show
Unused Code introduced by RJ Garcia
The parameter $c 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...
17 24
            return 'file_exists';
18 24
        });
19 24
        $c->add('renderTemplate', function($c) {
20 24
            $rt = new RenderTemplate\FileSystemRenderTemplate([
21
                [
22 24
                    Template\matchExtensions($c->get('config')['php_extensions']),
23 24
                    new RenderTemplate\PhpRenderTemplate($c->get('renderTemplate.bind'))
24
                ],
25
                [
26 24
                    Template\matchExtensions($c->get('config')['image_extensions']),
27 24
                    RenderTemplate\MapContentRenderTemplate::base64Encode(new RenderTemplate\StaticFileRenderTemplate())
28
                ],
29
                [
30 24
                    Template\matchStub(true),
31 24
                    new RenderTemplate\StaticFileRenderTemplate(),
32
                ]
33
            ]);
34 24
            if ($c->get('config')['validate_paths']) {
35 24
                $rt = new RenderTemplate\ValidatePathRenderTemplate($rt, $c->get('fileExists'));
36
            }
37 24
            $rt = array_reduce($c->get('renderTemplate.factories'), function($rt, $create) {
38 24
                return $create($rt);
39 24
            }, $rt);
40 24
            $rt = new RenderTemplate\ComposeRenderTemplate($rt, $c->get('compose'));
41 24
            return $rt;
42 24
        });
43 24
        $c->add('renderTemplate.bind', function() {
44 24
            return Util\id();
45 24
        });
46 24
        $c->add('renderTemplate.factories', function() {
47 24
            return [];
48 24
        });
49
50 24
        $plates->addMethods([
51
            'pushComposers' => function(Engine $e, $def_composer) {
52 24
                $e->getContainer()->wrapComposed('compose', function($composed, $c) use ($def_composer) {
53 24
                    return array_merge($composed, $def_composer($c));
54 24
                });
55 24
            },
56
            'unshiftComposers' => function(Engine $e, $def_composer) {
57
                $e->getContainer()->wrapComposed('compose', function($composed, $c) use ($def_composer) {
58
                    return array_merge($def_composer($c), $composed);
59
                });
60 24
            },
61 24
            'addConfig' => function(Engine $e, array $config) {
62 24
                $e->getContainer()->merge('config', $config);
63 24
            },
64
            /** merges in config values, but will defer to values already set in the config */
65 24
            'defineConfig' => function(Engine $e, array $config_def) {
66 24
                $config = $e->getContainer()->get('config');
67 24
                $e->getContainer()->add('config', array_merge($config_def, $config));
68 24
            },
69
        ]);
70 24
    }
71
}
72