YetiForceCompany/YetiForceCRM

View on GitHub
modules/Settings/MailSmtp/actions/SaveAjax.php

Summary

Maintainability
A
3 hrs
Test Coverage
F
0%

Method updateSmtp has 51 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function updateSmtp(App\Request $request)
    {
        $encryptInstance = \App\Encryption::getInstance();
        $data = [
            'mailer_type' => $request->getByType('mailer_type'),
Severity: Major
Found in modules/Settings/MailSmtp/actions/SaveAjax.php - About 2 hrs to fix

    Function updateSmtp has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
    Open

        public function updateSmtp(App\Request $request)
        {
            $encryptInstance = \App\Encryption::getInstance();
            $data = [
                'mailer_type' => $request->getByType('mailer_type'),
    Severity: Minor
    Found in modules/Settings/MailSmtp/actions/SaveAjax.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 updateSmtp() has an NPath complexity of 640. The configured NPath complexity threshold is 200.
    Open

        public function updateSmtp(App\Request $request)
        {
            $encryptInstance = \App\Encryption::getInstance();
            $data = [
                'mailer_type' => $request->getByType('mailer_type'),

    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 updateSmtp() has a Cyclomatic Complexity of 12. The configured cyclomatic complexity threshold is 10.
    Open

        public function updateSmtp(App\Request $request)
        {
            $encryptInstance = \App\Encryption::getInstance();
            $data = [
                'mailer_type' => $request->getByType('mailer_type'),

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

        public function updateSmtp(App\Request $request)

    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 '76', column '19').
    Open

            $response = new Vtiger_Response();

    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 '56', column '17').
    Open

            $mailer = new \App\Mailer();

    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\Encryption' in method 'updateSmtp'.
    Open

            $encryptInstance = \App\Encryption::getInstance();

    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_MailSmtp_Record_Model' in method 'updateSmtp'.
    Open

                    $recordModel = Settings_MailSmtp_Record_Model::getInstanceById($request->getInteger('record'));

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

                'unsubscribe' => App\Json::encode($request->getArray('unsubscribe', 'Text')),

    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\Purifier' in method 'updateSmtp'.
    Open

                $result = ['success' => false, 'message' => \App\Purifier::purify($testMailer['error'])];

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

                } else {
                    $recordModel = Settings_MailSmtp_Record_Model::getCleanInstance();
                }

    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 'Settings_MailSmtp_Record_Model' in method 'updateSmtp'.
    Open

                    $recordModel = Settings_MailSmtp_Record_Model::getCleanInstance();

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

            } else {
                $result = ['success' => false, 'message' => \App\Purifier::purify($testMailer['error'])];
            }

    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\Purifier' in method 'updateSmtp'.
    Open

                'smtp_folder' => \App\Purifier::decodeHtml($request->getByType('smtp_folder', 'Text')),

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

                'smtp_validate_cert' => $request->isEmpty('smtp_validate_cert') ? 0 : $request->getInteger('smtp_validate_cert'),

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

                'save_send_mail' => $request->isEmpty('save_send_mail') ? 0 : $request->getInteger('save_send_mail'),

    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 "default" 5 times.
    Open

                'default' => $request->isEmpty('default') ? 0 : $request->getInteger('default'),

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

                'smtp_port' => $request->isEmpty('smtp_port') ? '' : $request->getInteger('smtp_port'),

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

                'authentication' => $request->isEmpty('authentication') ? 0 : $request->getInteger('authentication'),

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

                'individual_delivery' => $request->isEmpty('individual_delivery') ? 0 : $request->getInteger('individual_delivery'),

    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 getInstance from undeclared class \App\Encryption (Did you mean class \Tests\App\Encryption)
    Open

            $encryptInstance = \App\Encryption::getInstance();

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

                    App\Db::getInstance('admin')->createCommand()->update('s_#__mail_smtp', ['default' => 0])->execute();

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

    class Settings_MailSmtp_SaveAjax_Action extends Settings_Vtiger_Basic_Action

    The class Settings_MailSmtp_SaveAjax_Action is not named in CamelCase.
    Open

    class Settings_MailSmtp_SaveAjax_Action extends Settings_Vtiger_Basic_Action
    {
        /**
         * Class constructor.
         */

    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

         */

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

                'from_email' => $request->getByType('from_email', 'Text'),

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

                'smtp_host' => $request->getByType('smtp_host', 'Text'),

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

            $mailer->loadSmtp($data);

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

         * Function updates smtp configuration.

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

         *

    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

                'authentication' => $request->isEmpty('authentication') ? 0 : $request->getInteger('authentication'),

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

                'individual_delivery' => $request->isEmpty('individual_delivery') ? 0 : $request->getInteger('individual_delivery'),

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

         * Class constructor.

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

            $this->exposeMethod('updateSmtp');

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

                'mailer_type' => $request->getByType('mailer_type'),

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

                'name' => $request->getByType('name', 'Text'),

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

                'unsubscribe' => App\Json::encode($request->getArray('unsubscribe', 'Text')),

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

                'smtp_folder' => \App\Purifier::decodeHtml($request->getByType('smtp_folder', 'Text')),

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

                'host' => $request->getByType('host', 'Text'),

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

                'options' => $request->getByType('options', 'Text'),

    Line exceeds 120 characters; contains 128 characters
    Open

                'individual_delivery' => $request->isEmpty('individual_delivery') ? 0 : $request->getInteger('individual_delivery'),

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

                'secure' => $request->getByType('secure'),

    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

            $encryptInstance = \App\Encryption::getInstance();

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

                'default' => $request->isEmpty('default') ? 0 : $request->getInteger('default'),

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

                'smtp_username' => $request->getByType('smtp_username', 'Text'),

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

                'smtp_validate_cert' => $request->isEmpty('smtp_validate_cert') ? 0 : $request->getInteger('smtp_validate_cert'),

    Line exceeds 120 characters; contains 125 characters
    Open

                'smtp_validate_cert' => $request->isEmpty('smtp_validate_cert') ? 0 : $request->getInteger('smtp_validate_cert'),

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

        public function updateSmtp(App\Request $request)

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

            if (isset($testMailer['result']) && false !== $testMailer['result']) {

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

         */

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

                'confirm_reading_to' => $request->getByType('confirm_reading_to', 'Text'),

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

                'organization' => $request->getByType('organization', 'Text'),

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

            $testMailer = $mailer->test();

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

                }

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

            $response->setResult($result);

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

        }

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

                'priority' => $request->getByType('priority', '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

         * @param \App\Request $request

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

                'reply_to' => $request->getByType('reply_to', 'Text'),

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

                'save_send_mail' => $request->isEmpty('save_send_mail') ? 0 : $request->getInteger('save_send_mail'),

    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

        public function __construct()

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

            $mailer = new \App\Mailer();

    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

                $result = ['success' => false, 'message' => \App\Purifier::purify($testMailer['error'])];

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

            $response->emit();

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

        /**

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

                'port' => $request->getInteger('port'),

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

                'smtp_port' => $request->isEmpty('smtp_port') ? '' : $request->getInteger('smtp_port'),

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

                    $recordModel->set($key, $value);

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

            parent::__construct();

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

        {

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

                'password' => $encryptInstance->encrypt($request->getRaw('password')),

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

                'smtp_password' => $encryptInstance->encrypt($request->getRaw('smtp_password')),

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

                'username' => $request->getByType('username', 'Text'),

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

                'from_name' => $request->getByType('from_name', 'Text'),

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

                $recordModel->save();

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

                    $recordModel = Settings_MailSmtp_Record_Model::getCleanInstance();

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

            }

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

            $response = new Vtiger_Response();

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

                if (!$request->isEmpty('record')) {

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

                    $recordModel = Settings_MailSmtp_Record_Model::getInstanceById($request->getInteger('record'));

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

                    App\Db::getInstance('admin')->createCommand()->update('s_#__mail_smtp', ['default' => 0])->execute();

    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

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

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

                $result = ['success' => true, 'url' => $recordModel->getDetailViewUrl()];

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

                if (!empty($data['default'])) {

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

    class Settings_MailSmtp_SaveAjax_Action extends Settings_Vtiger_Basic_Action

    There are no issues that match your filters.

    Category
    Status