YetiForceCompany/YetiForceCRM

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

Summary

Maintainability
A
3 hrs
Test Coverage
F
0%

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

    public function doTask($recordModel)
    {
        $mailerContent = [
            'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),
        ];
Severity: Minor
Found in modules/com_vtiger_workflow/tasks/VTEmailTask.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 doTask has 40 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function doTask($recordModel)
    {
        $mailerContent = [
            'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),
        ];
Severity: Minor
Found in modules/com_vtiger_workflow/tasks/VTEmailTask.php - About 1 hr to fix

    The method doTask() has an NPath complexity of 1280. The configured NPath complexity threshold is 200.
    Open

        public function doTask($recordModel)
        {
            $mailerContent = [
                'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),
            ];

    NPathComplexity

    Since: 0.1

    The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.

    Example

    class Foo {
        function bar() {
            // lots of complicated code
        }
    }

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

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

        public function doTask($recordModel)
        {
            $mailerContent = [
                'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),
            ];

    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 19 to the 15 allowed.
    Open

        public function doTask($recordModel)

    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

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

                \App\Mailer::addMail($mailerContent);

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

            $emailParser = \App\EmailParser::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

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

                'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),

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

                        } else {
                            $mailerContent['from'] = ['email' => $key, 'name' => $value];
                        }

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

            return ['subject', 'content', 'recepient', 'emailcc', 'emailbcc', 'fromEmail', 'smtp', 'emailoptout'];

    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.

    Static call to undeclared method \App\EmailParser::getInstanceByModel
    Open

            $emailParser = \App\EmailParser::getInstanceByModel($recordModel);

    Reference to undeclared property \VTEmailTask->emailbcc
    Open

            $bccEmail = $emailParser->setContent($this->emailbcc)->parse()->getContent(true);

    Reference to undeclared property \VTEmailTask->fromEmail
    Open

                $fromEmailDetails = $emailParser->setContent($this->fromEmail)->parse()->getContent(true);

    Reference to undeclared property \VTEmailTask->emailcc
    Open

            $ccEmail = $emailParser->setContent($this->emailcc)->parse()->getContent(true);

    Reference to undeclared property \VTEmailTask->fromEmail
    Open

            if ($this->fromEmail) {

    Reference to undeclared property \VTEmailTask->recepient
    Open

            $toEmail = $emailParser->setContent($this->recepient)->parse()->getContent(true);

    Reference to undeclared property \VTEmailTask->content (Did you mean expr->contents)
    Open

            $mailerContent['content'] = $textParser->setContent($this->content)->parse()->getContent();

    Reference to undeclared property \VTEmailTask->smtp
    Open

                'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),

    Reference to undeclared property \VTEmailTask->emailoptout
    Open

            $emailParser->emailoptout = $this->emailoptout ? true : false;

    Reference to undeclared property \VTEmailTask->subject
    Open

            $mailerContent['subject'] = $textParser->setContent($this->subject)->parse()->getContent();

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

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

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

    class VTEmailTask 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

                $mailerContent['to'] = $toEmail;

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

                if ($fromEmailDetails) {

    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

            ];

    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

         * @param Vtiger_Record_Model $recordModel

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

                \App\Mailer::addMail($mailerContent);

    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

                'smtp_id' => ($this->smtp) ? $this->smtp : App\Mail::getDefaultSmtp(),

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

                    foreach ($fromEmailDetails as $key => $value) {

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

                }

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

            unset($emailParser);

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

            if (!empty($mailerContent['content'])) {

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

        // Sending email takes more time, this should be handled via queue all the time.

    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 ($toEmail) {

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

            $bccEmail = $emailParser->setContent($this->emailbcc)->parse()->getContent(true);

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

            if ($this->fromEmail) {

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

                        if (\is_int($key)) {

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

                            $mailerContent['from'] = ['email' => $key, 'name' => $value];

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

            }

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

            $mailerContent['content'] = $textParser->setContent($this->content)->parse()->getContent();

    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

            $toEmail = $emailParser->setContent($this->recepient)->parse()->getContent(true);

    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

                $fromEmailDetails = $emailParser->setContent($this->fromEmail)->parse()->getContent(true);

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

                $mailerContent['cc'] = $ccEmail;

    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

            $emailParser->emailoptout = $this->emailoptout ? true : false;

    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($toEmail) && empty($ccEmail) && empty($bccEmail)) {

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

            unset($textParser);

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

            $mailerContent = [

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

            if ($bccEmail) {

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

                            $mailerContent['from'] = ['email' => $value, 'name' => $value];

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

            if ($ccEmail) {

    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

            }

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

         */

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

            $emailParser = \App\EmailParser::getInstanceByModel($recordModel);

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

            $mailerContent['subject'] = $textParser->setContent($this->subject)->parse()->getContent();

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

            return ['subject', 'content', 'recepient', 'emailcc', 'emailbcc', 'fromEmail', 'smtp', 'emailoptout'];

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

        {

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

            $ccEmail = $emailParser->setContent($this->emailcc)->parse()->getContent(true);

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

                $mailerContent['bcc'] = $bccEmail;

    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

         * Execute task.

    There are no issues that match your filters.

    Category
    Status