Engine   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 9

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 61
ccs 34
cts 35
cp 0.9714
rs 10
c 2
b 0
f 0
wmc 10
lcom 1
cbo 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A create() 0 6 1
A createWithConfig() 0 14 1
A render() 0 7 1
A addMethods() 0 3 1
A __call() 0 8 2
A register() 0 3 1
A getContainer() 0 3 1
1
<?php
2
3
namespace League\Plates;
4
5
/** API for the Plates system. This wraps the container and allows extensions to add methods for syntactic sugar. */
6
final class Engine
7
{
8
    private $container;
9
10 24
    public function __construct(Util\Container $container = null) {
11 24
        $this->container = $container ?: Util\Container::create(['engine_methods' => []]);
12 24
    }
13
14
    /** Create a configured engine and set the base dir and extension optionally */
15 8
    public static function create($base_dir, $ext = null) {
16 8
        return self::createWithConfig(array_filter([
17 8
            'base_dir' => $base_dir,
18 8
            'ext' => $ext
19
        ]));
20
    }
21
22
    /** Create a configured engine and pass in an array to configure after extension registration */
23 24
    public static function createWithConfig(array $config = []) {
24 24
        $plates = new self();
25
26 24
        $plates->register(new PlatesExtension());
27 24
        $plates->register(new Extension\Data\DataExtension());
28 24
        $plates->register(new Extension\Path\PathExtension());
29 24
        $plates->register(new Extension\RenderContext\RenderContextExtension());
30 24
        $plates->register(new Extension\LayoutSections\LayoutSectionsExtension());
31 24
        $plates->register(new Extension\Folders\FoldersExtension());
32
33 24
        $plates->addConfig($config);
0 ignored issues
show
Documentation Bug introduced by RJ Garcia
The method addConfig 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...
34
35 24
        return $plates;
36
    }
37
38
    /** @return string */
39 24
    public function render(string $template_name, array $data = [], array $attributes = []): string {
40 24
        return $this->container->get('renderTemplate')->renderTemplate(new Template(
41 24
            $template_name,
42 24
            $data,
43 24
            $attributes
44
        ));
45
    }
46
47 24
    public function addMethods(array $methods) {
48 24
        $this->container->merge('engine_methods', $methods);
49 24
    }
50 24
    public function __call($method, array $args) {
51 24
        $methods = $this->container->get('engine_methods');
52 24
        if (isset($methods[$method])) {
53 24
            return $methods[$method]($this, ...$args);
54
        }
55
56
        throw new \BadMethodCallException("No method {$method} found for engine.");
57
    }
58
59 24
    public function register(Extension $extension) {
60 24
        $extension->register($this);
61 24
    }
62
63 24
    public function getContainer() {
64 24
        return $this->container;
65
    }
66
}
67