YetiForceCompany/YetiForceCRM

View on GitHub
modules/Settings/AdminAccess/models/Record.php

Summary

Maintainability
A
45 mins
Test Coverage
F
0%

Function save has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
Open

    public function save()
    {
        $db = \App\Db::getInstance('admin');
        $transaction = $db->beginTransaction();
        try {
Severity: Minor
Found in modules/Settings/AdminAccess/models/Record.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

Missing class import via use statement (line '63', column '16').
Open

        $data = (new App\Db\Query())

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 '68', column '25').
Open

            $data['user'] = (new \App\Db\Query())->from(\App\Security\AdminAccess::ACCESS_TABLE_NAME)->select(['user'])->where(['module_id' => $data['id']])->column();

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\Db' in method 'getInstance'.
Open

            ->one(App\Db::getInstance('admin'));

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\Cache' in method 'clearCache'.
Open

        \App\Cache::delete('AdminActiveModules', '');

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\Db' in method 'save'.
Open

        $db = \App\Db::getInstance('admin');

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\Cache' in method 'clearCache'.
Open

            \App\Cache::delete('AdminPermittedModulesByUser', $userId);

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 'Settings_Vtiger_Module_Model' in method 'getModule'.
Open

            $this->module = Settings_Vtiger_Module_Model::getInstance('Settings:AdminAccess');

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 save uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
Open

                } else {
                    $dbCommand->update($tableName, $tableData, ['id' => $this->getId()])->execute();
                }

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_id" 3 times.
Open

            $data['user'] = (new \App\Db\Query())->from(\App\Security\AdminAccess::ACCESS_TABLE_NAME)->select(['user'])->where(['module_id' => $data['id']])->column();

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 \Settings_Vtiger_Module_Model::getFieldsForSave
Open

        $saveFields = array_intersect($moduleModel->getFieldsForSave(), array_keys($this->changes));

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

            $dbCommand = $db->createCommand();

Reference to undeclared property \Settings_AdminAccess_Record_Model->module
Open

        return $this->module;

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

            $data['user'] = (new \App\Db\Query())->from(\App\Security\AdminAccess::ACCESS_TABLE_NAME)->select(['user'])->where(['module_id' => $data['id']])->column();

Call to undeclared method \Settings_Vtiger_Module_Model::getFieldInstanceByName
Open

            $fieldModel = $moduleModel->getFieldInstanceByName($fieldName);

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

        $data = (new App\Db\Query())

Reference to undeclared property \Settings_AdminAccess_Record_Model->module
Open

            $this->module = Settings_Vtiger_Module_Model::getInstance('Settings:AdminAccess');

Reference to undeclared property \Settings_AdminAccess_Record_Model->module
Open

        $this->module = $moduleModel;

Reference to undeclared property \Settings_AdminAccess_Record_Model->module
Open

        if (!isset($this->module)) {

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

        $transaction = $db->beginTransaction();

Each class must be in a namespace of at least one level (a top-level vendor name)
Open

class Settings_AdminAccess_Record_Model extends Settings_Vtiger_Record_Model

Avoid variables with short names like $db. Configured minimum length is 3.
Open

        $db = \App\Db::getInstance('admin');

ShortVariable

Since: 0.2

Detects when a field, local, or parameter has a very short name.

Example

class Something {
    private $q = 15; // VIOLATION - Field
    public static function main( array $as ) { // VIOLATION - Formal
        $r = 20 + $this->q; // VIOLATION - Local
        for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
            $r += $this->q;
        }
    }
}

Source https://phpmd.org/rules/naming.html#shortvariable

The class Settings_AdminAccess_Record_Model is not named in CamelCase.
Open

class Settings_AdminAccess_Record_Model extends Settings_Vtiger_Record_Model
{
    /**
     * Changes value.
     *

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

    /**

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

        $where = [$moduleModel->getBaseIndex() => $module];

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

     * Record ID.

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

     * @return self

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

            $moduleModel = $instance->getModule();

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

     * @var array

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

     */

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

    {

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

        $instance = new self();

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

    /**

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

    public function getId()

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

        return $this->get('id');

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

     * Record name.

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

     *

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

     */

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

    {

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

        return $this->get('name');

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

     *

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

    /**

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

     * @return int

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

        if (null === $moduleModel) {

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

        }

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

     *

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

    }

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

     *

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

    public function getName()

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

    public static function getInstance($module, $moduleModel = null)

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

        $data = (new App\Db\Query())

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

            ->one(App\Db::getInstance('admin'));

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

    {

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

                        $insertData[] = [$this->getId(), $value];

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

        return $forSave;

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

    }

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

     */

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

    }

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

     * @param Settings_Vtiger_Module_Model|null $moduleModel

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

        if (!is_numeric($module)) {

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

    public function save()

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

        $transaction = $db->beginTransaction();

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

                    $dbCommand->batchInsert($tableName, ['module_id', 'user'], $insertData)->execute();

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

            $transaction->commit();

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

     * @return array

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

    {

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

        foreach ($saveFields as $fieldName) {

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

     *

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

     *

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

        }

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

        \App\Cache::delete('AdminActiveModules', '');

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

     * Changes value.

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

    private $changes = [];

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

     */

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

    {

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

    /**

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

     * @return void

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

    /**

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

     * Gets instance.

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

        $instance->setData($data);

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

    /**

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

            }

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

     * Prepare value to save.

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

        $forSave = [];

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

     */

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

    public function clearCache(): void

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

     */

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

     * @return string

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

            ->where($where)

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

                    $insertData = [];

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

                }

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

     *

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

        $moduleModel = $this->getModule();

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

        if ($key !== $this->getModule()->getBaseIndex() && ($this->value[$key] ?? null) !== $value) {

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

            $this->changes[$key] = $this->get($key);

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

    /**

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

     * @return Settings_Vtiger_Module_Model

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

    {

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

    }

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

        if ($data) {

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

            $dbCommand = $db->createCommand();

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

    public function getValuesForSave()

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

        }

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

        $users = array_merge($this->get('user'), $previous);

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

        $instance->setModule($moduleModel);

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

     * @param Settings_Vtiger_Module_Model $moduleModel

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

     */

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

            foreach ($this->getValuesForSave() as $tableName => $tableData) {

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

                    $dbCommand->delete($tableName, ['module_id' => $this->getId()])->execute();

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

            $fieldModel = $moduleModel->getFieldInstanceByName($fieldName);

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

    /**

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

    {

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

        }

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

     * @param int|string                        $module

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

        $this->module = $moduleModel;

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

                if (\App\Security\AdminAccess::ACCESS_TABLE_NAME === $tableName) {

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

            $transaction->rollBack();

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

        $saveFields = array_intersect($moduleModel->getFieldsForSave(), array_keys($this->changes));

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

    }

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

        $this->value[$key] = $value;

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

     * Function to get Module instance.

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

     *

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

            $where = ['name' => $module];

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

     *

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

            $value = $this->get($fieldName);

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

    /**

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

    }

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

            throw $ex;

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

        return $this;

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

        }

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

    {

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

        return $instance;

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

    }

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

     * Function to set module instance to this record instance.

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

        }

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

     * @return $this

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

        }

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

    {

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

     * Function to set the value for a given key.

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

     * @param mixed  $value

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

        $db = \App\Db::getInstance('admin');

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

    }

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

                    foreach ($tableData['user'] as $value) {

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

                    }

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

                    $dbCommand->update($tableName, $tableData, ['id' => $this->getId()])->execute();

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

            $forSave[$fieldModel->getTableName()][$fieldModel->getColumnName()] = $value;

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

    public function getModule()

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

            $this->module = Settings_Vtiger_Module_Model::getInstance('Settings:AdminAccess');

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

        }

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

     */

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

            $this->clearCache();

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

     * @param string $key

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

    public function set($key, $value)

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

     * Clear cache.

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

            ->from($moduleModel->getBaseTable())

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

    public function setModule($moduleModel)

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

        } catch (\Throwable $ex) {

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

     */

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

     */

Line exceeds 120 characters; contains 167 characters
Open

            $data['user'] = (new \App\Db\Query())->from(\App\Security\AdminAccess::ACCESS_TABLE_NAME)->select(['user'])->where(['module_id' => $data['id']])->column();

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

        try {

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

                } else {

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

    /**

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

        if (!isset($this->module)) {

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

        return $this->module;

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

        $previous = $this->changes['user'] ?? [];

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

            \App\Cache::delete('AdminPermittedModulesByUser', $userId);

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

            $data['user'] = (new \App\Db\Query())->from(\App\Security\AdminAccess::ACCESS_TABLE_NAME)->select(['user'])->where(['module_id' => $data['id']])->column();

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

     *

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

        return $this;

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

     * Function to save.

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

    }

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

     *

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

        foreach (array_unique($users) as $userId) {

Class name "Settings_AdminAccess_Record_Model" is not in camel caps format
Open

class Settings_AdminAccess_Record_Model extends Settings_Vtiger_Record_Model

There are no issues that match your filters.

Category
Status