Function saveInventoryColumnsForRecords
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public static function saveInventoryColumnsForRecords(string $moduleName, array $records)
{
$dbCommand = \App\Db::getInstance()->createCommand();
$availableColumns = array_keys(\Vtiger_Inventory_Model::getInstance($moduleName)->getFields());
foreach ($records as $columns) {
- 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
Avoid using undefined variables such as '$updateData' which will lead to PHP notices. Open
unset($insertData, $availableColumns, $updateData, $row, $table, $schemeExists);
- Read upRead up
- Exclude checks
UndefinedVariable
Since: 2.8.0
Detects when a variable is used that has not been defined before.
Example
class Foo
{
private function bar()
{
// $message is undefined
echo $message;
}
}
Source https://phpmd.org/rules/cleancode.html#undefinedvariable
Avoid using undefined variables such as '$row' which will lead to PHP notices. Open
unset($insertData, $availableColumns, $updateData, $row, $table, $schemeExists);
- Read upRead up
- Exclude checks
UndefinedVariable
Since: 2.8.0
Detects when a variable is used that has not been defined before.
Example
class Foo
{
private function bar()
{
// $message is undefined
echo $message;
}
}
Source https://phpmd.org/rules/cleancode.html#undefinedvariable
Missing class import via use statement (line '53', column '15'). Open
throw new \App\Exceptions\IllegalValue('ERR_NOT_ALLOWED_VALUE', 406);
- 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
Missing class import via use statement (line '29', column '24'). Open
$columnsJSON = (new \App\Db\Query())
- 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
Missing class import via use statement (line '60', column '25'). Open
$schemeExists = (new \App\Db\Query())
- 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
Avoid using static access to class '\App\Json' in method 'saveInventoryColumnsForRecords'. Open
$json = \App\Json::encode($columns);
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class '\App\Json' in method 'getInventoryColumnsForRecord'. Open
$columns = $columnsJSON ? \App\Json::decode($columnsJSON) : $columnsAll;
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
The method saveInventoryColumnsForRecords uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$insertData[] = [$recordId, $json];
}
- 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 "columns" 3 times. Open
->select(['columns'])
- 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 "crmid" 4 times. Open
->where(['crmid' => $recordId])
- 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.
Avoid unused local variables such as '$updateData'. Open
unset($insertData, $availableColumns, $updateData, $row, $table, $schemeExists);
- Read upRead up
- Exclude checks
UnusedLocalVariable
Since: 0.2
Detects when a local variable is declared and/or assigned, but not used.
Example
class Foo {
public function doSomething()
{
$i = 5; // Unused
}
}
Source https://phpmd.org/rules/unusedcode.html#unusedlocalvariable
Avoid unused local variables such as '$row'. Open
unset($insertData, $availableColumns, $updateData, $row, $table, $schemeExists);
- Read upRead up
- Exclude checks
UnusedLocalVariable
Since: 0.2
Detects when a local variable is declared and/or assigned, but not used.
Example
class Foo {
public function doSomething()
{
$i = 5; // Unused
}
}
Source https://phpmd.org/rules/unusedcode.html#unusedlocalvariable
Variable $row
is undeclared Open
unset($insertData, $availableColumns, $updateData, $row, $table, $schemeExists);
- Exclude checks
Variable $updateData
is undeclared Open
unset($insertData, $availableColumns, $updateData, $row, $table, $schemeExists);
- Exclude checks
Call to undeclared method \App\Db\Query::from
Open
$schemeExists = (new \App\Db\Query())
- Exclude checks
Call to undeclared method \App\Db::createCommand
Open
$dbCommand = \App\Db::getInstance()->createCommand();
- Exclude checks
Call to undeclared method \App\Db\Query::select
Open
$columnsJSON = (new \App\Db\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
* @return array
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->select(['columns'])
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->where(['crmid' => $recordId])
- 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
->where(['crmid' => $recordId])
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->exists();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$columns = $columnsJSON ? \App\Json::decode($columnsJSON) : $columnsAll;
- 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
$columnsAll = array_keys(\Vtiger_Inventory_Model::getInstance($moduleName)->getFields());
- 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
$table = 'u_#__pdf_inv_scheme';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($records as $recordId => $columns) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$insertData[] = [$recordId, $json];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (array_diff($columns, $availableColumns)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$json = \App\Json::encode($columns);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param array $records
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (null === $columns) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!empty($insertData)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getInventoryColumnsForRecord(int $recordId, string $moduleName, ?array $columns = null)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->scalar();
- 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
public static function saveInventoryColumnsForRecords(string $moduleName, array $records)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$availableColumns = array_keys(\Vtiger_Inventory_Model::getInstance($moduleName)->getFields());
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
throw new \App\Exceptions\IllegalValue('ERR_NOT_ALLOWED_VALUE', 406);
- 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
$insertData = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($schemeExists) {
- 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 array_intersect($columns, $columnsAll);
- 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
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->from('u_#__pdf_inv_scheme')
- 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
* Save column scheme for specified record.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->from($table)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$dbCommand->batchInsert($table, ['crmid', 'columns'], $insertData)->execute();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$schemeExists = (new \App\Db\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
* @param string $moduleName
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($records as $columns) {
- 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
* Get column scheme for specified record.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param int $recordId
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param string $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
* @param array|null $columns
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$columnsJSON = (new \App\Db\Query())
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$dbCommand->update($table, ['columns' => $json], ['crmid' => $recordId])->execute();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
unset($insertData, $availableColumns, $updateData, $row, $table, $schemeExists);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @throws \App\Exceptions\IllegalValue
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks