Completed
Push — master ( 494091...32c874 )
by David
27s
created

PluginTif::compile()   C

Complexity

Conditions 16
Paths 113

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 58
Code Lines 29

Importance

Changes 0
Metric Value
cc 16
eloc 29
nc 113
nop 3
dl 0
loc 58
rs 6.0298
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
 * Copyright (c) 2013-2017
4
 *
5
 * @category  Library
6
 * @package   Dwoo\Plugins\Functions
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2017 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.2
13
 * @date      2017-01-06
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Functions;
18
19
use Dwoo\Compiler;
20
use Dwoo\Core;
21
use Dwoo\Exception;
22
use Dwoo\Compilation\Exception as CompilationException;
23
use Dwoo\ICompilable;
24
use Dwoo\Plugin;
25
use Dwoo\Plugins\Blocks\PluginIf;
26
27
/**
28
 * Ternary if operation.
29
 * It evaluates the first argument and returns the second if it's true, or the third if it's false
30
 * <pre>
31
 *  * rest : you can not use named parameters to call this, use it either with three arguments in the correct order
32
 *  (expression, true result, false result) or write it as in php (expression ? true result : false result)
33
 * </pre>
34
 * This software is provided 'as-is', without any express or implied warranty.
35
 * In no event will the authors be held liable for any damages arising from the use of this software.
36
 */
37
class PluginTif extends Plugin implements ICompilable
38
{
39
    /**
40
     * @param Compiler $compiler
41
     * @param array    $rest
42
     * @param array    $tokens
43
     *
44
     * @return mixed|string
45
     * @throws CompilationException
46
     */
47
    public static function compile(Compiler $compiler, array $rest, array $tokens)
48
    {
49
        // load if plugin
50
        if (!class_exists(Core::NAMESPACE_PLUGINS_BLOCKS . 'PluginIf')) {
51
            try {
52
                $compiler->getDwoo()->getLoader()->loadPlugin('if');
53
            }
54
            catch (Exception $e) {
55
                throw new CompilationException($compiler, 'Tif: the if plugin is required to use Tif');
56
            }
57
        }
58
59
        if (count($rest) == 1) {
60
            return $rest[0];
61
        }
62
63
        // fetch false result and remove the ":" if it was present
64
        $falseResult = array_pop($rest);
65
66
        if (trim(end($rest), '"\'') === ':') {
67
            // remove the ':' if present
68
            array_pop($rest);
69
        } elseif (trim(end($rest), '"\'') === '?' || count($rest) === 1) {
70
            if ($falseResult === '?' || $falseResult === ':') {
71
                throw new CompilationException($compiler,
72
                    'Tif: incomplete tif statement, value missing after ' . $falseResult);
73
            }
74
            // there was in fact no false result provided, so we move it to be the true result instead
75
            $trueResult = $falseResult;
76
            $falseResult = "''";
77
        }
78
79
        // fetch true result if needed
80
        if (!isset($trueResult)) {
81
            $trueResult = array_pop($rest);
82
            // no true result provided so we use the expression arg
83
            if ($trueResult === '?') {
84
                $trueResult = true;
85
            }
86
        }
87
88
        // remove the '?' if present
89
        if (trim(end($rest), '"\'') === '?') {
90
            array_pop($rest);
91
        }
92
93
        // check params were correctly provided
94
        if (empty($rest) || $trueResult === null || $falseResult === null) {
95
            throw new CompilationException($compiler,
96
                'Tif: you must provide three parameters serving as <expression> ? <true value> : <false value>');
97
        }
98
99
        // parse condition
100
        $condition = PluginIf::replaceKeywords($rest, $tokens, $compiler);
101
102
        return '((' . implode(' ', $condition) . ') ? ' . ($trueResult === true ? implode(' ',
103
                $condition) : $trueResult) . ' : ' . $falseResult . ')';
104
    }
105
}