Completed
Branch master (e379bd)
by Pierre-Henry
33:06
created

Smarty_Internal_Runtime_CacheResourceFile::clear()   F

Complexity

Conditions 34
Paths 256

Size

Total Lines 106
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 34
eloc 68
nc 256
nop 5
dl 0
loc 106
rs 3.4111
c 0
b 0
f 0

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

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...
80
                    }
81
                } else {
82
                    // delete only php files
83
                    if (substr($_filepath, -4) !== '.php') {
84
                        continue;
85
                    }
86
                    $_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length)));
87
                    $_parts_count = count($_parts);
88
                    // check name
89
                    if (isset($resource_name)) {
90
                        if ($_parts[ $_parts_count - 1 ] != $_resourcename_parts) {
0 ignored issues
show
Bug introduced by
The variable $_resourcename_parts does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
91
                            continue;
92
                        }
93
                    }
94
                    // check compile id
95
                    if (isset($_compile_id) && (!isset($_parts[ $_parts_count - 2 - $_compile_id_offset ]) ||
96
                                                $_parts[ $_parts_count - 2 - $_compile_id_offset ] != $_compile_id)
97
                    ) {
98
                        continue;
99
                    }
100
                    // check cache id
101
                    if (isset($_cache_id)) {
102
                        // count of cache id parts
103
                        $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset :
104
                            $_parts_count - 1 - $_compile_id_offset;
105
                        if ($_parts_count < $_cache_id_parts_count) {
0 ignored issues
show
Bug introduced by
The variable $_cache_id_parts_count does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
106
                            continue;
107
                        }
108
                        for ($i = 0; $i < $_cache_id_parts_count; $i ++) {
109
                            if ($_parts[ $i ] != $_cache_id_parts[ $i ]) {
0 ignored issues
show
Bug introduced by
The variable $_cache_id_parts does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
110
                                continue 2;
111
                            }
112
                        }
113
                    }
114
                    // expired ?
115
                    if (isset($exp_time)) {
116
                        if ($exp_time < 0) {
117
                            preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_file), $match);
118
                            if ($_time < (@filemtime($_file) + $match[ 1 ])) {
119
                                continue;
120
                            }
121
                        } else {
122
                            if ($_time - @filemtime($_file) < $exp_time) {
123
                                continue;
124
                            }
125
                        }
126
                    }
127
                    $_count += @unlink($_filepath) ? 1 : 0;
128
                    if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) {
129
                        opcache_invalidate($_filepath, true);
130
                    }
131
                }
132
            }
133
        }
134
        return $_count;
135
    }
136
}