clearCompiledTemplate()   F
last analyzed

Complexity

Conditions 35
Paths 709

Size

Total Lines 95
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 35
eloc 70
c 1
b 0
f 0
nc 709
nop 4
dl 0
loc 95
rs 0.4041

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
 * Smarty Method ClearCompiledTemplate
5
 *
6
 * Smarty::clearCompiledTemplate() method
7
 *
8
 * @package    Smarty
9
 * @subpackage PluginsInternal
10
 * @author     Uwe Tews
11
 */
12
class Smarty_Internal_Method_ClearCompiledTemplate
13
{
14
    /**
15
     * Valid for Smarty object
16
     *
17
     * @var int
18
     */
19
    public $objMap = 1;
20
21
    /**
22
     * Delete compiled template file
23
     *
24
     * @api  Smarty::clearCompiledTemplate()
25
     * @link http://www.smarty.net/docs/en/api.clear.compiled.template.tpl
26
     *
27
     * @param \Smarty $smarty
28
     * @param string  $resource_name template name
29
     * @param string  $compile_id    compile id
30
     * @param integer $exp_time      expiration time
31
     *
32
     * @return int number of template files deleted
33
     * @throws \SmartyException
34
     */
35
    public function clearCompiledTemplate(Smarty $smarty, $resource_name = null, $compile_id = null, $exp_time = null)
36
    {
37
        // clear template objects cache
38
        $smarty->_clearTemplateCache();
39
        $_compile_dir = $smarty->getCompileDir();
40
        if ($_compile_dir === '/') { //We should never want to delete this!
41
            return 0;
42
        }
43
        $_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null;
44
        $_dir_sep = $smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
45
        if (isset($resource_name)) {
46
            $_save_stat = $smarty->caching;
47
            $smarty->caching = Smarty::CACHING_OFF;
48
            /* @var Smarty_Internal_Template $tpl */
49
            $tpl = $smarty->createTemplate($resource_name);
50
            $smarty->caching = $_save_stat;
51
            if (!$tpl->source->handler->uncompiled && !$tpl->source->handler->recompiled && $tpl->source->exists) {
52
                $_resource_part_1 = basename(str_replace('^', DIRECTORY_SEPARATOR, $tpl->compiled->filepath));
53
                $_resource_part_1_length = strlen($_resource_part_1);
54
            } else {
55
                return 0;
56
            }
57
            $_resource_part_2 = str_replace('.php', '.cache.php', $_resource_part_1);
58
            $_resource_part_2_length = strlen($_resource_part_2);
59
        }
60
        $_dir = $_compile_dir;
61
        if ($smarty->use_sub_dirs && isset($_compile_id)) {
62
            $_dir .= $_compile_id . $_dir_sep;
63
        }
64
        if (isset($_compile_id)) {
65
            $_compile_id_part = $_compile_dir . $_compile_id . $_dir_sep;
66
            $_compile_id_part_length = strlen($_compile_id_part);
67
        }
68
        $_count = 0;
69
        try {
70
            $_compileDirs = new RecursiveDirectoryIterator($_dir);
71
            // NOTE: UnexpectedValueException thrown for PHP >= 5.3
72
        } catch (Exception $e) {
73
            return 0;
74
        }
75
        $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
76
        foreach ($_compile as $_file) {
77
            if (substr(basename($_file->getPathname()), 0, 1) === '.') {
78
                continue;
79
            }
80
            $_filepath = (string)$_file;
81
            if ($_file->isDir()) {
82
                if (!$_compile->isDot()) {
0 ignored issues
show
Bug introduced by
The method isDot() does not exist on RecursiveIteratorIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
                if (!$_compile->/** @scrutinizer ignore-call */ isDot()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
                    // delete folder if empty
84
                    @rmdir($_file->getPathname());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

84
                    /** @scrutinizer ignore-unhandled */ @rmdir($_file->getPathname());

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
85
                }
86
            } else {
87
                // delete only php files
88
                if (substr($_filepath, -4) !== '.php') {
89
                    continue;
90
                }
91
                $unlink = false;
92
                if ((!isset($_compile_id) ||
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (! IssetNode || IssetNod...ce_part_2_length) === 0, Probably Intended Meaning: ! IssetNode || (IssetNod...e_part_2_length) === 0)
Loading history...
93
                     (isset($_filepath[ $_compile_id_part_length ]) &&
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_compile_id_part_length does not seem to be defined for all execution paths leading up to this point.
Loading history...
94
                      $a = !strncmp($_filepath, $_compile_id_part, $_compile_id_part_length)))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_compile_id_part does not seem to be defined for all execution paths leading up to this point.
Loading history...
Unused Code introduced by
The assignment to $a is dead and can be removed.
Loading history...
95
                    && (!isset($resource_name) || (isset($_filepath[ $_resource_part_1_length ])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_resource_part_1_length does not seem to be defined for all execution paths leading up to this point.
Loading history...
96
                                                   && substr_compare(
97
                                                          $_filepath,
98
                                                          $_resource_part_1,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_resource_part_1 does not seem to be defined for all execution paths leading up to this point.
Loading history...
99
                                                          -$_resource_part_1_length,
100
                                                          $_resource_part_1_length
101
                                                      ) === 0) || (isset($_filepath[ $_resource_part_2_length ])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_resource_part_2_length does not seem to be defined for all execution paths leading up to this point.
Loading history...
102
                                                                   && substr_compare(
103
                                                                          $_filepath,
104
                                                                          $_resource_part_2,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_resource_part_2 does not seem to be defined for all execution paths leading up to this point.
Loading history...
105
                                                                          -$_resource_part_2_length,
106
                                                                          $_resource_part_2_length
107
                                                                      ) === 0))
108
                ) {
109
                    if (isset($exp_time)) {
110
                        if (is_file($_filepath) && time() - filemtime($_filepath) >= $exp_time) {
111
                            $unlink = true;
112
                        }
113
                    } else {
114
                        $unlink = true;
115
                    }
116
                }
117
                if ($unlink && is_file($_filepath) && @unlink($_filepath)) {
118
                    $_count++;
119
                    if (function_exists('opcache_invalidate')
120
                        && (!function_exists('ini_get') || strlen(ini_get('opcache.restrict_api')) < 1)
121
                    ) {
122
                        opcache_invalidate($_filepath, true);
123
                    } elseif (function_exists('apc_delete_file')) {
124
                        apc_delete_file($_filepath);
125
                    }
126
                }
127
            }
128
        }
129
        return $_count;
130
    }
131
}
132