YetiForceCompany/YetiForceCRM

View on GitHub
modules/Campaigns/models/Relation.php

Summary

Maintainability
A
3 hrs
Test Coverage
D
62%

Function getRelationField has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
Open

    public function getRelationField()
    {
        $relationField = $this->get('relationField');
        if (!$relationField) {
            $relationField = false;
Severity: Minor
Found in modules/Campaigns/models/Relation.php - About 2 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 getRelationField has 27 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function getRelationField()
    {
        $relationField = $this->get('relationField');
        if (!$relationField) {
            $relationField = false;
Severity: Minor
Found in modules/Campaigns/models/Relation.php - About 1 hr to fix

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

        public function getRelationField()
    Severity: Critical
    Found in modules/Campaigns/models/Relation.php by sonar-php

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

                $db->createCommand()->update('vtiger_campaign_records', ['campaignrelstatusid' => new yii\db\Expression($case)], ['campaignid' => $sourceRecordId])->execute();
    Severity: Minor
    Found in modules/Campaigns/models/Relation.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\Db' in method 'updateStatus'.
    Open

                $db = App\Db::getInstance();
    Severity: Minor
    Found in modules/Campaigns/models/Relation.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

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

            $relationField = $this->get('relationField');
    Severity: Critical
    Found in modules/Campaigns/models/Relation.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.

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

                $db->createCommand()->update('vtiger_campaign_records', ['campaignrelstatusid' => new yii\db\Expression($case)], ['campaignid' => $sourceRecordId])->execute();
    Severity: Critical
    Found in modules/Campaigns/models/Relation.php by phan

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

                    $case .= " WHEN {$db->quoteValue($relatedRecordId)} THEN {$db->quoteValue($status)}";
    Severity: Critical
    Found in modules/Campaigns/models/Relation.php by phan

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

                $db->createCommand()->update('vtiger_campaign_records', ['campaignrelstatusid' => new yii\db\Expression($case)], ['campaignid' => $sourceRecordId])->execute();
    Severity: Critical
    Found in modules/Campaigns/models/Relation.php by phan

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

    class Campaigns_Relation_Model extends Vtiger_Relation_Model

    The class Campaigns_Relation_Model is not named in CamelCase.
    Open

    class Campaigns_Relation_Model extends Vtiger_Relation_Model
    {
        /**
         * Function to update the status of relation.
         *
    Severity: Minor
    Found in modules/Campaigns/models/Relation.php by phpmd

    CamelCaseClassName

    Since: 0.2

    It is considered best practice to use the CamelCase notation to name classes.

    Example

    class class_name {
    }

    Source

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

                $db = App\Db::getInstance();
    Severity: Minor
    Found in modules/Campaigns/models/Relation.php by phpmd

    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

    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 ($relatedModelFields as $fieldName => $fieldModel) {

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

                            $relationFieldArray[$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

                foreach ($statusDetails as $relatedRecordId => $status) {

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

                                $this->set('relationField', $fieldModel);

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

                if (!$relationField && $relationFieldArray) {

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

        public function updateStatus($sourceRecordId, $statusDetails = [])

    Line exceeds 120 characters; contains 178 characters
    Open

            if ($sourceRecordId && $statusDetails && \in_array($this->getRelationModuleModel()->getName(), ['Accounts', 'Leads', 'Vendors', 'Contacts', 'Partners', 'Competition'])) {

    Line exceeds 120 characters; contains 171 characters
    Open

                $db->createCommand()->update('vtiger_campaign_records', ['campaignrelstatusid' => new yii\db\Expression($case)], ['campaignid' => $sourceRecordId])->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 ('userCreator' !== !$fieldModel->getFieldDataType()) {

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

                                break;

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

                $case = ' CASE crmid ';

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

                    if ($fieldModel->isReferenceField()) {

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

                        if (\in_array($parentModule->getName(), $referenceList)) {

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

                        }

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

                    reset($relationFieldArray);

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

                $relationField = false;

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

                $relationFieldArray = [];

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

         * @param int   $sourceRecordId

    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

                $case .= 'ELSE campaignrelstatusid END';

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

         * @return Vtiger_Field_Model

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

                $parentModule = $this->getParentModuleModel();

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

                $relatedModelFields = $relatedModel->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

         * @param array $statusDetails

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

            $relationField = $this->get('relationField');

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

                $relatedModel = $this->getRelationModuleModel();

    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

            return $relationField;

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

                    $this->set('relationField', current($relationFieldArray));

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

         * Function to update the status of relation.

    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

                        $referenceList = $fieldModel->getReferenceList();

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

                $db->createCommand()->update('vtiger_campaign_records', ['campaignrelstatusid' => new yii\db\Expression($case)], ['campaignid' => $sourceRecordId])->execute();

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

            if (!$relationField) {

    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 get relation field for relation module and parent 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 function getRelationField()

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

                    $relationField = current($relationFieldArray);

    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 ($sourceRecordId && $statusDetails && \in_array($this->getRelationModuleModel()->getName(), ['Accounts', 'Leads', 'Vendors', 'Contacts', 'Partners', 'Competition'])) {

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

                    $case .= " WHEN {$db->quoteValue($relatedRecordId)} THEN {$db->quoteValue($status)}";

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

         */

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

                                $relationField = $fieldModel;

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

    class Campaigns_Relation_Model extends Vtiger_Relation_Model

    There are no issues that match your filters.

    Category
    Status