DataExtension::register()   B
last analyzed

Complexity

Conditions 6
Paths 1

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 6.0118

Importance

Changes 0
Metric Value
cc 6
nc 1
nop 1
dl 0
loc 36
ccs 27
cts 29
cp 0.931
crap 6.0118
rs 8.7217
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\Extension\Data;
4
5
use League\Plates;
6
7
/** The DataExtension adds the ability to hydrate data into a template before it gets rendered. */
8
final class DataExtension implements Plates\Extension
9
{
10 24
    public function register(Plates\Engine $plates) {
11 24
        $c = $plates->getContainer();
12 24
        $c->add('data.globals', []);
13 24
        $c->add('data.template_data', []);
14
15 24
        $plates->defineConfig(['merge_parent_data' => true]);
0 ignored issues
show
Documentation Bug introduced by RJ Garcia
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...
16 24
        $plates->pushComposers(function($c) {
0 ignored issues
show
Documentation Bug introduced by RJ Garcia
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...
17 24
            return array_filter([
18 24
                'data.addGlobals' => $c->get('data.globals') ? addGlobalsCompose($c->get('data.globals')) : null,
19 24
                'data.mergeParentData' => $c->get('config')['merge_parent_data'] ? mergeParentDataCompose() : null,
20 24
                'data.perTemplateData' => $c->get('data.template_data') ? perTemplateDataCompose($c->get('data.template_data')) : null,
21
            ]);
22 24
        });
23
24 24
        $plates->addMethods([
25 24
            'addGlobals' => function(Plates\Engine $e, array $data) {
26 4
                $c = $e->getContainer();
27 4
                $c->merge('data.globals', $data);
28 24
            },
29 24
            'addGlobal' => function(Plates\Engine $e, $name, $value) {
30
                $e->getContainer()->merge('data.globals', [$name => $value]);
31 24
            },
32 24
            'addData' => function(Plates\Engine $e, $data, $name = null) {
33 4
                if (!$name) {
34
                    return $e->addGlobals($data);
0 ignored issues
show
Documentation Bug introduced by RJ Garcia
The method addGlobals 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...
35
                }
36
37 4
                $template_data = $e->getContainer()->get('data.template_data');
38 4
                if (!isset($template_data[$name])) {
39 4
                    $template_data[$name] = [];
40
                }
41 4
                $template_data[$name] = array_merge($template_data[$name], $data);
42 4
                $e->getContainer()->add('data.template_data', $template_data);
43 24
            }
44
        ]);
45 24
    }
46
}
47