YetiForceCompany/YetiForceCRM

View on GitHub
modules/FInvoice/dashboards/SummationByUser.php

Summary

Maintainability
A
2 hrs
Test Coverage
F
0%

Method getWidgetData has 39 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function getWidgetData($moduleName, $widgetParam, $time)
    {
        $currentUserId = \App\User::getCurrentUserId();
        $s = new \yii\db\Expression('sum(sum_gross)');
        $queryGenerator = new \App\QueryGenerator($moduleName);
Severity: Minor
Found in modules/FInvoice/dashboards/SummationByUser.php - About 1 hr to fix

    Function getWidgetData has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
    Open

        public function getWidgetData($moduleName, $widgetParam, $time)
        {
            $currentUserId = \App\User::getCurrentUserId();
            $s = new \yii\db\Expression('sum(sum_gross)');
            $queryGenerator = new \App\QueryGenerator($moduleName);
    Severity: Minor
    Found in modules/FInvoice/dashboards/SummationByUser.php - About 25 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 '54', column '25').
    Open

            $queryGenerator = new \App\QueryGenerator($moduleName);

    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 '53', column '12').
    Open

            $s = new \yii\db\Expression('sum(sum_gross)');

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

            $widget = Vtiger_Widget_Model::getInstance($request->getInteger('linkid'), \App\User::getCurrentUserId());

    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\Fields\Owner' in method 'getWidgetData'.
    Open

                $chartData['datasets'][0]['backgroundColor'][] = $currentUserId === (int) $row['assigned_user_id'] ? \App\Fields\Owner::getColor($row['assigned_user_id']) : 'rgba(0,0,0,0.25)';

    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\User' in method 'getWidgetData'.
    Open

            $currentUserId = \App\User::getCurrentUserId();

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

            $widget = Vtiger_Widget_Model::getInstance($request->getInteger('linkid'), \App\User::getCurrentUserId());

    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 {
                $viewer->view('dashboards/SummationByUser.tpl', $moduleName);
            }

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

                } else {
                    $chartData['fullLabels'][] = '';
                }

    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\Utils' in method 'getWidgetData'.
    Open

                $chartData['labels'][] = empty($widgetParam['showUsers']) ? '' : \App\Utils::getInitials($label);

    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\Fields\Owner' in method 'getWidgetData'.
    Open

                $label = \App\Fields\Owner::getLabel($row['assigned_user_id']);

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

                $time = Settings_WidgetsManagement_Module_Model::getDefaultDateRange($widget);

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

            $viewer->assign('DTIME', \App\Fields\Date::formatRangeToDisplay($time));

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

            $param = \App\Json::decode($widget->get('data'));

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

                $chartData['fullLabels'] = [];

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

                'datasets' => [

    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 "assigned_user_id" 6 times.
    Open

            $queryGenerator->setField('assigned_user_id');

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

            if (!empty($widgetParam['showUsers'])) {

    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 getColor from undeclared class \App\Fields\Owner
    Open

                $chartData['datasets'][0]['backgroundColor'][] = $currentUserId === (int) $row['assigned_user_id'] ? \App\Fields\Owner::getColor($row['assigned_user_id']) : 'rgba(0,0,0,0.25)';

    Call to undeclared method \Vtiger_Viewer::assign
    Open

            $viewer->assign('WIDGET', $widget);

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

            $queryGenerator->setCustomColumn(['s' => $s]);

    Call to method getCurrentUserId from undeclared class \App\User (Did you mean class \Tests\App\User)
    Open

            $widget = Vtiger_Widget_Model::getInstance($request->getInteger('linkid'), \App\User::getCurrentUserId());

    Call to undeclared method \Vtiger_Viewer::assign
    Open

            $viewer->assign('DTIME', \App\Fields\Date::formatRangeToDisplay($time));

    Call to undeclared method \Vtiger_Viewer::assign
    Open

            $viewer->assign('DATA', $data);

    Call to undeclared method \Vtiger_Viewer::assign
    Open

            $viewer->assign('PARAM', $param);

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

            $queryGenerator->addCondition('saledate', implode(',', $time), 'bw');

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

            $query = $queryGenerator->createQuery();

    Call to method getInstance from undeclared class \Vtiger_Widget_Model
    Open

            $widget = Vtiger_Widget_Model::getInstance($request->getInteger('linkid'), \App\User::getCurrentUserId());

    Call to undeclared method \Vtiger_Viewer::assign
    Open

            $viewer->assign('MODULE_NAME', $moduleName);

    Call to method getCurrentUserId from undeclared class \App\User (Did you mean class \Tests\App\User)
    Open

            $currentUserId = \App\User::getCurrentUserId();

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

            $queryGenerator = new \App\QueryGenerator($moduleName);

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

            $s = new \yii\db\Expression('sum(sum_gross)');

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

            $queryGenerator->setGroup('assigned_user_id');

    Call to method getLabel from undeclared class \App\Fields\Owner
    Open

                $label = \App\Fields\Owner::getLabel($row['assigned_user_id']);

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

            $queryGenerator->setField('assigned_user_id');

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

    class FInvoice_SummationByUser_Dashboard extends Vtiger_IndexAjax_View

    The class FInvoice_SummationByUser_Dashboard is not named in CamelCase.
    Open

    class FInvoice_SummationByUser_Dashboard extends Vtiger_IndexAjax_View
    {
        /**
         * Process.
         *

    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 $s. Configured minimum length is 3.
    Open

            $s = new \yii\db\Expression('sum(sum_gross)');

    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

            $widget = Vtiger_Widget_Model::getInstance($request->getInteger('linkid'), \App\User::getCurrentUserId());

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

         * @param \App\Request $request

    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 process(App\Request $request)

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

         * Process.

    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

                        'tooltips' => [],

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

                }

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

            $time = $request->getDateRange('time');

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

            $moduleName = $request->getModule();

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

            $viewer->assign('DTIME', \App\Fields\Date::formatRangeToDisplay($time));

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

            $queryGenerator->setGroup('assigned_user_id');

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

            ];

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

                $viewer->view('dashboards/SummationByUser.tpl', $moduleName);

    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

            $s = new \yii\db\Expression('sum(sum_gross)');

    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

            $viewer->assign('WIDGET', $widget);

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

                $viewer->view('dashboards/SummationByUserContents.tpl', $moduleName);

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

        public function getWidgetData($moduleName, $widgetParam, $time)

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

            $queryGenerator->setField('assigned_user_id');

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

            $query->having(['>', $s, 0]);

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

            while ($row = $dataReader->read()) {

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

            $viewer = $this->getViewer($request);

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

            $viewer->assign('PARAM', $param);

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

            if ($request->has('content')) {

    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

                ],

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

                    $chartData['fullLabels'][] = '';

    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  $widgetParam

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

         * @param array  $time

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

            $queryGenerator = new \App\QueryGenerator($moduleName);

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

            $query->orderBy(['s' => SORT_DESC]);

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

            return $chartData;

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

            $param = \App\Json::decode($widget->get('data'));

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

            $viewer->assign('MODULE_NAME', $moduleName);

    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

                'labels' => [],

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

            $chartData['show_chart'] = (bool) \count($chartData['datasets'][0]['data']);

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

            if (empty($time)) {

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

                $time = Settings_WidgetsManagement_Module_Model::getDefaultDateRange($widget);

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

         * Get widget data.

    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

            $queryGenerator->addCondition('saledate', implode(',', $time), 'bw');

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

            $chartData = [

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

                'datasets' => [

    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 (!empty($widgetParam['showUsers']) || $currentUserId === (int) $row['assigned_user_id']) {

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

                    $chartData['fullLabels'][] = $label;

    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

            if (!empty($widgetParam['showUsers'])) {

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

            $data = $this->getWidgetData($moduleName, $param, $time);

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

            $viewer->assign('DATA', $data);

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

            $currentUserId = \App\User::getCurrentUserId();

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

            $queryGenerator->setCustomColumn(['s' => $s]);

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

                $chartData['fullLabels'] = [];

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

                $chartData['datasets'][0]['data'][] = round((float) $row['s'], 2);

    Line exceeds 120 characters; contains 188 characters
    Open

                $chartData['datasets'][0]['backgroundColor'][] = $currentUserId === (int) $row['assigned_user_id'] ? \App\Fields\Owner::getColor($row['assigned_user_id']) : 'rgba(0,0,0,0.25)';

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

         *

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

                $label = \App\Fields\Owner::getLabel($row['assigned_user_id']);

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

                $chartData['datasets'][0]['backgroundColor'][] = $currentUserId === (int) $row['assigned_user_id'] ? \App\Fields\Owner::getColor($row['assigned_user_id']) : 'rgba(0,0,0,0.25)';

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

                $chartData['labels'][] = empty($widgetParam['showUsers']) ? '' : \App\Utils::getInitials($label);

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

            }

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

            $dataReader = $query->createCommand()->query();

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

                        'data' => [],

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

                        'backgroundColor' => [],

    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

            $query = $queryGenerator->createQuery();

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

                'show_chart' => false,

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

    class FInvoice_SummationByUser_Dashboard extends Vtiger_IndexAjax_View

    There are no issues that match your filters.

    Category
    Status