YetiForceCompany/YetiForceCRM

View on GitHub
modules/com_vtiger_workflow/tasks/VTWatchdog.php

Summary

Maintainability
A
2 hrs
Test Coverage
F
0%

Method doTask has 38 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function doTask($recordModel)
    {
        $moduleName = $recordModel->getModuleName();
        $recordId = $recordModel->getId();
        switch ($this->recipients) {
Severity: Minor
Found in modules/com_vtiger_workflow/tasks/VTWatchdog.php - About 1 hr to fix

    Function doTask has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
    Open

        public function doTask($recordModel)
        {
            $moduleName = $recordModel->getModuleName();
            $recordId = $recordModel->getId();
            switch ($this->recipients) {
    Severity: Minor
    Found in modules/com_vtiger_workflow/tasks/VTWatchdog.php - About 55 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

    Avoid using static access to class '\App\User' in method 'doTask'.
    Open

            if (!empty($this->skipCurrentUser) && false !== ($key = array_search(\App\User::getCurrentUserId(), $users))) {

    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\PrivilegeUtil' in method 'doTask'.
    Open

                    $users = \App\PrivilegeUtil::getUserByMember($this->recipients);

    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 assigning values to variables in if clauses and the like (line '60', column '52').
    Open

        public function doTask($recordModel)
        {
            $moduleName = $recordModel->getModuleName();
            $recordId = $recordModel->getId();
            switch ($this->recipients) {

    IfStatementAssignment

    Since: 2.7.0

    Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.

    Example

    class Foo
    {
        public function bar($flag)
        {
            if ($foo = 'bar') { // possible typo
                // ...
            }
            if ($baz = 0) { // always false
                // ...
            }
        }
    }

    Source http://phpmd.org/rules/cleancode.html#ifstatementassignment

    Avoid using static access to class 'Vtiger_Watchdog_Model' in method 'doTask'.
    Open

                    $watchdog = Vtiger_Watchdog_Model::getInstanceById($recordId, $moduleName);

    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_Record_Model' in method 'doTask'.
    Open

            $notification = Vtiger_Record_Model::getCleanInstance('Notification');

    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\ModuleHierarchy' in method 'doTask'.
    Open

            $relatedField = \App\ModuleHierarchy::getMappingRelatedField($moduleName);

    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\TextParser' in method 'doTask'.
    Open

            $textParser = \App\TextParser::getInstanceByModel($recordModel);

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

                    $users = array_merge([$recordModel->get('assigned_user_id')], explode(',', $recordModel->get('shownerid')));

    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 with 3 arg(s) to \Vtiger_Record_Model::set() which only takes 2 arg(s) defined at /code/modules/Vtiger/models/Record.php:88
    Open

            $notification->set('title', $textParser->setContent($this->title)->parse()->getContent(), 'Text');

    Reference to undeclared property \VTWatchdog->type
    Open

            $notification->set('notification_type', $this->type);

    Reference to undeclared property \VTWatchdog->recipients
    Open

            switch ($this->recipients) {

    Reference to undeclared property \VTWatchdog->skipCurrentUser
    Open

            if (!empty($this->skipCurrentUser) && false !== ($key = array_search(\App\User::getCurrentUserId(), $users))) {

    Reference to undeclared property \VTWatchdog->recipients
    Open

                    $users = \App\PrivilegeUtil::getUserByMember($this->recipients);

    Reference to undeclared property \VTWatchdog->title
    Open

            $notification->set('title', $textParser->setContent($this->title)->parse()->getContent(), 'Text');

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

            if (!empty($this->skipCurrentUser) && false !== ($key = array_search(\App\User::getCurrentUserId(), $users))) {

    Call to method getInstanceByModel from undeclared class \App\TextParser (Did you mean class \Tests\App\TextParser)
    Open

            $textParser = \App\TextParser::getInstanceByModel($recordModel);

    Call with 3 arg(s) to \Vtiger_Record_Model::set() which only takes 2 arg(s) defined at /code/modules/Vtiger/models/Record.php:88
    Open

            $notification->set('description', $textParser->setContent($this->message)->parse()->getContent(), 'Text');

    Reference to undeclared property \VTWatchdog->message
    Open

            $notification->set('description', $textParser->setContent($this->message)->parse()->getContent(), 'Text');

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

    class VTWatchdog extends VTTask

    A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 12 and the first side effect is on line 10.
    Open

    <?php

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

        public function getFieldNames()

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

         * Get request method name for field name.

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

         * Execute task.

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

         * @param Vtiger_Record_Model $recordModel

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

         */

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

                case 'showner':

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

        /**

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

                case 'owner_and_showner':

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

        public $executeImmediately = 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

        public function doTask($recordModel)

    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

        }

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

                    $users = explode(',', $recordModel->get('shownerid'));

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

            return ['type', 'message', 'recipients', 'title', 'skipCurrentUser'];

    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

                return false;

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

            $notification->setHandlerExceptions(['disableHandlers' => true]);

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

            switch ($this->recipients) {

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

                default:

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

                    $users = \App\PrivilegeUtil::getUserByMember($this->recipients);

    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

            $users = array_filter($users);

    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 getFieldsNamesRequestMethod(): array

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

                case 'watchdog':

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

                    $watchdog = Vtiger_Watchdog_Model::getInstanceById($recordId, $moduleName);

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

                case 'owner':

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

            if (!empty($this->skipCurrentUser) && false !== ($key = array_search(\App\User::getCurrentUserId(), $users))) {

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

            $textParser = \App\TextParser::getInstanceByModel($recordModel);

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

            $notification->save();

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

        {

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

                    $users = [$recordModel->get('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

         *

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

            $moduleName = $recordModel->getModuleName();

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

            $relatedField = \App\ModuleHierarchy::getMappingRelatedField($moduleName);

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

        public $srcWatchdogModule = 'Notification';

    Line exceeds 120 characters; contains 124 characters
    Open

                    $users = array_merge([$recordModel->get('assigned_user_id')], explode(',', $recordModel->get('shownerid')));

    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

            $recordId = $recordModel->getId();

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

                    $users = array_merge([$recordModel->get('assigned_user_id')], explode(',', $recordModel->get('shownerid')));

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

            return ['message' => 'getForHtml'];

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

        {

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

                    $users = $watchdog->getWatchingUsers();

    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

                    break;

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

            $notification->set('notification_status', 'PLL_UNREAD');

    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

            $notification->set('title', $textParser->setContent($this->title)->parse()->getContent(), 'Text');

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

        }

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

            $notification->set('shownerid', implode(',', $users));

    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

                unset($users[$key]);

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

            $notification = Vtiger_Record_Model::getCleanInstance('Notification');

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

            $notification->set('description', $textParser->setContent($this->message)->parse()->getContent(), 'Text');

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

            if (empty($users)) {

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

            $notification->set($relatedField, $recordId);

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

            $notification->set('notification_type', $this->type);

    There are no issues that match your filters.

    Category
    Status