YetiForceCompany/YetiForceCRM

View on GitHub
app/Pdf/InventoryColumns.php

Summary

Maintainability
A
45 mins
Test Coverage
F
0%

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) {
Severity: Minor
Found in app/Pdf/InventoryColumns.php - About 45 mins to fix

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);
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpmd

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);
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpmd

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);
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpmd

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())
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpmd

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())
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpmd

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);
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpmd

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;
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpmd

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];
            }
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpmd

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'])
Severity: Critical
Found in app/Pdf/InventoryColumns.php by sonar-php

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])
Severity: Critical
Found in app/Pdf/InventoryColumns.php by sonar-php

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);
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpmd

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);
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpmd

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);
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phan

Variable $updateData is undeclared
Open

        unset($insertData, $availableColumns, $updateData, $row, $table, $schemeExists);
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phan

Call to undeclared method \App\Db\Query::from
Open

            $schemeExists = (new \App\Db\Query())
Severity: Critical
Found in app/Pdf/InventoryColumns.php by phan

Call to undeclared method \App\Db::createCommand
Open

        $dbCommand = \App\Db::getInstance()->createCommand();
Severity: Critical
Found in app/Pdf/InventoryColumns.php by phan

Call to undeclared method \App\Db\Query::select
Open

            $columnsJSON = (new \App\Db\Query())
Severity: Critical
Found in app/Pdf/InventoryColumns.php by phan

Spaces must be used to indent lines; tabs are not allowed
Open

     *
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     *
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     * @return array
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

                ->select(['columns'])
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

                ->where(['crmid' => $recordId])
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        $dbCommand = \App\Db::getInstance()->createCommand();
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

                ->where(['crmid' => $recordId])
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

                ->exists();
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

            $columns = $columnsJSON ? \App\Json::decode($columnsJSON) : $columnsAll;
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     */
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        $columnsAll = array_keys(\Vtiger_Inventory_Model::getInstance($moduleName)->getFields());
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     */
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

    {
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        $table = 'u_#__pdf_inv_scheme';
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        foreach ($records as $recordId => $columns) {
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

                $insertData[] = [$recordId, $json];
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

            if (array_diff($columns, $availableColumns)) {
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

            $json = \App\Json::encode($columns);
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     * @param array  $records
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        if (null === $columns) {
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        if (!empty($insertData)) {
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

    public static function getInventoryColumnsForRecord(int $recordId, string $moduleName, ?array $columns = null)
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

                ->scalar();
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        }
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

    /**
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     *
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

    public static function saveInventoryColumnsForRecords(string $moduleName, array $records)
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        $availableColumns = array_keys(\Vtiger_Inventory_Model::getInstance($moduleName)->getFields());
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

                throw new \App\Exceptions\IllegalValue('ERR_NOT_ALLOWED_VALUE', 406);
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        }
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        }
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        $insertData = [];
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

            if ($schemeExists) {
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

    {
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        return array_intersect($columns, $columnsAll);
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

            } else {
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

            }
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

                ->from('u_#__pdf_inv_scheme')
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

    }
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     * Save column scheme for specified record.
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

                ->from($table)
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

            $dbCommand->batchInsert($table, ['crmid', 'columns'], $insertData)->execute();
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

            $schemeExists = (new \App\Db\Query())
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

    }
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

    /**
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     * @param string     $moduleName
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        foreach ($records as $columns) {
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

            }
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     * Get column scheme for specified record.
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     * @param int        $recordId
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     * @param string $moduleName
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     *
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     * @param array|null $columns
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

            $columnsJSON = (new \App\Db\Query())
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

                $dbCommand->update($table, ['columns' => $json], ['crmid' => $recordId])->execute();
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        unset($insertData, $availableColumns, $updateData, $row, $table, $schemeExists);
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

     * @throws \App\Exceptions\IllegalValue
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

Spaces must be used to indent lines; tabs are not allowed
Open

        }
Severity: Minor
Found in app/Pdf/InventoryColumns.php by phpcodesniffer

There are no issues that match your filters.

Category
Status