Passed
Push — master ( 501fc3...f64e27 )
by Paweł
47:53
created

StringyExtension::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\CoreBundle\Twig;
16
17
class StringyExtension extends \Twig_Extension
18
{
19
    const EXCLUDE_FUNCTIONS = ['__construct', '__toString', 'create'];
20
21
    /**
22
     * @var \Twig_Environment
23
     */
24
    protected $environment;
25
26
    /**
27
     * @var array
28
     */
29
    protected $functions = [];
30
31
    /**
32
     * @var array
33
     */
34
    protected $filters = [];
35
36 83
    public function __construct(\Twig_Environment $environment)
37
    {
38 83
        $this->environment = $environment;
39 83
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 11
    public function getFilters()
45
    {
46 11
        $this->lazyInit();
47
48 11
        return $this->filters;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 11
    public function getFunctions()
55
    {
56 11
        $this->lazyInit();
57
58 11
        return $this->functions;
59
    }
60
61
    /**
62
     * Initializes arrays of filters and functions.
63
     */
64 11
    private function lazyInit()
65
    {
66 11
        $stringyClass = new \ReflectionClass('Stringy\Stringy');
67 11
        $methods = $stringyClass->getMethods(\ReflectionMethod::IS_PUBLIC);
68
        $names = array_map(function ($value) {
69 11
            return $value->getName();
70 11
        }, $methods);
71
72 11
        foreach ($names as $name) {
73 11
            if (in_array($name, self::EXCLUDE_FUNCTIONS)) {
74 11
                continue;
75
            }
76
77 11
            $method = $stringyClass->getMethod($name);
78
79
            // Get the return type from the doc comment
80 11
            $doc = $method->getDocComment();
81 11
            if (strpos($doc, '@return bool')) {
82
                // Don't add functions which have the same name as any already in the environment
83 11
                if ($this->environment->getFunction($name)) {
84
                    continue;
85
                }
86
                $this->functions[$name] = new \Twig_SimpleFunction($name, function () use ($name) {
87 2
                    return call_user_func_array(['Stringy\StaticStringy', $name], func_get_args());
88 11
                });
89
            } else {
90
                // Don't add filters which have the same name as any already in the environment
91 11
                if ($this->environment->getFilter($name)) {
92 11
                    continue;
93
                }
94 11
                $this->filters[$name] = new \Twig_SimpleFilter($name, function () use ($name) {
95 3
                    return call_user_func_array(['Stringy\StaticStringy', $name], func_get_args());
96 11
                });
97
            }
98
        }
99 11
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 83
    public function getName()
105
    {
106 83
        return 'stringy_extension';
107
    }
108
}
109