Completed
Push — master ( 22bc57...1ce92a )
by
unknown
29:24 queued 15:51
created

RenderContextExtension::register()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 84
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 5.1281

Importance

Changes 0
Metric Value
cc 5
eloc 59
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 84
ccs 48
cts 58
cp 0.8276
crap 5.1281
rs 8.35

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\Extension\RenderContext;
4
5
use League\Plates;
6
7
/** The render context extension provides a RenderContext object and functions to be used within the render context object. This RenderContext object is injected into the template data to allow usefulness in the templates. */
8
final class RenderContextExtension implements Plates\Extension
9
{
10 16
    public function register(Plates\Engine $plates) {
11 16
        $c = $plates->getContainer();
12 16
        $c->addStack('renderContext.func', function($c) {
13
            return [
14 16
                'notFound' => notFoundFunc(),
15 16
                'plates' => Plates\Util\stackGroup([
16 16
                    splitByNameFunc($c->get('renderContext.func.funcs')),
17 16
                    aliasNameFunc($c->get('renderContext.func.aliases')),
18
                ]),
19
            ];
20 16
        });
21 16
        $c->add('renderContext.func.aliases', [
22 16
            'e' => 'escape',
23
            '__invoke' => 'escape',
24
            'stop' => 'end',
25
        ]);
26 16
        $c->add('renderContext.func.funcs', function($c) {
27 16
            $template_args = assertTemplateArgsFunc();
28 16
            $one_arg = assertArgsFunc(1);
29 16
            $config = $c->get('config');
30
31
            return [
32 16
                'insert' => [insertFunc(), $template_args],
33
                'escape' => [
34 16
                    isset($config['escape_flags'], $config['escape_encoding'])
35
                        ? escapeFunc($config['escape_flags'], $config['escape_encoding'])
36 16
                        : escapeFunc(),
37 16
                    $one_arg,
38
                ],
39 16
                'data' => [templateDataFunc(), assertArgsFunc(0, 1)],
40 16
                'name' => [accessTemplatePropFunc('name')],
41 16
                'context' => [accessTemplatePropFunc('context')],
42 16
                'component' => [componentFunc(), $template_args],
43 16
                'slot' => [slotFunc(), $one_arg],
44 16
                'end' => [endFunc()]
45
            ];
46 16
        });
47 16
        $c->add('include.bind', function($c) {
48
            return renderContextBind($c->get('config')['render_context_var_name']);
49 16
        });
50 16
        $c->add('renderContext.factory', function($c) {
51 16
            return RenderContext::factory(
52
                function() use ($c) { return $c->get('renderTemplate'); },
53
                $c->get('renderContext.func')
54 16
            );
55 16
        });
56
57 16
        $plates->defineConfig([
0 ignored issues
show
Documentation Bug introduced by
The method defineConfig does not exist on object<League\Plates\Engine>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
58 16
            'render_context_var_name' => 'v',
59 16
            'escape_encoding' => null,
60
            'escape_flags' => null,
61 16
        ]);
62
        $plates->pushComposers(function($c) {
0 ignored issues
show
Documentation Bug introduced by
The method pushComposers does not exist on object<League\Plates\Engine>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
63 16
            return [
64
                'renderContext.renderContext' => renderContextCompose(
65 16
                    $c->get('renderContext.factory'),
66 16
                    $c->get('config')['render_context_var_name']
67
                )
68
            ];
69
        });
70
71
        $plates->addMethods([
72
            'registerFunction' => function(Plates\Engine $e, $name, callable $func, callable $assert_args = null, $simple = true) {
73
                $c = $e->getContainer();
74 16
                $func = $simple ? wrapSimpleFunc($func) : $func;
75
76 16
                $c->wrap('renderContext.func.funcs', function($funcs, $c) use ($name, $func, $assert_args) {
0 ignored issues
show
Unused Code introduced by
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...
77 16
                    $funcs[$name] = $assert_args ? [$assert_args, $func] : [$func];
78
                    return $funcs;
79 16
                });
80 16
            },
81 16
            'addFuncs' => function(Plates\Engine $e, callable $add_funcs, $simple = false) {
82 16
                $e->getContainer()->wrap('renderContext.func.funcs', function($funcs, $c) use ($add_funcs, $simple) {
83 16
                    $new_funcs = $simple
84
                        ? array_map(wrapSimpleFunc::class, $add_funcs($c))
85 16
                        : $add_funcs($c);
86
                    return array_merge($funcs, $new_funcs);
87 16
                });
88
            },
89
            'wrapFuncs' => function(Plates\Engine $e, callable $wrap_funcs) {
90
                $e->getContainer()->wrap('renderContext.func.funcs', $wrap_funcs);
91
            }
92
        ]);
93
    }
94
}
95