IfTag::render()   C
last analyzed

Complexity

Conditions 16
Paths 8

Size

Total Lines 109
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 72
nc 8
nop 1
dl 0
loc 109
rs 5.5666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Platine Template
5
 *
6
 * Platine Template is a template engine that has taken a lot of inspiration from Django.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Template
11
 * Copyright (c) 2014 Guz Alexander, http://guzalexander.com
12
 * Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com
13
 * Copyright (c) 2006 Mateo Murphy
14
 *
15
 * Permission is hereby granted, free of charge, to any person obtaining a copy
16
 * of this software and associated documentation files (the "Software"), to deal
17
 * in the Software without restriction, including without limitation the rights
18
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
 * copies of the Software, and to permit persons to whom the Software is
20
 * furnished to do so, subject to the following conditions:
21
 *
22
 * The above copyright notice and this permission notice shall be included in all
23
 * copies or substantial portions of the Software.
24
 *
25
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
 * SOFTWARE.
32
 */
33
34
/**
35
 *  @file IfTag.php
36
 *
37
 *  The "if" Template tag class
38
 *
39
 *  @package    Platine\Template\Tag
40
 *  @author Platine Developers Team
41
 *  @copyright  Copyright (c) 2020
42
 *  @license    http://opensource.org/licenses/MIT  MIT License
43
 *  @link   https://www.platine-php.com
44
 *  @version 1.0.0
45
 *  @filesource
46
 */
47
48
declare(strict_types=1);
49
50
namespace Platine\Template\Tag;
51
52
use Platine\Template\Exception\ParseException;
53
use Platine\Template\Parser\AbstractCondition;
54
use Platine\Template\Parser\Context;
55
use Platine\Template\Parser\Lexer;
56
use Platine\Template\Parser\Parser;
57
use Platine\Template\Parser\Token;
58
59
/**
60
 * @class IfTag
61
 * @package Platine\Template\Tag
62
 */
63
class IfTag extends AbstractCondition
64
{
65
    /**
66
     * holding the nodes to render for each logical block
67
     * @var array<int, mixed>
68
     */
69
    protected array $nodeListHolders = [];
70
71
    /**
72
     * holding the block type, block markup (conditions)
73
     *  and block node list
74
     * @var array<int, mixed>
75
     */
76
    protected array $blocks = [];
77
78
    /**
79
    * {@inheritdoc}
80
    */
81
    public function __construct(string $markup, array &$tokens, Parser $parser)
82
    {
83
        //Initialize
84
        $this->nodeListHolders[count($this->blocks)] = [];
85
        $this->nodeList = & $this->nodeListHolders[count($this->blocks)];
86
        array_push($this->blocks, ['if', $markup, & $this->nodeList]);
87
88
        parent::__construct($markup, $tokens, $parser);
89
    }
90
91
    /**
92
    * {@inheritdoc}
93
    */
94
    public function render(Context $context): string
95
    {
96
        $context->push();
97
        $lexerLogical = new Lexer('/\s+(and|or)\s+/');
98
        $lexerConditional = new Lexer(
99
            '/('
100
                . Token::QUOTED_FRAGMENT
101
                . ')\s*([=!<>a-z_]+)?\s*('
102
                . Token::QUOTED_FRAGMENT
103
                . ')?/'
104
        );
105
106
        $result = '';
107
        foreach ($this->blocks as $block) {
108
            if ($block[0] === 'else') {
109
                $result = $this->renderAll($block[2], $context);
110
111
                break;
112
            }
113
114
            if ($block[0] === 'if' || $block[0] === 'elseif') {
115
                // Extract logical operators
116
                $lexerLogical->matchAll($block[1]);
117
                $operators = $lexerLogical->getArrayMatch(1);
118
                // Extract individual conditions
119
                $individualConditions = $lexerLogical->split($block[1]);
120
121
                $conditions = [];
122
                foreach ($individualConditions as $condition) {
123
                    if ($lexerConditional->match($condition)) {
124
                        $left = $lexerConditional->isMatchNotNull(1)
125
                                ? $lexerConditional->getStringMatch(1)
126
                                : null;
127
                        $operator = $lexerConditional->isMatchNotNull(2)
128
                                ? $lexerConditional->getStringMatch(2)
129
                                : null;
130
                        $right = $lexerConditional->isMatchNotNull(3)
131
                                ? $lexerConditional->getStringMatch(3)
132
                                : null;
133
134
                        array_push($conditions, [
135
                            'left' => $left,
136
                            'operator' => $operator,
137
                            'right' => $right,
138
                        ]);
139
                    } else {
140
                        throw new ParseException(sprintf(
141
                            'Syntax Error in "%s" - Valid syntax: if [conditions]',
142
                            'if'
143
                        ));
144
                    }
145
                }
146
147
                if (count($operators) > 0) {
148
                    // If statement contains and/or
149
                    $display = $this->evaluateCondition(
150
                        $conditions[0]['left'],
151
                        $conditions[0]['right'],
152
                        $conditions[0]['operator'],
153
                        $context
154
                    );
155
156
                    foreach ($operators as $key => $operator) {
157
                        if ($operator === 'and') {
158
                            $display = (
159
                                $display
160
                                && $this->evaluateCondition(
161
                                    $conditions[$key + 1]['left'],
162
                                    $conditions[$key + 1]['right'],
163
                                    $conditions[$key + 1]['operator'],
164
                                    $context
165
                                )
166
                            );
167
                        } else {
168
                            $display = (
169
                                $display
170
                                || $this->evaluateCondition(
171
                                    $conditions[$key + 1]['left'],
172
                                    $conditions[$key + 1]['right'],
173
                                    $conditions[$key + 1]['operator'],
174
                                    $context
175
                                )
176
                            );
177
                        }
178
                    }
179
                } else {
180
                    // If statement is a single condition
181
                    $display = $this->evaluateCondition(
182
                        $conditions[0]['left'],
183
                        $conditions[0]['right'],
184
                        $conditions[0]['operator'],
185
                        $context
186
                    );
187
                }
188
189
                // hook for if not tag
190
                $display = $this->negateCondition($display);
191
192
                if ($display) {
193
                    $result = $this->renderAll($block[2], $context);
194
195
                    break;
196
                }
197
            }
198
        }
199
200
        $context->pop();
201
202
        return $result;
203
    }
204
205
    /**
206
     * Handler negate condition
207
     * @param mixed $value
208
     * @return mixed
209
     */
210
    protected function negateCondition(mixed $value): mixed
211
    {
212
        // no need to negate a condition in a regular `if`
213
        // tag (will do that in `ifnot` tag)
214
215
        return $value;
216
    }
217
218
    /**
219
    * {@inheritdoc}
220
    */
221
    protected function unknownTag(string $tag, string $param, array $tokens): void
222
    {
223
        if ($tag === 'else' || $tag === 'elseif') {
224
            // Update reference to node list holder for this block
225
            $this->nodeListHolders[count($this->blocks) + 1] = [];
226
            $this->nodeList = & $this->nodeListHolders[count($this->blocks) + 1];
227
            array_push($this->blocks, [$tag, $param, &$this->nodeList]);
228
        } else {
229
            parent::unknownTag($tag, $param, $tokens);
230
        }
231
    }
232
}
233