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.

SprintfEngine::render()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 2
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of StringTemplate.
4
 *
5
 * (c) 2013 Nicolò Martini
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace StringTemplate;
12
13
/**
14
 * Class Engine
15
 *
16
 * Replace placeholder in strings with nested (array) values, allowing an optional
17
 * sprintf-like parameter after the placeholder name.
18
 *
19
 * Example:
20
 * <code>
21
 * $engine->render('This is {a} and these are {c.0%E} and {c.1}', ['a' => 'b', 'c' => [1, 'e']]);
22
 * //Prints "This is b and these are d and 1.000000E+0"
23
 * </code>
24
 */
25
class SprintfEngine extends Engine
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function render($template, $value)
31
    {
32
        //Performance: if there are no '%' fallback to Engine
33
        if (strstr($template, '%') == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strstr($template, '%') of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
34
            return parent::render($template, $value);
35
        }
36
37
        $result = $template;
38
        if (!is_array($value))
39
            $value = array('' => $value);
40
41
        foreach (new NestedKeyIterator(new RecursiveArrayOnlyIterator($value)) as $key => $value) {
42
            $pattern = "/" . $this->left . $key . "(%[^" . $this->right . "]+)?" . $this->right . "/";
43
            preg_match_all($pattern, $template, $matches);
44
            $substs = array_map(function ($match) use ($value) {
45
                return $match !== '' ? sprintf($match, $value) : $value;
46
            }, $matches[1]);
47
            $result = str_replace($matches[0], $substs, $result);
48
        }
49
        return $result;
50
    }
51
}