BaseDataSource::getFunctions()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 9
c 2
b 0
f 1
dl 0
loc 18
rs 9.9666
cc 4
nc 4
nop 0
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