YetiForceCompany/YetiForceCRM

View on GitHub
modules/Settings/LayoutEditor/models/Block.php

Summary

Maintainability
A
35 mins
Test Coverage
F
28%

Function isAddCustomFieldEnabled has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
Open

    public function isAddCustomFieldEnabled()
    {
        $actionNotSupportedModules = ['calendar', 'events', 'faq', 'helpdesk'];
        $blocksEliminatedArray = ['calendar' => ['LBL_TASK_INFORMATION', 'LBL_DESCRIPTION_INFORMATION', 'LBL_RELATED_TO', 'LBL_REMINDER_INFORMATION', 'LBL_RECURRENCE_INFORMATION'],
            'helpdesk' => ['LBL_TICKET_RESOLUTION', 'LBL_COMMENTS'],
Severity: Minor
Found in modules/Settings/LayoutEditor/models/Block.php - About 35 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 '59', column '22').
Open

            'sequence' => new yii\db\Expression($caseSequence),

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 getInstance has a boolean flag argument $moduleInstance, which is a certain sign of a Single Responsibility Principle violation.
Open

    public static function getInstance($value, $moduleInstance = false)

BooleanArgumentFlag

Since: 1.4.0

A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.

Example

class Foo {
    public function bar($flag = true) {
    }
}

Source https://phpmd.org/rules/cleancode.html#booleanargumentflag

Missing class import via use statement (line '60', column '19').
Open

            'block' => new yii\db\Expression($caseBlock),

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

            } else {
                return false;
            }

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

Avoid using static access to class 'App\Db' in method 'updateFieldSequenceNumber'.
Open

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

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

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

            $fieldId = $newFieldSequence['fieldid'];

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 method __construct from undeclared class \yii\db\Expression
Open

            'block' => new yii\db\Expression($caseBlock),

Call to method __construct from undeclared class \yii\db\Expression
Open

            'sequence' => new yii\db\Expression($caseSequence),

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

            $caseSequence .= " WHEN fieldid = {$db->quoteValue($fieldId)} THEN {$db->quoteValue($newFieldSequence['sequence'])}";

Saw unextractable annotation for comment '* @param <type> $moduleModel - module instance'</type>
Open

     * @param <type> $moduleModel - module instance

Saw unextractable annotation for comment '* @return <array> - list of Vtiger_Block_Model'</array>
Open

     * @return <array> - list of Vtiger_Block_Model

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

        $db->createCommand()->update('vtiger_field', [

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

            $caseBlock .= " WHEN fieldid = {$db->quoteValue($newFieldSequence['fieldid'])} THEN {$db->quoteValue($newFieldSequence['block'])}";

Avoid excessively long variable names like $actionNotSupportedModules. Keep variable name length under 20.
Open

        $actionNotSupportedModules = ['calendar', 'events', 'faq', 'helpdesk'];

LongVariable

Since: 0.2

Detects when a field, formal or local variable is declared with a long name.

Example

class Something {
    protected $reallyLongIntName = -3; // VIOLATION - Field
    public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
        $otherReallyLongName = -5; // VIOLATION - Local
        for ($interestingIntIndex = 0; // VIOLATION - For
             $interestingIntIndex < 10;
             $interestingIntIndex++ ) {
        }
    }
}

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

Avoid excessively long variable names like $blocksEliminatedArray. Keep variable name length under 20.
Open

        $blocksEliminatedArray = ['calendar' => ['LBL_TASK_INFORMATION', 'LBL_DESCRIPTION_INFORMATION', 'LBL_RELATED_TO', 'LBL_REMINDER_INFORMATION', 'LBL_RECURRENCE_INFORMATION'],

LongVariable

Since: 0.2

Detects when a field, formal or local variable is declared with a long name.

Example

class Something {
    protected $reallyLongIntName = -3; // VIOLATION - Field
    public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
        $otherReallyLongName = -5; // VIOLATION - Local
        for ($interestingIntIndex = 0; // VIOLATION - For
             $interestingIntIndex < 10;
             $interestingIntIndex++ ) {
        }
    }
}

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

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

class Settings_LayoutEditor_Block_Model extends Vtiger_Block_Model

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

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

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_LayoutEditor_Block_Model is not named in CamelCase.
Open

class Settings_LayoutEditor_Block_Model extends Vtiger_Block_Model
{
    /**
     * Function to check whether adding custom field is allowed or not.
     *

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

            'faq' => ['LBL_COMMENT_INFORMATION']];

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

    /**

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

            'block' => new yii\db\Expression($caseBlock),

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 = $this->getFields();

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

            }

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

            'helpdesk' => ['LBL_TICKET_RESOLUTION', 'LBL_COMMENTS'],

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

     * @param array $blockFieldSequence

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

        }

Line exceeds 120 characters; contains 129 characters
Open

            $caseSequence .= " WHEN fieldid = {$db->quoteValue($fieldId)} THEN {$db->quoteValue($newFieldSequence['sequence'])}";

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

     * Function to retrieve block instances for a 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

    public static function getInstance($value, $moduleInstance = false)

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

     * @return Vtiger_Block_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

        return 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

     * Function to save sequence number of fields.

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

            $fieldId = $newFieldSequence['fieldid'];

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

        foreach ($blockFieldSequence as $newFieldSequence) {

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 <array> - list of Vtiger_Block_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

        $blocksEliminatedArray = ['calendar' => ['LBL_TASK_INFORMATION', 'LBL_DESCRIPTION_INFORMATION', 'LBL_RELATED_TO', 'LBL_REMINDER_INFORMATION', 'LBL_RECURRENCE_INFORMATION'],

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

        if (\in_array(strtolower($this->module->name), $actionNotSupportedModules)) {

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

     */

Line exceeds 120 characters; contains 143 characters
Open

            $caseBlock .= " WHEN fieldid = {$db->quoteValue($newFieldSequence['fieldid'])} THEN {$db->quoteValue($newFieldSequence['block'])}";

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 ($objectProperties as $properName => $propertyValue) {

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

    {

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

        $activeFields = [];

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

    public function isAddCustomFieldEnabled()

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

        $fieldIdList = [];

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

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

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

        }

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

        $blockModel = 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

        foreach ($fields as $fieldName => $fieldModel) {

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 $activeFields;

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 <type> $moduleModel - module instance

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

    public function getLayoutBlockActiveFields()

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

                if (\in_array($this->get('label'), $blocksEliminatedArray[strtolower($this->module->name)])) {

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

    public static function updateFieldSequenceNumber($blockFieldSequence)

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

        $caseSequence = 'CASE';

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

        foreach ($blockFieldSequence as $newFieldSequence) {

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

        }

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

        $blockModelList = [];

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

     * Function to check whether adding custom field is allowed or not.

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

            if (!empty($blocksEliminatedArray[strtolower($this->module->name)])) {

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

            $caseSequence .= " WHEN fieldid = {$db->quoteValue($fieldId)} THEN {$db->quoteValue($newFieldSequence['sequence'])}";

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

        $caseSequence .= ' END';

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

        $caseBlock .= ' END';

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

            'sequence' => new yii\db\Expression($caseSequence),

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

        return self::getInstanceFromBlockObject(parent::getInstance($value, $moduleInstance));

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 ($blockObjects as $blockObject) {

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

        return $blockModelList;

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

        $actionNotSupportedModules = ['calendar', 'events', 'faq', 'helpdesk'];

Line exceeds 120 characters; contains 180 characters
Open

        $blocksEliminatedArray = ['calendar' => ['LBL_TASK_INFORMATION', 'LBL_DESCRIPTION_INFORMATION', 'LBL_RELATED_TO', 'LBL_REMINDER_INFORMATION', 'LBL_RECURRENCE_INFORMATION'],

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 vtlib\Block $blockObject - vtlib block object

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

            $blockModel->{$properName} = $propertyValue;

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

            if ($fieldModel->isActiveField()) {

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

     *

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

        $objectProperties = get_object_vars($blockObject);

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

        return $blockModel;

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

                $blockModelList[] = self::getInstanceFromBlockObject($blockObject);

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

        $caseBlock = 'CASE';

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

            $caseBlock .= " WHEN fieldid = {$db->quoteValue($newFieldSequence['fieldid'])} THEN {$db->quoteValue($newFieldSequence['block'])}";

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

     * Function to retrieve block instance from vtlib\Block object.

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 getInstanceFromBlockObject(vtlib\Block $blockObject)

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

    public static function getAllForModule(vtlib\ModuleBasic $moduleModel)

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

        if ($blockObjects) {

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

                    return false;

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

        $db->createCommand()->update('vtiger_field', [

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

        $blockObjects = parent::getAllForModule($moduleModel);

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

                $activeFields[$fieldName] = $fieldModel;

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

     * @return bool true/false

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 false;

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

    {

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

            $fieldIdList[] = $fieldId;

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

        ], ['fieldid' => $fieldIdList])->execute();

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

class Settings_LayoutEditor_Block_Model extends Vtiger_Block_Model

There are no issues that match your filters.

Category
Status