Completed
Push — master ( 640397...4ec2f8 )
by Mikael
02:52
created

Variable::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Anax\TextFilter\Filter;
4
5
use \Symfony\Component\Yaml\Yaml;
6
7
/**
8
 * Filter to replace variable expressions like %var% with the respective value
9
 * defined in the frontmatter.
10
 */
11
class Variable implements FilterInterface
12
{
13
    /**
14
     * @var array $frontmatter parsed as frontmatter, accumulated when parsing
15
     *                         the text.
16
     */
17
    protected $frontmatter;
18
19
20
21
    /**
22
     * Parse the text through the filter and do what the filter does,
23
     * return the resulting text and with some optional additional details,
24
     * all wrapped in an key-value array.
25
     *
26
     * @param string $text        to parse.
27
     * @param array  $frontmatter optional to use while parsing.
28
     * @param array  $options     custom options to use while parsing.
29
     *
30
     * @return array with the resulting text and frontmatter.
31
     */
32 4
    public function parse($text, array $frontmatter, array $options = [])
33
    {
34 4
        $this->frontmatter = $frontmatter;
35 4
        $text = $this->parseForVariable($text);
36
37
        return [
38 4
            "text" => $text,
39 4
            "frontmatter" => $this->frontmatter,
40
        ];
41
    }
42
43
44
45
    /**
46
     * Detect variable expressions %var% and replace with their values, if the
47
     * variable is defined.
48
     *
49
     * @param string $text to parse for variable expressions..
50
     *
51
     * @return string as parsed text where variable are replaced with values.
52
     */
53 4
    protected function parseForVariable($text)
54
    {
55 4
        $text = preg_replace_callback(
56 4
            "/(%[a-zA-Z_$][\w$]*%)/",
57 4
            [$this, "getVariableValue"],
58 4
            $text
59
        );
60
61 4
        return $text;
62
    }
63
64
65
66
    /**
67
     * Detect and extract inline variables.
68
     *
69
     * @param string $text to parse.
0 ignored issues
show
Bug introduced by
There is no parameter named $text. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
70
     *
71
     * @return boolean|void true when block is found and parsed, else void.
72
     *
73
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
74
     */
75 4
    protected function getVariableValue(array $matches)
76
    {
77 4
        $res = $matches[0];
78 4
        $variable = substr($matches[0], 1, -1);
79
80 4
        if (isset($this->frontmatter[$variable])) {
81 3
            $res = $this->frontmatter[$variable];
82
        }
83
84 4
        return $res;
85
    }
86
}
87