BaseDataSource   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
eloc 12
c 2
b 0
f 1
dl 0
loc 30
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A registerAsTwigFunction() 0 3 1
A getFunctions() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\AjaxcomBundle\DataSource;
6
7
use Twig\Extension\AbstractExtension;
8
use Twig\TwigFunction;
9
10
/**
11
 * Class BaseDataSource.
12
 *
13
 * @author Ivan Barlog <[email protected]>
14
 */
15
class BaseDataSource extends AbstractExtension
16
{
17
    protected const TWIG_FUNCTION_SUFFIX = 'Provider';
18
19
    /**
20
     * @return array
21
     */
22
    public function getFunctions()
23
    {
24
        $functions = [];
25
        $class = new \ReflectionClass(get_class($this));
26
27
        if (self::class === $class->name) {
28
            return $functions;
29
        }
30
31
        foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
32
            if (false === ($offset = strpos($method->getName(), self::TWIG_FUNCTION_SUFFIX))) {
33
                continue;
34
            }
35
36
            $functions[] = $this->registerAsTwigFunction(substr($method->getName(), 0, $offset));
37
        }
38
39
        return $functions;
40
    }
41
42
    private function registerAsTwigFunction(string $name): ?TwigFunction
43
    {
44
        return new TwigFunction($name, [$this, $name.self::TWIG_FUNCTION_SUFFIX]);
45
    }
46
}
47