FoldersExtension::register()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 3.0004

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 1
dl 0
loc 37
ccs 26
cts 27
cp 0.963
crap 3.0004
rs 9.328
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\Extension\Folders;
4
5
use League\Plates;
6
7
final class FoldersExtension implements Plates\Extension
8
{
9 24
    public function register(Plates\Engine $plates) {
10 24
        $c = $plates->getContainer();
11 24
        $c->add('folders.folders', []);
12 24
        $c->wrapStack('path.resolvePath', function($stack, $c) {
13 24
            $config = $c;
14 24
            return array_merge($stack, [
15 24
                'folders' => foldersResolvePath(
16 24
                    $c->get('folders.folders'),
17 24
                    $c->get('config')['folder_separator'],
18 24
                    $c->get('fileExists')
19
                )
20
            ]);
21 24
        });
22 24
        $c->wrapComposed('path.normalizeName', function($composed, $c) {
23 24
            return array_merge($composed, [
24 24
                'folders.stripFolders' => stripFoldersNormalizeName($c->get('folders.folders'))
25
            ]);
26 24
        });
27
28 24
        $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...
29 24
            'folder_separator' => '::',
30
        ]);
31 24
        $plates->addMethods([
32 24
            'addFolder' => function($plates, $folder, $prefixes, $fallback = false) {
33 4
                $prefixes = is_string($prefixes) ? [$prefixes] : $prefixes;
34 4
                if ($fallback) {
35
                    $prefixes[] = '';
36
                }
37 4
                $plates->getContainer()->merge('folders.folders', [
38
                    $folder => [
39 4
                        'folder' => $folder,
40 4
                        'prefixes' => $prefixes,
41
                    ]
42
                ]);
43 24
            },
44
        ]);
45 24
    }
46
}
47