| Conditions | 34 |
| Paths | 256 |
| Total Lines | 106 |
| Code Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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()); |
||
|
|
|||
| 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) { |
||
| 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) { |
||
| 106 | continue; |
||
| 107 | } |
||
| 108 | for ($i = 0; $i < $_cache_id_parts_count; $i ++) { |
||
| 109 | if ($_parts[ $i ] != $_cache_id_parts[ $i ]) { |
||
| 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 | } |
If you suppress an error, we recommend checking for the error condition explicitly: