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

PluginInclude   A

Coupling/Cohesion

Components 1
Dependencies 4

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 12.16 %

Importance

Changes 0
Metric Value
dl 9
loc 74
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
F process() 9 60 12

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Exception as Exception;
20
use Dwoo\Plugin;
21
use Dwoo\Security\Exception as SecurityException;
22
23
/**
24
 * Inserts another template into the current one
25
 * <pre>
26
 *  * file : the resource name of the template
27
 *  * cache_time : cache length in seconds
28
 *  * cache_id : cache identifier for the included template
29
 *  * compile_id : compilation identifier for the included template
30
 *  * data : data to feed into the included template, it can be any array and will default to $_root (the current data)
31
 *  * assign : if set, the output of the included template will be saved in this variable instead of being output
32
 *  * rest : any additional parameter/value provided will be added to the data array
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 PluginInclude extends Plugin
38
{
39
    /**
40
     * @param        $file
41
     * @param null   $cache_time
42
     * @param null   $cache_id
43
     * @param null   $compile_id
44
     * @param string $data
45
     * @param null   $assign
46
     * @param array  $rest
47
     *
48
     * @return string
49
     */
50
    public function process($file, $cache_time = null, $cache_id = null, $compile_id = null, $data = '_root', $assign = null, array $rest = array())
51
    {
52
        $include = null;
53
        if ($file === '') {
54
            return '';
55
        }
56
57 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...
58
            // resource:identifier given, extract them
59
            $resource   = $m[1];
60
            $identifier = $m[2];
61
        } else {
62
            // get the current template's resource
63
            $resource   = $this->core->getTemplate()->getResourceName();
64
            $identifier = $file;
65
        }
66
67
        try {
68
            $include = $this->core->templateFactory($resource, $identifier, $cache_time, $cache_id, $compile_id);
69
        }
70
        catch (SecurityException $e) {
71
            $this->core->triggerError('Include : Security restriction : ' . $e->getMessage(), E_USER_WARNING);
72
        }
73
        catch (Exception $e) {
74
            $this->core->triggerError('Include : ' . $e->getMessage(), E_USER_WARNING);
75
        }
76
77
        if ($include === null) {
78
            $this->core->triggerError('Include : Resource "' . $resource . ':' . $identifier . '" not found.',
79
                E_USER_WARNING);
80
        } elseif ($include === false) {
81
            $this->core->triggerError('Include : Resource "' . $resource . '" does not support includes.',
82
                E_USER_WARNING);
83
        }
84
85
        if (is_string($data)) {
86
            $vars = $this->core->readVar($data);
87
        } else {
88
            $vars = $data;
89
        }
90
91
        if (count($rest)) {
92
            $vars = $rest + $vars;
93
        }
94
95
        $clone = clone $this->core;
96
        $out   = $clone->get($include, $vars);
97
98
        if ($assign !== null) {
99
            $this->core->assignInScope($out, $assign);
100
        }
101
102
        foreach ($clone->getReturnValues() as $name => $value) {
103
            $this->core->assignInScope($value, $name);
104
        }
105
106
        if ($assign === null) {
107
            return $out;
108
        }
109
    }
110
}