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.
Passed
Push — master ( 074ced...44b942 )
by Christian
03:04
created

src/Extension/RouterTwigExtension.php (2 issues)

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\Extension\AbstractExtension;
19
use Twig\Extension\InitRuntimeInterface;
20
use Twig\TwigFilter;
21
use Twig\TwigFunction;
22
23
final class RouterTwigExtension extends AbstractExtension implements InitRuntimeInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Twig\Extension\InitRuntimeInterface has been deprecated: since Twig 2.7, to be removed in 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

23
final class RouterTwigExtension extends AbstractExtension implements /** @scrutinizer ignore-deprecated */ InitRuntimeInterface

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
24
{
25
    /**
26
     * @var RouterInterface
27
     */
28
    private $router;
29
30
    /**
31
     * @var array
32
     */
33
    private $options;
34
35
    /**
36
     * @var Environment
37
     */
38
    private $environment;
39
40
    /**
41
     * @param RouterInterface $router
42
     * @param array           $options
43
     *
44
     * @throws LoaderError
45
     */
46
    public function __construct(RouterInterface $router, array $options = [])
47
    {
48
        $this->router  = $router;
49
        $this->options = $options;
50
51
        if (!isset($this->options['template'])) {
52
            throw new LoaderError('Pager template is not set.');
53
        }
54
        if (!isset($this->options['extremeLimit'])) {
55
            throw new LoaderError('Pager extreme limit is not set.');
56
        }
57
        if (!isset($this->options['nearbyLimit'])) {
58
            throw new LoaderError('Pager nearby limit is not set.');
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function initRuntime(\Twig_Environment $environment): void
66
    {
67
        $this->environment = $environment;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getFunctions()
74
    {
75
        return [
76
            new TwigFunction('routeExists', [$this, 'routeExists']),
77
            new TwigFunction('page_pager', [$this, 'generatePager'], [
78
                'is_safe' => ['html'],
79
            ]),
80
        ];
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getFilters()
87
    {
88
        return [
89
            new TwigFilter('splitTag', [$this, 'splitTag']),
90
        ];
91
    }
92
93
    /**
94
     * @param string $name
95
     *
96
     * @return bool
97
     */
98
    public function routeExists(string $name): bool
99
    {
100
        return null !== $this->router->getRouteCollection()->get($name);
101
    }
102
103
    /**
104
     * @param string $text
105
     * @param string $tag
106
     *
107
     * @return string[]
108
     */
109
    public function splitTag(string $text, string $tag): array
110
    {
111
        if (!$tag) {
112
            return [$text];
113
        }
114
115
        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...
116
    }
117
118
    /**
119
     * @param BasePager $pager
120
     * @param array     $options
121
     *
122
     * @throws \Twig_Error_Loader
123
     * @throws \Twig_Error_Runtime
124
     * @throws \Twig_Error_Syntax
125
     *
126
     * @return string
127
     */
128
    public function generatePager(BasePager $pager, array $options = []): string
129
    {
130
        $data = array_merge(array_merge($this->options, $options), [
131
            'itemsCount'  => $pager->count(),
132
            'limit'       => $pager->getMaxPerPage(),
133
            'currentPage' => $pager->getPage(),
134
        ]);
135
136
        $data['lastPage'] = $this->getNumPages((int) $data['limit'], (int) $data['itemsCount']);
137
138
        return $this->environment->render($data['template'], $data);
139
    }
140
141
    /**
142
     * @param int $limit
143
     * @param int $count
144
     *
145
     * @return int
146
     */
147
    protected function getNumPages(int $limit, int $count): int
148
    {
149
        if ($limit < 1) {
150
            return 1;
151
        }
152
153
        return (int) ceil($count / $limit);
154
    }
155
}
156