Smarty_Internal_Runtime_CacheResourceFile::clear()   F
last analyzed

Complexity

Conditions 37
Paths 256

Size

Total Lines 109
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 37
eloc 71
c 1
b 0
f 0
nc 256
nop 5
dl 0
loc 109
rs 2.6333

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
 * Smarty cache resource file clear method
4
 *
5
 * @package    Smarty
6
 * @subpackage PluginsInternal
7
 * @author     Uwe Tews
8
 */
9
10
/**
11
 * Smarty Internal Runtime Cache Resource File Class
12
 *
13
 * @package    Smarty
14
 * @subpackage PluginsInternal
15
 */
16
class Smarty_Internal_Runtime_CacheResourceFile
17
{
18
    /**
19
     * Empty cache for a specific template
20
     *
21
     * @param Smarty  $smarty
22
     * @param string  $resource_name template name
23
     * @param string  $cache_id      cache id
24
     * @param string  $compile_id    compile id
25
     * @param integer $exp_time      expiration time (number of seconds, not timestamp)
26
     *
27
     * @return integer number of cache files deleted
28
     */
29
    public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
30
    {
31
        $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
32
        $_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null;
33
        $_dir_sep = $smarty->use_sub_dirs ? '/' : '^';
34
        $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0;
35
        $_dir = $smarty->getCacheDir();
36
        if ($_dir === '/') { //We should never want to delete this!
37
            return 0;
38
        }
39
        $_dir_length = strlen($_dir);
40
        if (isset($_cache_id)) {
41
            $_cache_id_parts = explode('|', $_cache_id);
42
            $_cache_id_parts_count = count($_cache_id_parts);
43
            if ($smarty->use_sub_dirs) {
44
                foreach ($_cache_id_parts as $id_part) {
45
                    $_dir .= $id_part . '/';
46
                }
47
            }
48
        }
49
        if (isset($resource_name)) {
50
            $_save_stat = $smarty->caching;
51
            $smarty->caching = Smarty::CACHING_LIFETIME_CURRENT;
52
            $tpl = new $smarty->template_class($resource_name, $smarty);
53
            $smarty->caching = $_save_stat;
54
            // remove from template cache
55
            $tpl->source; // have the template registered before unset()
56
            if ($tpl->source->exists) {
57
                $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath));
58
            } else {
59
                return 0;
60
            }
61
        }
62
        $_count = 0;
63
        $_time = time();
64
        if (file_exists($_dir)) {
65
            $_cacheDirs = new RecursiveDirectoryIterator($_dir);
66
            $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
67
            foreach ($_cache as $_file) {
68
                if (substr(basename($_file->getPathname()), 0, 1) === '.') {
69
                    continue;
70
                }
71
                $_filepath = (string)$_file;
72
                // directory ?
73
                if ($_file->isDir()) {
74
                    if (!$_cache->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

74
                    if (!$_cache->/** @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...
75
                        // delete folder if empty
76
                        @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

76
                        /** @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...
77
                    }
78
                } else {
79
                    // delete only php files
80
                    if (substr($_filepath, -4) !== '.php') {
81
                        continue;
82
                    }
83
                    $_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length)));
84
                    $_parts_count = count($_parts);
85
                    // check name
86
                    if (isset($resource_name)) {
87
                        if ($_parts[ $_parts_count - 1 ] !== $_resourcename_parts) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_resourcename_parts does not seem to be defined for all execution paths leading up to this point.
Loading history...
88
                            continue;
89
                        }
90
                    }
91
                    // check compile id
92
                    if (isset($_compile_id) && (!isset($_parts[ $_parts_count - 2 - $_compile_id_offset ])
93
                                                || $_parts[ $_parts_count - 2 - $_compile_id_offset ] !== $_compile_id)
94
                    ) {
95
                        continue;
96
                    }
97
                    // check cache id
98
                    if (isset($_cache_id)) {
99
                        // count of cache id parts
100
                        $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset :
101
                            $_parts_count - 1 - $_compile_id_offset;
102
                        if ($_parts_count < $_cache_id_parts_count) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_cache_id_parts_count does not seem to be defined for all execution paths leading up to this point.
Loading history...
103
                            continue;
104
                        }
105
                        for ($i = 0; $i < $_cache_id_parts_count; $i++) {
106
                            if ($_parts[ $i ] !== $_cache_id_parts[ $i ]) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_cache_id_parts does not seem to be defined for all execution paths leading up to this point.
Loading history...
107
                                continue 2;
108
                            }
109
                        }
110
                    }
111
                    if (is_file($_filepath)) {
112
                        // expired ?
113
                        if (isset($exp_time)) {
114
                            if ($exp_time < 0) {
115
                                preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_filepath), $match);
116
                                if ($_time < (filemtime($_filepath) + $match[ 1 ])) {
117
                                    continue;
118
                                }
119
                            } else {
120
                                if ($_time - filemtime($_filepath) < $exp_time) {
121
                                    continue;
122
                                }
123
                            }
124
                        }
125
                        $_count += @unlink($_filepath) ? 1 : 0;
126
                        if (function_exists('opcache_invalidate')
127
                            && (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
128
                        ) {
129
                            opcache_invalidate($_filepath, true);
130
                        } elseif (function_exists('apc_delete_file')) {
131
                            apc_delete_file($_filepath);
132
                        }
133
                    }
134
                }
135
            }
136
        }
137
        return $_count;
138
    }
139
}
140