YetiForceCompany/YetiForceCRM

View on GitHub
app/Fields/SharedOwner.php

Summary

Maintainability
A
3 hrs
Test Coverage
F
57%

Function getById has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
Open

    public static function getById($crmId)
    {
        $values = [];
        if (\is_array($crmId)) {
            foreach ($crmId as $id) {
Severity: Minor
Found in app/Fields/SharedOwner.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 getById has 28 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public static function getById($crmId)
    {
        $values = [];
        if (\is_array($crmId)) {
            foreach ($crmId as $id) {
Severity: Minor
Found in app/Fields/SharedOwner.php - About 1 hr to fix

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

        public static function getById($crmId)
    Severity: Critical
    Found in app/Fields/SharedOwner.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 '42', column '18').
    Open

                $query = (new \App\Db\Query())->from('u_#__crmentity_showners')->where(['crmid' => $crmId]);
    Severity: Minor
    Found in app/Fields/SharedOwner.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\Cache' in method 'getById'.
    Open

                    Cache::save('SharedOwnerFieldValue', $crmId, $values, Cache::LONG);
    Severity: Minor
    Found in app/Fields/SharedOwner.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

    Avoid using static access to class '\App\Cache' in method 'getById'.
    Open

                        Cache::save('SharedOwnerFieldValue', $id, $value, Cache::LONG);
    Severity: Minor
    Found in app/Fields/SharedOwner.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

    Avoid using static access to class '\App\Cache' in method 'getById'.
    Open

                return Cache::get('SharedOwnerFieldValue', $crmId);
    Severity: Minor
    Found in app/Fields/SharedOwner.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

    Avoid using static access to class '\App\Cache' in method 'getById'.
    Open

                        $values[$id] = Cache::get('SharedOwnerFieldValue', $id);
    Severity: Minor
    Found in app/Fields/SharedOwner.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

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

                } else {
                    $values = $query->select(['userid'])->distinct()->column();
                    if (empty($values)) {
                        $values = [];
                    }
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpmd

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

            } elseif (Cache::has('SharedOwnerFieldValue', $crmId)) {
    Severity: Minor
    Found in app/Fields/SharedOwner.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

    Avoid using static access to class '\App\Cache' in method 'getById'.
    Open

                    if (Cache::has('SharedOwnerFieldValue', $id)) {
    Severity: Minor
    Found in app/Fields/SharedOwner.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 "SharedOwnerFieldValue" 6 times.
    Open

                    if (Cache::has('SharedOwnerFieldValue', $id)) {
    Severity: Critical
    Found in app/Fields/SharedOwner.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.

    Argument 2 (key) is int but \App\Cache::has() takes string defined at /code/app/Cache.php:74
    Open

            } elseif (Cache::has('SharedOwnerFieldValue', $crmId)) {
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phan

    Argument 2 (key) is int but \App\Cache::has() takes string defined at /code/app/Cache.php:74
    Open

                    if (Cache::has('SharedOwnerFieldValue', $id)) {
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phan

    Argument 2 (key) is int but \App\Cache::get() takes string defined at /code/app/Cache.php:61
    Open

                        $values[$id] = Cache::get('SharedOwnerFieldValue', $id);
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phan

    Argument 2 (key) is int but \App\Cache::save() takes string defined at /code/app/Cache.php:89
    Open

                    Cache::save('SharedOwnerFieldValue', $crmId, $values, Cache::LONG);
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phan

    Argument 2 (key) is int but \App\Cache::get() takes string defined at /code/app/Cache.php:61
    Open

                return Cache::get('SharedOwnerFieldValue', $crmId);
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phan

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

                $query = (new \App\Db\Query())->from('u_#__crmentity_showners')->where(['crmid' => $crmId]);
    Severity: Critical
    Found in app/Fields/SharedOwner.php by phan

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

        /**
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

         */
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                }
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                        Cache::save('SharedOwnerFieldValue', $id, $value, Cache::LONG);
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

            if (\is_array($crmId)) {
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                } else {
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                    $values = $query->select(['userid'])->distinct()->column();
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

        }
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

            } elseif (Cache::has('SharedOwnerFieldValue', $crmId)) {
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                }
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

            }
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                if (\is_array($crmId)) {
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                    }
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                $query = (new \App\Db\Query())->from('u_#__crmentity_showners')->where(['crmid' => $crmId]);
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                    if (empty($values)) {
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

            }
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

         *
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

        public static function getById($crmId)
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                foreach ($crmId as $id) {
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

         * Get shared owner list by crmid.
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

         *
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                return Cache::get('SharedOwnerFieldValue', $crmId);
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                    $rows = $query->select(['crmid', 'userid'])->createCommand()->queryAllByGroup(2);
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                    }
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                    Cache::save('SharedOwnerFieldValue', $crmId, $values, Cache::LONG);
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

         * @param int|int[] $crmId
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                        unset($crmId[$id]);
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

            if ($crmId) {
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                        $values[$id] = $value;
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

         * @return array|int[]
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                        $values = [];
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                        $values[$id] = Cache::get('SharedOwnerFieldValue', $id);
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

            $values = [];
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                    foreach ($rows as $id => $value) {
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

        {
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                    if (Cache::has('SharedOwnerFieldValue', $id)) {
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

            return $values;
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

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

                    }
    Severity: Minor
    Found in app/Fields/SharedOwner.php by phpcodesniffer

    There are no issues that match your filters.

    Category
    Status