File xpdocachemanager.class.php
has 713 lines of code (exceeds 250 allowed). Consider refactoring. Open
<?php
/*
* Copyright 2010-2015 by MODX, LLC.
*
* This file is part of xPDO.
Function deleteTree
has a Cognitive Complexity of 79 (exceeds 5 allowed). Consider refactoring. Open
public function deleteTree($dirname, $options= array('deleteTop' => false, 'skipDirs' => false, 'extensions' => array('.cache.php'))) {
$result= false;
if (is_dir($dirname)) { /* Operate on dirs only */
if (substr($dirname, -1) != '/') {
$dirname .= '/';
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function copyTree
has a Cognitive Complexity of 70 (exceeds 5 allowed). Consider refactoring. Open
public function copyTree($source, $target, $options= array()) {
$copied= false;
$source= strtr($source, '\\', '/');
$target= strtr($target, '\\', '/');
if ($source[strlen($source) - 1] == '/') $source = substr($source, 0, strlen($source) - 1);
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getCachePath
has a Cognitive Complexity of 62 (exceeds 5 allowed). Consider refactoring. Open
public function getCachePath() {
$cachePath= false;
if (empty($this->xpdo->cachePath)) {
if (!isset ($this->xpdo->config['cache_path'])) {
while (true) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function get
has a Cognitive Complexity of 54 (exceeds 5 allowed). Consider refactoring. Open
public function get($key, $options= array()) {
$value= null;
$cacheKey= $this->getCacheKey($key, $options);
if (file_exists($cacheKey)) {
if ($file = @fopen($cacheKey, 'rb')) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function writeFile
has a Cognitive Complexity of 39 (exceeds 5 allowed). Consider refactoring. Open
public function writeFile($filename, $content, $mode= 'wb', $options= array()) {
$written= false;
if (!is_array($options)) {
$options = is_scalar($options) && !is_bool($options) ? array('new_folder_permissions' => $options) : array();
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function copyFile
has a Cognitive Complexity of 27 (exceeds 5 allowed). Consider refactoring. Open
public function copyFile($source, $target, $options = array()) {
$copied= false;
if (!is_array($options)) $options = is_scalar($options) && !is_bool($options) ? array('new_file_permissions' => $options) : array();
if (func_num_args() === 4) $options['new_folder_permissions'] = func_get_arg(3);
if ($this->writeTree(dirname($target), $options)) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function generateObject
has a Cognitive Complexity of 22 (exceeds 5 allowed). Consider refactoring. Open
public function generateObject($obj, $objName, $generateObjVars= false, $generateRelated= false, $objRef= 'this->xpdo', $format= xPDOCacheManager::CACHE_PHP) {
$source= false;
if (is_object($obj) && $obj instanceof xPDOObject) {
$className= $obj->_class;
$source= "\${$objName}= \${$objRef}->newObject('{$className}');\n";
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
xPDOCacheManager
has 25 functions (exceeds 20 allowed). Consider refactoring. Open
class xPDOCacheManager {
const CACHE_PHP = 0;
const CACHE_JSON = 1;
const CACHE_SERIALIZE = 2;
const CACHE_DIR = 'objects/';
Method copyTree
has 61 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function copyTree($source, $target, $options= array()) {
$copied= false;
$source= strtr($source, '\\', '/');
$target= strtr($target, '\\', '/');
if ($source[strlen($source) - 1] == '/') $source = substr($source, 0, strlen($source) - 1);
Method deleteTree
has 58 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function deleteTree($dirname, $options= array('deleteTop' => false, 'skipDirs' => false, 'extensions' => array('.cache.php'))) {
$result= false;
if (is_dir($dirname)) { /* Operate on dirs only */
if (substr($dirname, -1) != '/') {
$dirname .= '/';
Method writeFile
has 57 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function writeFile($filename, $content, $mode= 'wb', $options= array()) {
$written= false;
if (!is_array($options)) {
$options = is_scalar($options) && !is_bool($options) ? array('new_folder_permissions' => $options) : array();
}
Function endsWith
has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring. Open
public function endsWith($string, $pattern) {
$matched= false;
if (is_string($string) && ($stringLen= strlen($string))) {
if (is_array($pattern)) {
foreach ($pattern as $subPattern) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function writeTree
has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring. Open
public function writeTree($dirname, $options= array()) {
$written= false;
if (!empty ($dirname)) {
if (!is_array($options)) $options = is_scalar($options) && !is_bool($options) ? array('new_folder_permissions' => $options) : array();
$mode = $this->getOption('new_folder_permissions', $options, $this->getFolderPermissions());
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method get
has 48 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function get($key, $options= array()) {
$value= null;
$cacheKey= $this->getCacheKey($key, $options);
if (file_exists($cacheKey)) {
if ($file = @fopen($cacheKey, 'rb')) {
Method getCachePath
has 46 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function getCachePath() {
$cachePath= false;
if (empty($this->xpdo->cachePath)) {
if (!isset ($this->xpdo->config['cache_path'])) {
while (true) {
Function lockFile
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
public function lockFile($file, array $options = array()) {
$locked = false;
$lockDir = $this->getOption('lock_dir', $options, $this->getCachePath() . 'locks' . DIRECTORY_SEPARATOR);
if ($this->writeTree($lockDir, $options)) {
$lockFile = $this->lockFileName($file, $options);
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function set
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
public function set($key, $var, $expire= 0, $options= array()) {
$set= false;
if ($var !== null) {
if ($expire === true)
$expire= 0;
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getCacheProvider
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
public function & getCacheProvider($key = '', $options = array()) {
$objCache = null;
if (empty($key)) {
$key = $this->getOption(xPDO::OPT_CACHE_KEY, $options, 'default');
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function matches
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
public function matches($string, $pattern) {
$matched= false;
if (is_string($string) && ($stringLen= strlen($string))) {
if (is_array($pattern)) {
foreach ($pattern as $subPattern) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getOption
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
public function getOption($key, $options = array(), $default = null) {
$option = $default;
if (is_array($key)) {
if (!is_array($option)) {
$default= $option;
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getOption
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
public function getOption($key, $options = array(), $default = null) {
$option = $default;
if (is_array($key)) {
if (!is_array($option)) {
$default= $option;
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method copyFile
has 30 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function copyFile($source, $target, $options = array()) {
$copied= false;
if (!is_array($options)) $options = is_scalar($options) && !is_bool($options) ? array('new_file_permissions' => $options) : array();
if (func_num_args() === 4) $options['new_folder_permissions'] = func_get_arg(3);
if ($this->writeTree(dirname($target), $options)) {
Method set
has 30 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function set($key, $var, $expire= 0, $options= array()) {
$set= false;
if ($var !== null) {
if ($expire === true)
$expire= 0;
Avoid deeply nested control flow statements. Open
if (!filesize($cacheKey)) {
$value= false;
break;
}
Avoid deeply nested control flow statements. Open
if ($payload !== false) {
$payload = $this->xpdo->fromJSON($payload);
if (is_array($payload) && isset($payload['expires']) && (empty($payload['expires']) || time() < $payload['expires'])) {
if (array_key_exists('content', $payload)) {
$value= $payload['content'];
Avoid deeply nested control flow statements. Open
if (is_dir($path)) {
$suboptions = array_merge($options, array('deleteTop' => !$this->getOption('skipDirs', $options, false)));
if ($subresult= $this->deleteTree($path, $suboptions)) {
$result= array_merge($result, $subresult);
}
Method generateObject
has 6 arguments (exceeds 4 allowed). Consider refactoring. Open
public function generateObject($obj, $objName, $generateObjVars= false, $generateRelated= false, $objRef= 'this->xpdo', $format= xPDOCacheManager::CACHE_PHP) {
Avoid deeply nested control flow statements. Open
if ($payload !== false) {
$payload = unserialize($payload);
if (is_array($payload) && isset($payload['expires']) && (empty($payload['expires']) || time() < $payload['expires'])) {
if (array_key_exists('content', $payload)) {
$value= $payload['content'];
Function delete
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public function delete($key, $options= array()) {
$deleted= false;
if ($this->getOption('multiple_object_delete', $options, true)) {
$cacheKey= $this->getCacheKey($key, array_merge($options, array('cache_ext' => '')));
if (file_exists($cacheKey) && is_dir($cacheKey)) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function refresh
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public function refresh(array $providers = array(), array &$results = array()) {
if (empty($providers)) {
foreach ($this->caches as $cacheKey => $cache) {
$providers[$cacheKey] = array();
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function set
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public function set($key, & $var, $lifetime= 0, $options= array()) {
$return= false;
if ($cache = $this->getCacheProvider($this->getOption(xPDO::OPT_CACHE_KEY, $options), $options)) {
$value= null;
if (is_object($var) && $var instanceof xPDOObject) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Similar blocks of code found in 2 locations. Consider refactoring. Open
public function getOption($key, $options = array(), $default = null) {
$option = $default;
if (is_array($key)) {
if (!is_array($option)) {
$default= $option;
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 224.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76