Function getInstanceFromDb
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public static function getInstanceFromDb($row)
{
$map = [];
foreach ($row as $key => $value) {
if ('content' == $key) {
- 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
Missing class import via use statement (line '95', column '22'). Open
$dataReader = (new App\Db\Query())->from(self::$tableName)
- Read upRead up
- Exclude checks
MissingImport
Since: 2.7.0
Importing all external classes in a file through use statements makes them clearly visible.
Example
function make() {
return new \stdClass();
}
Source http://phpmd.org/rules/cleancode.html#MissingImport
The method getInstanceFromDb uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$map[$key] = $value;
}
- Read upRead up
- Exclude checks
ElseExpression
Since: 1.4.0
An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.
Example
class Foo
{
public function bar($flag)
{
if ($flag) {
// one branch
} else {
// another branch
}
}
}
Source https://phpmd.org/rules/cleancode.html#elseexpression
Define a constant instead of duplicating this literal "module" 3 times. Open
$dbCommand->update(self::$tableName, ['content' => $this->getStringifiedContent()], ['name' => $this->getValue('name'), 'module' => $this->getValue('module')])->execute();
- Read upRead up
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() { prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times execute('action1'); release('action1'); }
Compliant Solution
ACTION_1 = 'action1'; function run() { prepare(ACTION_1); execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Define a constant instead of duplicating this literal "content" 5 times. Open
if ('content' == $key) {
- Read upRead up
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() { prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times execute('action1'); release('action1'); }
Compliant Solution
ACTION_1 = 'action1'; function run() { prepare(ACTION_1); execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Call to undeclared method \App\Db::createCommand
Open
\App\Db::getInstance()
- Exclude checks
Call to undeclared method \App\Db::createCommand
Open
$dbCommand = App\Db::getInstance()->createCommand();
- Exclude checks
Call to undeclared method \App\Db\Query::from
Open
$dataReader = (new App\Db\Query())->from(self::$tableName)
- Exclude checks
Each class must be in a namespace of at least one level (a top-level vendor name) Open
class Import_Map_Model extends \App\Base
- Exclude checks
The class Import_Map_Model is not named in CamelCase. Open
class Import_Map_Model extends \App\Base
{
public static $tableName = 'vtiger_import_maps';
public $map;
- Read upRead up
- Exclude checks
CamelCaseClassName
Since: 0.2
It is considered best practice to use the CamelCase notation to name classes.
Example
class class_name {
}
Source
Spaces must be used to indent lines; tabs are not allowed Open
$map[$key] = $value;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->update('vtiger_import_maps', ['date_modified' => date('Y-m-d H:i:s'), 'deleted' => 1], ['id' => $mapId])
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$key = str_replace('&', '/amp/', $key);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$importMap = self::getInstanceFromDb($row);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($pairs as $pair) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$mappedName = str_replace('/eq/', '=', $mappedName);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $this->map[$key];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$keyValueStrings = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\count($values) > 0) {
- Exclude checks
Line exceeds 120 characters; contains 183 characters Open
$dbCommand->update(self::$tableName, ['content' => $this->getStringifiedContent()], ['name' => $this->getValue('name'), 'module' => $this->getValue('module')])->execute();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function getAllValues()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $savedMaps;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return new self($map);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->createCommand()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->createCommand()->query();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$dataReader = (new App\Db\Query())->from(self::$tableName)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
while ($row = $dataReader->read()) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$content = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$mappedName = str_replace('/amp/', '&', $mappedName);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$map[$key] = $content;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getAllByModule($moduleName)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$map = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$savedMaps = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public $map;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($row as $key => $value) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ('content' == $key) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$key = str_replace('=', '/eq/', $key);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$keyValueStrings[] = $key . '=' . $value;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$dbCommand->insert(self::$tableName, $values)->execute();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static $tableName = 'vtiger_import_maps';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$pairs = $value ? explode('&', $value) : [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->execute();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (empty($this->map['content'])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$content = $this->map['content'];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$values['content'] = null;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$values['date_entered'] = date('Y-m-d H:i:s');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$dbCommand = App\Db::getInstance()->createCommand();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$savedMaps[$importMap->getId()] = $importMap;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getInstanceFromDb($row)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $this->map['id'];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function getStringifiedContent()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($content as $key => $value) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->map = $map;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$content["$mappedName"] = $sequence;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} else {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function markAsDeleted($mapId)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
\App\Db::getInstance()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function getId()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function getValue($key)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
[$mappedName, $sequence] = explode('=', $pair);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function save()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function __construct($map)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return implode('&', $keyValueStrings);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$dataReader->close();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $this->map;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$values = $this->getAllValues();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$dbCommand->update(self::$tableName, ['content' => $this->getStringifiedContent()], ['name' => $this->getValue('name'), 'module' => $this->getValue('module')])->execute();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->where(['deleted' => 0, 'module' => $moduleName])
- Exclude checks
Class name "Import_Map_Model" is not in camel caps format Open
class Import_Map_Model extends \App\Base
- Exclude checks