PluginWith   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 3 1
A preProcessing() 0 4 1
A postProcessing() 0 17 2
1
<?php
2
/**
3
 * Copyright (c) 2013-2016
4
 *
5
 * @category  Library
6
 * @package   Dwoo\Plugins\Blocks
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2016 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.0
13
 * @date      2016-09-19
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Blocks;
18
19
use Dwoo\IElseable;
20
use Dwoo\Compiler;
21
use Dwoo\Block\Plugin as BlockPlugin;
22
use Dwoo\ICompilable\Block as ICompilableBlock;
23
24
/**
25
 * Moves the scope down into the provided variable, allowing you to use shorter
26
 * variable names if you repeatedly access values into a single array.
27
 * The with block won't display anything at all if the provided scope is empty,
28
 * so in effect it acts as {if $var}*content*{/if}
29
 * <pre>
30
 *  * var : the variable name to move into
31
 * </pre>
32
 * Example :
33
 * instead of the following :
34
 * <code>
35
 * {if $long.boring.prefix}
36
 *   {$long.boring.prefix.val} - {$long.boring.prefix.secondVal} - {$long.boring.prefix.thirdVal}
37
 * {/if}
38
 * </code>
39
 * you can use :
40
 * <code>
41
 * {with $long.boring.prefix}
42
 *   {$val} - {$secondVal} - {$thirdVal}
43
 * {/with}
44
 * </code>
45
 * This software is provided 'as-is', without any express or implied warranty.
46
 * In no event will the authors be held liable for any damages arising from the use of this software.
47
 */
48
class PluginWith extends BlockPlugin implements ICompilableBlock, IElseable
49
{
50
    protected static $cnt = 0;
51
52
    /**
53
     * @param $var
54
     */
55
    public function init($var)
0 ignored issues
show
Unused Code introduced by
The parameter $var is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57
    }
58
59
    /**
60
     * @param Compiler $compiler
61
     * @param array    $params
62
     * @param string   $prepend
63
     * @param string   $append
64
     * @param string   $type
65
     *
66
     * @return string
67
     */
68
    public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
69
    {
70
        return '';
71
    }
72
73
    /**
74
     * @param Compiler $compiler
75
     * @param array    $params
76
     * @param string   $prepend
77
     * @param string   $append
78
     * @param string   $content
79
     *
80
     * @return string
81
     */
82
    public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content)
83
    {
84
        $rparams = $compiler->getRealParams($params);
85
        $cparams = $compiler->getCompiledParams($params);
86
87
        $compiler->setScope($rparams['var']);
88
89
        $pre = Compiler::PHP_OPEN . 'if (' . $cparams['var'] . ')' . "\n{\n" . '$_with' . (self::$cnt) . ' = $this->setScope("' . $rparams['var'] . '");' . "\n/* -- start with output */\n" . Compiler::PHP_CLOSE;
90
91
        $post = Compiler::PHP_OPEN . "\n/* -- end with output */\n" . '$this->setScope($_with' . (self::$cnt ++) . ', true);' . "\n}\n" . Compiler::PHP_CLOSE;
92
93
        if (isset($params['hasElse'])) {
94
            $post .= $params['hasElse'];
95
        }
96
97
        return $pre . $content . $post;
98
    }
99
}
100