YetiForceCompany/YetiForceCRM

View on GitHub
modules/Settings/FieldsDependency/models/Module.php

Summary

Maintainability
A
1 hr
Test Coverage
F
23%

Function removeField has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
Open

    public static function removeField(string $moduleName, string $fieldName)
    {
        $tabId = \App\Module::getModuleId($moduleName);
        $dataReader = (new \App\Db\Query())->select(['id'])->from('s_#__fields_dependency')
            ->orWhere(['and', ['tabid' => $tabId], ['or', ['like', 'fields', "\"{$fieldName}\""], ['like', 'conditionsFields', "\"{$fieldName}\""]]])
Severity: Minor
Found in modules/Settings/FieldsDependency/models/Module.php - About 1 hr 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 '69', column '22').
Open

        $dataReader = (new \App\Db\Query())->select(['id'])->from('s_#__fields_dependency')

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\Module' in method 'removeField'.
Open

            $baseModuleName = \App\Module::getModuleName($fieldDepend->get('tabid'));

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\Condition' in method 'removeField'.
Open

            $conditions = \App\Condition::removeFieldFromCondition($baseModuleName, $conditions, $moduleName, $fieldName);

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 'removeField'.
Open

            $conditions = \App\Json::decode($fieldDepend->get('conditions'));

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\Module' in method 'removeField'.
Open

        $tabId = \App\Module::getModuleId($moduleName);

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 'Vtiger_Module_Model' in method 'getSupportedModules'.
Open

        return Vtiger_Module_Model::getAll([0, 2], [], true);

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 'removeField'.
Open

                $fields = \App\Json::decode($fieldDepend->get('fields'));

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_FieldsDependency_Record_Model' in method 'removeField'.
Open

            $fieldDepend = \Settings_FieldsDependency_Record_Model::getInstanceById($fieldDependId);

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 assigning values to variables in if clauses and the like (line '80', column '20').
Open

    public static function removeField(string $moduleName, string $fieldName)
    {
        $tabId = \App\Module::getModuleId($moduleName);
        $dataReader = (new \App\Db\Query())->select(['id'])->from('s_#__fields_dependency')
            ->orWhere(['and', ['tabid' => $tabId], ['or', ['like', 'fields', "\"{$fieldName}\""], ['like', 'conditionsFields', "\"{$fieldName}\""]]])

IfStatementAssignment

Since: 2.7.0

Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.

Example

class Foo
{
    public function bar($flag)
    {
        if ($foo = 'bar') { // possible typo
            // ...
        }
        if ($baz = 0) { // always false
            // ...
        }
    }
}

Source http://phpmd.org/rules/cleancode.html#ifstatementassignment

Define a constant instead of duplicating this literal "tabid" 3 times.
Open

        'tabid' => 'LBL_SOURCE_MODULE',

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

            ->orWhere(['like', 'conditions', $fieldName])->createCommand()->query();

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 "fields" 4 times.
Open

        'fields' => 'LBL_FIELDS',

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\Query::select
Open

        $dataReader = (new \App\Db\Query())->select(['id'])->from('s_#__fields_dependency')

Call to method getInstanceById from undeclared class \Settings_FieldsDependency_Record_Model
Open

            $fieldDepend = \Settings_FieldsDependency_Record_Model::getInstanceById($fieldDependId);

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

class Settings_FieldsDependency_Module_Model extends Settings_Vtiger_Module_Model

The class Settings_FieldsDependency_Module_Model is not named in CamelCase.
Open

class Settings_FieldsDependency_Module_Model extends Settings_Vtiger_Module_Model
{
    /** {@inheritdoc} */
    public $baseTable = 's_#__fields_dependency';

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

    /** {@inheritdoc} */

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 getDefaultUrl()

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

    {

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

            $fieldDepend->save();

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

    public static function getSupportedModules(): array

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

    public $baseIndex = 'id';

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

    /** {@inheritdoc} */

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

    }

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

            ->orWhere(['like', 'conditions', $fieldName])->createCommand()->query();

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

            $conditions = \App\Json::decode($fieldDepend->get('conditions'));

Line exceeds 120 characters; contains 122 characters
Open

            $conditions = \App\Condition::removeFieldFromCondition($baseModuleName, $conditions, $moduleName, $fieldName);

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

                    unset($fields[$key]);

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

        'name' => 'LBL_NAME',

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

    /** {@inheritdoc} */

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

    public $baseTable = 's_#__fields_dependency';

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

        'status' => 'FL_ACTIVE',

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

        $tabId = \App\Module::getModuleId($moduleName);

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

        'tabid' => 'LBL_SOURCE_MODULE',

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

     * Remove fields from dependent field entries.

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

                $fieldDepend->set('fields', $fields);

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 getCreateRecordUrl()

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

        $dataReader = (new \App\Db\Query())->select(['id'])->from('s_#__fields_dependency')

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

                }

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

                    continue;

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 string $moduleName

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

            $fieldDepend = \Settings_FieldsDependency_Record_Model::getInstanceById($fieldDependId);

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

                if (false !== ($key = array_search($fieldName, $fields))) {

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

                if (empty($fields)) {

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

     * Function to get Supported modules for fields dependency.

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 static function removeField(string $moduleName, string $fieldName)

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

        'views' => 'LBL_VIEWS',

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

                $fields = \App\Json::decode($fieldDepend->get('fields'));

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 Vtiger_Module_Model::getAll([0, 2], [], true);

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

            $baseModuleName = \App\Module::getModuleName($fieldDepend->get('tabid'));

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

            if ($baseModuleName === $moduleName) {

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

                    $fieldDepend->delete();

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

            }

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

    /** {@inheritdoc} */

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

    /** {@inheritdoc} */

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

            $fieldDepend->set('conditions', $conditions);

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

    public $listFields = [

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

    public $name = 'FieldsDependency';

Line exceeds 120 characters; contains 149 characters
Open

            ->orWhere(['and', ['tabid' => $tabId], ['or', ['like', 'fields', "\"{$fieldName}\""], ['like', 'conditionsFields', "\"{$fieldName}\""]]])

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

        while ($fieldDependId = $dataReader->readColumn(0)) {

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

    /** {@inheritdoc} */

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

        'mandatory' => 'LBL_MANDATORY',

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

        return 'index.php?parent=Settings&module=FieldsDependency&view=List';

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

        'fields' => 'LBL_FIELDS',

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

        return 'index.php?parent=Settings&module=FieldsDependency&view=Edit';

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

     * @param string $fieldName

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

            $conditions = \App\Condition::removeFieldFromCondition($baseModuleName, $conditions, $moduleName, $fieldName);

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

        'gui' => 'LBL_GUI',

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

     */

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

            ->orWhere(['and', ['tabid' => $tabId], ['or', ['like', 'fields', "\"{$fieldName}\""], ['like', 'conditionsFields', "\"{$fieldName}\""]]])

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

                }

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

class Settings_FieldsDependency_Module_Model extends Settings_Vtiger_Module_Model

There are no issues that match your filters.

Category
Status