YetiForceCompany/YetiForceCRM

View on GitHub
modules/OSSMailScanner/scanneractions/CreatedEmail.php

Summary

Maintainability
A
3 hrs
Test Coverage
F
0%

Method process has 52 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function process(OSSMail_Mail_Model $mail)
    {
        $type = $mail->getTypeEmail();
        $exceptionsAll = OSSMailScanner_Record_Model::getConfig('exceptions');
        if (!empty($exceptionsAll['crating_mails'])) {
Severity: Major
Found in modules/OSSMailScanner/scanneractions/CreatedEmail.php - About 2 hrs to fix

    Function process has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
    Open

        public function process(OSSMail_Mail_Model $mail)
        {
            $type = $mail->getTypeEmail();
            $exceptionsAll = OSSMailScanner_Record_Model::getConfig('exceptions');
            if (!empty($exceptionsAll['crating_mails'])) {
    Severity: Minor
    Found in modules/OSSMailScanner/scanneractions/CreatedEmail.php - About 1 hr 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

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

        public function process(OSSMail_Mail_Model $mail)
        {
            $type = $mail->getTypeEmail();
            $exceptionsAll = OSSMailScanner_Record_Model::getConfig('exceptions');
            if (!empty($exceptionsAll['crating_mails'])) {

    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

    Reduce the number of returns of this function 4, down to the maximum allowed 3.
    Open

        public function process(OSSMail_Mail_Model $mail)

    Having too many return statements in a function increases the function's essential complexity because the flow of execution is broken each time a return statement is encountered. This makes it harder to read and understand the logic of the function.

    Noncompliant Code Example

    With the default threshold of 3:

    function myFunction(){ // Noncompliant as there are 4 return statements
      if (condition1) {
        return true;
      } else {
        if (condition2) {
          return false;
        } else {
          return true;
        }
      }
      return false;
    }
    

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

        public function process(OSSMail_Mail_Model $mail)

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

            $exceptionsAll = OSSMailScanner_Record_Model::getConfig('exceptions');

    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 {
                App\Db::getInstance()->createCommand()->update('vtiger_ossmailview', [
                    'id' => $mail->get('id'),
                ], ['ossmailviewid' => $mail->getMailCrmId()]
                )->execute();

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

        public function process(OSSMail_Mail_Model $mail)
        {
            $type = $mail->getTypeEmail();
            $exceptionsAll = OSSMailScanner_Record_Model::getConfig('exceptions');
            if (!empty($exceptionsAll['crating_mails'])) {

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

                $record->set('content', \App\TextUtils::htmlTruncate($mail->getContent(), $record->getField('content')->getMaxValue()));

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

                $record->set('orginal_mail', \App\TextUtils::htmlTruncate($mail->get('clean'), $record->getField('orginal_mail')->getMaxValue()));

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

                $record = OSSMailView_Record_Model::getCleanInstance('OSSMailView');

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

                $record->setFromUserValue('subject', $mail->isEmpty('subject') ? '-' : $mail->get('subject'));

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

                $mailForExceptions = (0 === $type) ? $mail->get('to_email') : $mail->get('from_email');

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

                $mailForExceptions = (0 === $type) ? $mail->get('to_email') : $mail->get('from_email');

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

                $toIds = array_merge($mail->findEmailAddress('to_email'), $mail->findEmailAddress('cc_email'), $mail->findEmailAddress('bcc_email'));

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

                $toIds = array_merge($mail->findEmailAddress('to_email'), $mail->findEmailAddress('cc_email'), $mail->findEmailAddress('bcc_email'));

    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 getConfig from undeclared class \OSSMailScanner_Record_Model
    Open

            $exceptionsAll = OSSMailScanner_Record_Model::getConfig('exceptions');

    Returning type array{mailViewId:bool|int} but process() is declared to return int
    Open

                return ['mailViewId' => $mail->getMailCrmId()];

    Returning type false but process() is declared to return int
    Open

                        return false;

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

                App\Db::getInstance()->createCommand()->update('vtiger_ossmailview', [

    Returning type array{mailViewId:int,attachments:array<int>|array<int>|mixed}</int></int> but process() is declared to return int
    Open

                    return ['mailViewId' => $id, 'attachments' => $mail->saveAttachments()];

    Returning type false but process() is declared to return int
    Open

            return false;

    Avoid excessively long class names like OSSMailScanner_CreatedEmail_ScannerAction. Keep class name length under 40.
    Open

    class OSSMailScanner_CreatedEmail_ScannerAction
    {
        /**
         * Process.
         *

    LongClassName

    Since: 2.9

    Detects when classes or interfaces are declared with excessively long names.

    Example

    class ATooLongClassNameThatHintsAtADesignProblem {
    
    }
    
    interface ATooLongInterfaceNameThatHintsAtADesignProblem {
    
    }

    Source https://phpmd.org/rules/naming.html#longclassname

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

    class OSSMailScanner_CreatedEmail_ScannerAction

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

                if ($id = $record->getId()) {

    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

    The class OSSMailScanner_CreatedEmail_ScannerAction is not named in CamelCase.
    Open

    class OSSMailScanner_CreatedEmail_ScannerAction
    {
        /**
         * Process.
         *

    CamelCaseClassName

    Since: 0.2

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

    Example

    class class_name {
    }

    Source

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

            if (false === $mail->getMailCrmId()) {

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

                $record->set('assigned_user_id', $mail->getAccountOwner());

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

                $record->set('to_email', $mail->get('to_email'));

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

            $type = $mail->getTypeEmail();

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

                $record->set('cc_email', $mail->get('cc_email'));

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

                $record = OSSMailView_Record_Model::getCleanInstance('OSSMailView');

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

                foreach ($exceptions as $exception) {

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

                $record->setFromUserValue('subject', $mail->isEmpty('subject') ? '-' : $mail->get('subject'));

    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

                    }

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

                    if (false !== strpos($mailForExceptions, $exception)) {

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

        {

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

            $exceptionsAll = OSSMailScanner_Record_Model::getConfig('exceptions');

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

            if (!empty($exceptionsAll['crating_mails'])) {

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

                $exceptions = explode(',', $exceptionsAll['crating_mails']);

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

                $mailForExceptions = (0 === $type) ? $mail->get('to_email') : $mail->get('from_email');

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

                $record->set('reply_to_email', $mail->get('reply_toaddress'));

    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

         * @param OSSMail_Mail_Model $mail

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

        public function process(OSSMail_Mail_Model $mail)

    Line exceeds 120 characters; contains 145 characters
    Open

                $toIds = array_merge($mail->findEmailAddress('to_email'), $mail->findEmailAddress('cc_email'), $mail->findEmailAddress('bcc_email'));

    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

                $fromIds = array_merge($mail->findEmailAddress('from_email'), $mail->findEmailAddress('reply_toaddress'));

    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

                $record->set('bcc_email', $mail->get('bcc_email'));

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

                $record->set('orginal_mail', \App\TextUtils::htmlTruncate($mail->get('clean'), $record->getField('orginal_mail')->getMaxValue()));

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

                    'cid' => $mail->getUniqueId(),

    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 ['mailViewId' => $mail->getMailCrmId()];

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

         * @return int

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

            }

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

                App\Db::getInstance()->createCommand()->update('vtiger_ossmailview', [

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

                $account = $mail->getAccount();

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

                $record->set('from_email', $mail->get('from_email'));

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

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

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

                $record->save();

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

                $record->setHandlerExceptions([]);

    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

            return false;

    Line exceeds 120 characters; contains 132 characters
    Open

                $record->set('content', \App\TextUtils::htmlTruncate($mail->getContent(), $record->getField('content')->getMaxValue()));

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

                    $record->set('attachments_exist', 1);

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

                    $mail->setMailCrmId($id);

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

                $toIds = array_merge($mail->findEmailAddress('to_email'), $mail->findEmailAddress('cc_email'), $mail->findEmailAddress('bcc_email'));

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

                ]]);

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

                $record->set('ossmailview_sendtype', $mail->getTypeEmail(true));

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

                if ($mail->get('isAttachments') || $mail->get('attachments')) {

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

                ], ['ossmailviewid' => $mail->getMailCrmId()]

    Line exceeds 120 characters; contains 142 characters
    Open

                $record->set('orginal_mail', \App\TextUtils::htmlTruncate($mail->get('clean'), $record->getField('orginal_mail')->getMaxValue()));

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

                $record->set('created_user_id', $mail->getAccountOwner())->set('createdtime', $mail->get('date'));

    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

                $record->set('mbox', $mail->getFolder())->set('type', $type)->set('mid', $mail->get('id'));

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

                $record->set('content', \App\TextUtils::htmlTruncate($mail->getContent(), $record->getField('content')->getMaxValue()));

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

                if ($id = $record->getId()) {

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

                $record->set('uid', $mail->get('message_id'))->set('rc_user', $account['user_id']);

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

                )->execute();

    Line exceeds 120 characters; contains 124 characters
    Open

                $record->set('from_id', implode(',', array_unique($fromIds)))->set('to_id', implode(',', array_unique($toIds)));

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

                    return ['mailViewId' => $id, 'attachments' => $mail->saveAttachments()];

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

                $record->set('from_id', implode(',', array_unique($fromIds)))->set('to_id', implode(',', array_unique($toIds)));

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

                $record->set('date', $mail->get('date'));

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

                $record->setDataForSave(['vtiger_ossmailview' => [

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

                    'id' => $mail->get('id'),

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

    class OSSMailScanner_CreatedEmail_ScannerAction

    Expected 0 spaces before closing bracket; newline found
    Open

                App\Db::getInstance()->createCommand()->update('vtiger_ossmailview', [

    There are no issues that match your filters.

    Category
    Status