GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RouterTwigExtension   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
A getFilters() 0 4 1
A getNumPages() 0 3 1
A routeExists() 0 3 1
A splitTag() 0 7 3
A getFunctions() 0 6 1
A generatePager() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\Twig\Extension;
13
14
use Sonata\DatagridBundle\Pager\BasePager;
15
use Symfony\Component\Routing\RouterInterface;
16
use Twig\Environment;
17
use Twig\Error\LoaderError;
18
use Twig\Error\RuntimeError;
19
use Twig\Error\SyntaxError;
20
use Twig\Extension\AbstractExtension;
21
use Twig\TwigFilter;
22
use Twig\TwigFunction;
23
24
final class RouterTwigExtension extends AbstractExtension
25
{
26
    /**
27
     * @var RouterInterface
28
     */
29
    private $router;
30
31
    /**
32
     * @var array
33
     */
34
    private $options;
35
36
    /**
37
     * @var Environment
38
     */
39
    private $environment;
40
41
    /**
42
     * @throws LoaderError
43
     */
44
    public function __construct(Environment $environment, RouterInterface $router, array $options = [])
45
    {
46
        $this->environment = $environment;
47
        $this->router      = $router;
48
        $this->options     = $options;
49
50
        if (!isset($this->options['template'])) {
51
            throw new LoaderError('Pager template is not set.');
52
        }
53
        if (!isset($this->options['extremeLimit'])) {
54
            throw new LoaderError('Pager extreme limit is not set.');
55
        }
56
        if (!isset($this->options['nearbyLimit'])) {
57
            throw new LoaderError('Pager nearby limit is not set.');
58
        }
59
    }
60
61
    public function getFunctions()
62
    {
63
        return [
64
            new TwigFunction('routeExists', [$this, 'routeExists']),
65
            new TwigFunction('page_pager', [$this, 'generatePager'], [
66
                'is_safe' => ['html'],
67
            ]),
68
        ];
69
    }
70
71
    public function getFilters()
72
    {
73
        return [
74
            new TwigFilter('splitTag', [$this, 'splitTag']),
75
        ];
76
    }
77
78
    public function routeExists(string $name): bool
79
    {
80
        return null !== $this->router->getRouteCollection()->get($name);
81
    }
82
83
    /**
84
     * @return string[]
85
     */
86
    public function splitTag(string $text, string $tag): array
87
    {
88
        if ('' === trim($tag)) {
89
            return [$text];
90
        }
91
92
        return preg_split('/(?=<'.$tag.'([^>])*>)/', $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) ?: [$text];
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_split('/(?=<...APTURE) ?: array($text) returns an array which contains values of type array which are incompatible with the documented value type string.
Loading history...
93
    }
94
95
    /**
96
     * @throws LoaderError
97
     * @throws RuntimeError
98
     * @throws SyntaxError
99
     */
100
    public function generatePager(BasePager $pager, array $options = []): string
101
    {
102
        $data = array_merge(array_merge($this->options, $options), [
103
            'itemsCount'  => $pager->count(),
104
            'limit'       => max(1, $pager->getMaxPerPage()),
105
            'currentPage' => $pager->getPage(),
106
        ]);
107
108
        $data['lastPage'] = self::getNumPages((int) $data['limit'], (int) $data['itemsCount']);
109
110
        return $this->environment->render($data['template'], $data);
111
    }
112
113
    private static function getNumPages(int $limit, int $count): int
114
    {
115
        return (int) ceil($count / $limit);
116
    }
117
}
118