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

PluginLoadTemplates::compile()   B

Complexity

Conditions 8
Paths 21

Duplication

Lines 28
Ratio 48.28 %

Size

Total Lines 58
Code Lines 35

Importance

Changes 0
Metric Value
cc 8
eloc 35
nc 21
nop 2
dl 28
loc 58
rs 7.1977
c 0
b 0
f 0

How to fix   Long Method   

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\Compilation\Exception as CompilationException;
21
use Dwoo\ICompilable;
22
use Dwoo\Plugin;
23
24
/**
25
 * Loads sub-templates contained in an external file
26
 * <pre>
27
 *  * file : the resource name of the file to load
28
 * </pre>
29
 * This software is provided 'as-is', without any express or implied warranty.
30
 * In no event will the authors be held liable for any damages arising from the use of this software.
31
 */
32
class PluginLoadTemplates extends Plugin implements ICompilable
33
{
34
    /**
35
     * @param Compiler $compiler
36
     * @param string   $file
37
     *
38
     * @return string
39
     * @throws CompilationException
40
     */
41
    public static function compile(Compiler $compiler, $file)
42
    {
43
        $file = substr($file, 1, - 1);
44
45
        if ($file === '') {
46
            return '';
47
        }
48
49 View Code Duplication
        if (preg_match('#^([a-z]{2,}):(.*)$#i', $file, $m)) {
0 ignored issues
show
Duplication introduced by DSanchez
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            // resource:identifier given, extract them
51
            $resource   = $m[1];
52
            $identifier = $m[2];
53
        } else {
54
            // get the current template's resource
55
            $resource   = $compiler->getDwoo()->getTemplate()->getResourceName();
56
            $identifier = $file;
57
        }
58
59
        $tpl = $compiler->getDwoo()->templateFactory($resource, $identifier);
60
61 View Code Duplication
        if ($tpl === null) {
0 ignored issues
show
Duplication introduced by DSanchez
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
            throw new CompilationException($compiler,
63
                'Load Templates : Resource "' . $resource . ':' . $identifier . '" not found.');
64
        } elseif ($tpl === false) {
65
            throw new CompilationException($compiler,
66
                'Load Templates : Resource "' . $resource . '" does not support includes.');
67
        }
68
69
        $cmp = clone $compiler;
70
        $cmp->compile($compiler->getDwoo(), $tpl);
71
        foreach ($cmp->getTemplatePlugins() as $template => $args) {
72
            $compiler->addTemplatePlugin($template, $args['params'], $args['uuid'], $args['body']);
73
        }
74
        foreach ($cmp->getUsedPlugins() as $plugin => $type) {
75
            $compiler->addUsedPlugin($plugin, $type);
76
        }
77
78
        $out = '\'\';// checking for modification in ' . $resource . ':' . $identifier . "\r\n";
79
80
        $modCheck = $tpl->getIsModifiedCode();
81
82 View Code Duplication
        if ($modCheck) {
0 ignored issues
show
Duplication introduced by DSanchez
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
            $out .= 'if (!(' . $modCheck . ')) { ob_end_clean(); return false; }';
84
        } else {
85
            $out .= 'try {
86
	$tpl = $this->templateFactory("' . $resource . '", "' . $identifier . '");
87
} catch (Dwoo\Exception $e) {
88
	$this->triggerError(\'Load Templates : Resource <em>' . $resource . '</em> was not added to Dwoo, can not extend <em>' . $identifier . '</em>\', E_USER_WARNING);
89
}
90
if ($tpl === null)
91
	$this->triggerError(\'Load Templates : Resource "' . $resource . ':' . $identifier . '" was not found.\', E_USER_WARNING);
92
elseif ($tpl === false)
93
	$this->triggerError(\'Load Templates : Resource "' . $resource . '" does not support extends.\', E_USER_WARNING);
94
if ($tpl->getUid() != "' . $tpl->getUid() . '") { ob_end_clean(); return false; }';
95
        }
96
97
        return $out;
98
    }
99
}