YetiForceCompany/YetiForceCRM

View on GitHub
modules/Vtiger/crons/MultiReference.php

Summary

Maintainability
C
1 day
Test Coverage
F
13%

Function process has a Cognitive Complexity of 37 (exceeds 5 allowed). Consider refactoring.
Open

    public function process()
    {
        $db = \App\Db::getInstance();
        $executed = [];
        $limit = 1000;
Severity: Minor
Found in modules/Vtiger/crons/MultiReference.php - About 5 hrs 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

Method process has 60 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function process()
    {
        $db = \App\Db::getInstance();
        $executed = [];
        $limit = 1000;
Severity: Major
Found in modules/Vtiger/crons/MultiReference.php - About 2 hrs to fix

    The method process() has a Cyclomatic Complexity of 10. The configured cyclomatic complexity threshold is 10.
    Open

        public function process()
        {
            $db = \App\Db::getInstance();
            $executed = [];
            $limit = 1000;

    CyclomaticComplexity

    Since: 0.1

    Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.

    Example

    // Cyclomatic Complexity = 11
    class Foo {
    1   public function example() {
    2       if ($a == $b) {
    3           if ($a1 == $b1) {
                    fiddle();
    4           } elseif ($a2 == $b2) {
                    fiddle();
                } else {
                    fiddle();
                }
    5       } elseif ($c == $d) {
    6           while ($c == $d) {
                    fiddle();
                }
    7        } elseif ($e == $f) {
    8           for ($n = 0; $n < $h; $n++) {
                    fiddle();
                }
            } else {
                switch ($z) {
    9               case 1:
                        fiddle();
                        break;
    10              case 2:
                        fiddle();
                        break;
    11              case 3:
                        fiddle();
                        break;
                    default:
                        fiddle();
                        break;
                }
            }
        }
    }

    Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity

    Refactor this function to reduce its Cognitive Complexity from 36 to the 15 allowed.
    Open

        public function process()

    Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.

    See

    Missing class import via use statement (line '24', column '17').
    Open

            $query = (new \App\Db\Query())->from('s_#__multireference');

    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 '31', column '29').
    Open

                        $queryGenerator = (new App\QueryGenerator($sourceModule))

    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 'process'.
    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

    The method process uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
    Open

                            } else {
                                $db->createCommand()
                                    ->update('s_#__multireference', [
                                        'lastid' => $id,
                                    ], ['source_module' => $sourceModule, 'dest_module' => $destModule, 'type' => 0])

    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

    The method process uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
    Open

                    } else {
                        if (\App\Record::isExists($multiReference['lastid'], $sourceModule)) {
                            if (\in_array($multiReference['lastid'], $executed)) {
                                continue;
                            }

    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 '\Vtiger_MultiReferenceValue_UIType' in method 'process'.
    Open

                        $fields = \Vtiger_MultiReferenceValue_UIType::getFieldsByModules($sourceModule, $destModule);

    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\Record' in method 'process'.
    Open

                        if (\App\Record::isExists($multiReference['lastid'], $sourceModule)) {

    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_MultiReferenceValue_UIType' in method 'process'.
    Open

                            $fields = Vtiger_MultiReferenceValue_UIType::getFieldsByModules($sourceModule, $destModule);

    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 "lastid" 8 times.
    Open

                            ->addCondition('id', $multiReference['lastid'], 'a')

    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 "s_#__multireference" 4 times.
    Open

            $query = (new \App\Db\Query())->from('s_#__multireference');

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

                    $destModule = $multiReference['dest_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 "source_module" 4 times.
    Open

                    $sourceModule = $multiReference['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.

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

                                $db->createCommand()
    Severity: Critical
    Found in modules/Vtiger/crons/MultiReference.php by phan

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

                        $db->createCommand()->delete('s_#__multireference', [
    Severity: Critical
    Found in modules/Vtiger/crons/MultiReference.php by phan

    Call to undeclared method \Vtiger_Base_UIType::reloadValue
    Open

                                \Vtiger_Field_Model::getInstanceFromFieldId($fieldId)
    Severity: Critical
    Found in modules/Vtiger/crons/MultiReference.php by phan

    Call to method __construct from undeclared class \App\QueryGenerator (Did you mean class \Tests\App\QueryGenerator)
    Open

                        $queryGenerator = (new App\QueryGenerator($sourceModule))
    Severity: Critical
    Found in modules/Vtiger/crons/MultiReference.php by phan

    Call to method setFields from undeclared class \App\QueryGenerator (Did you mean class \Tests\App\QueryGenerator)
    Open

                        $queryGenerator = (new App\QueryGenerator($sourceModule))
    Severity: Critical
    Found in modules/Vtiger/crons/MultiReference.php by phan

    Call to undeclared method \Vtiger_Base_UIType::reloadValue
    Open

                                \Vtiger_Field_Model::getInstanceFromFieldId($fieldId)
    Severity: Critical
    Found in modules/Vtiger/crons/MultiReference.php by phan

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

            $query = (new \App\Db\Query())->from('s_#__multireference');
    Severity: Critical
    Found in modules/Vtiger/crons/MultiReference.php by phan

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

                                $db->createCommand()->delete('s_#__multireference', [
    Severity: Critical
    Found in modules/Vtiger/crons/MultiReference.php by phan

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

    class Vtiger_MultiReference_Cron extends \App\CronHandler

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

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

    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

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

    class Vtiger_MultiReference_Cron extends \App\CronHandler
    {
        /** {@inheritdoc} */
        public function process()
        {

    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

            $query = (new \App\Db\Query())->from('s_#__multireference');

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

                                $db->createCommand()->delete('s_#__multireference', [

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

                                    ->update('s_#__multireference', [

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

                            if (\in_array($multiReference['lastid'], $executed)) {

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

                    $sourceModule = $multiReference['source_module'];

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

                            ->setOrder('id', 'DESC');

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

                        $dataReader = $queryGenerator->setOrder('id', 'ASC')->createQuery(true)->limit($limit)->createCommand()->query();

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

                                    ->execute();

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

            $executed = [];

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

                    if (0 === (int) $multiReference['type']) {

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

                            }

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

                                ])->execute();

    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 (\App\Record::isExists($multiReference['lastid'], $sourceModule)) {

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

                        $dataReader->close();

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

                                    ->getUITypeModel()

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

                        $db->createCommand()->delete('s_#__multireference', [

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

        }

    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

                    $destModule = $multiReference['dest_module'];

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

                                    'source_module' => $sourceModule,

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

                                        'lastid' => $id,

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

                                    ->reloadValue($multiReference['lastid']);

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

            }

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

                        $queryGenerator = (new App\QueryGenerator($sourceModule))

    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

                            foreach ($fields as $fieldId) {

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

                                    'type' => 0,

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

                                $executed[] = $multiReference['lastid'];

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

                            'source_module' => $sourceModule,

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

                            'lastid' => $multiReference['lastid'],

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

                            'type' => $multiReference['type'],

    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 ($rows as $multiReference) {

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

                            foreach ($fields as $fieldId) {

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

                                    ->getUITypeModel()

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

                            if ($lastId === $id) {

    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

                            }

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

                        ])->execute();

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

        public function process()

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

                                $db->createCommand()

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

                                \Vtiger_Field_Model::getInstanceFromFieldId($fieldId)

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

                        $fields = \Vtiger_MultiReferenceValue_UIType::getFieldsByModules($sourceModule, $destModule);

    Line exceeds 120 characters; contains 133 characters
    Open

                        $dataReader = $queryGenerator->setOrder('id', 'ASC')->createQuery(true)->limit($limit)->createCommand()->query();

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

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

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

            foreach ($query->batch(10) as $rows) {

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

                $this->updateLastActionTime();

    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

                                \Vtiger_Field_Model::getInstanceFromFieldId($fieldId)

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

                            ->addCondition('id', $multiReference['lastid'], 'a')

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

                                    'dest_module' => $destModule,

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

                            $fields = Vtiger_MultiReferenceValue_UIType::getFieldsByModules($sourceModule, $destModule);

    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

            $limit = 1000;

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

                            ->setFields(['id'])

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

                                    ], ['source_module' => $sourceModule, 'dest_module' => $destModule, 'type' => 0])

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

                            'dest_module' => $destModule,

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

                        $lastId = $queryGenerator->createQuery()->scalar();

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

                                    ->reloadValue($id);

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

                                continue;

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

    class Vtiger_MultiReference_Cron extends \App\CronHandler

    There are no issues that match your filters.

    Category
    Status