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 ( f5e6b3...c7b627 )
by Christian
02:34
created

RouterTwigExtension::getFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
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;
0 ignored issues
show
Documentation Bug introduced by
$environment is of type Twig_Environment, but the property $environment was declared to be of type Twig\Environment. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
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