YetiForceCompany/YetiForceCRM

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

Summary

Maintainability
A
25 mins
Test Coverage
F
0%

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

    public function saveModuleCustomNumberingAdvanceData(App\Request $request)
    {
        $updated = false;
        $sourceModule = $request->getByType('sourceModule', 2);
        $moduleId = \App\Module::getModuleId($sourceModule);
Severity: Minor
Found in modules/Settings/RecordNumbering/actions/SaveAjax.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 '56', 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 '101', 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 '33', column '14').
Open

            throw new \App\Exceptions\AppException('LBL_PERMISSION_DENIED');

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 '84', 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

The method saveModuleCustomNumberingData uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
Open

        } else {
            $message = App\Language::translate('LBL_PREFIX_IN_USE', $qualifiedModuleName);
            $response->setError($message);
        }

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_RecordNumbering_Module_Model' in method 'saveModuleCustomNumberingData'.
Open

        $moduleModel = Settings_RecordNumbering_Module_Model::getInstance($request->getByType('sourceModule', 2));

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\RecordNumber' in method 'saveModuleCustomNumberingAdvanceData'.
Open

            $sequenceField = \App\Fields\RecordNumber::getInstance($moduleId);

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\Language' in method 'saveModuleCustomNumberingAdvanceData'.
Open

            $response->setResult(App\Language::translate('LBL_SUCCESSFULLY_UPDATED', $sourceModule));

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\Language' in method 'saveModuleCustomNumberingAdvanceData'.
Open

            $response->setError(false, App\Language::translate('LBL_ERROR_WHILE_UPDATING', $sourceModule));

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\Module' in method 'saveModuleCustomNumberingAdvanceData'.
Open

        $moduleId = \App\Module::getModuleId($sourceModule);

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\Language' in method 'saveModuleCustomNumberingData'.
Open

            $response->setResult(App\Language::translate('LBL_SUCCESSFULLY_UPDATED', $qualifiedModuleName));

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

        } else {
            $response->setError(false, App\Language::translate('LBL_ERROR_WHILE_UPDATING', $sourceModule));
        }

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

        } else {
            $moduleModel->set('reset_sequence', '');
        }

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\Language' in method 'saveModuleCustomNumberingData'.
Open

            $message = App\Language::translate('LBL_PREFIX_IN_USE', $qualifiedModuleName);

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

        $moduleModel->set('sequenceNumber', $request->getByType('sequenceNumber', 'Integer'));

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

        $sourceModule = $request->getByType('sourceModule', 2);

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

        $moduleModel->set('leading_zeros', $request->getByType('leading_zeros', 'Integer'));

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

        if (!$request->isEmpty('reset_sequence') && \in_array($request->getByType('reset_sequence'), ['Y', 'M', 'D'])) {

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\Fields\RecordNumber (Did you mean class \Tests\Base\RecordNumber)
Open

            $sequenceField = \App\Fields\RecordNumber::getInstance($moduleId);

Call to method getInstance from undeclared class \App\Fields\RecordNumber (Did you mean class \Tests\Base\RecordNumber)
Open

        $result = App\Fields\RecordNumber::getInstance($request->getByType('sourceModule', 2))->updateRecords();

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

class Settings_RecordNumbering_SaveAjax_Action extends Settings_Vtiger_Index_Action

The class Settings_RecordNumbering_SaveAjax_Action is not named in CamelCase.
Open

class Settings_RecordNumbering_SaveAjax_Action extends Settings_Vtiger_Index_Action
{
    use \App\Controller\ExposeMethod;

    /** {@inheritdoc} */

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

    /** {@inheritdoc} */

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

        if (!$sourceModule) {

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

        $moduleModel->set('prefix', $request->getByType('prefix', 'Text'));

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

    /** {@inheritdoc} */

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

        parent::checkPermission($request);

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

            throw new \App\Exceptions\AppException('LBL_PERMISSION_DENIED');

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

                $sequenceField->updateModuleVariablesSequences($picklistSequence, ['value' => $picklistKey]);

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

        $sourceModule = $request->getByType('sourceModule', 2);

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

     * Function save module custom numbering data.

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

    {

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

            $moduleModel->set('reset_sequence', '');

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

        if ($updated) {

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

     * Function to update record with sequence number.

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

     */

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

        $qualifiedModuleName = $request->getModule(false);

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

        if (!empty($moduleId) && !empty($picklistValues)) {

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

            $sequenceField = \App\Fields\RecordNumber::getInstance($moduleId);

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

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

        $this->exposeMethod('saveModuleCustomNumberingAdvanceData');

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

    public function checkPermission(App\Request $request)

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

        if (!$request->isEmpty('reset_sequence') && \in_array($request->getByType('reset_sequence'), ['Y', 'M', 'D'])) {

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

            $moduleModel->set('reset_sequence', $request->getByType('reset_sequence'));

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

            $updated = true;

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

    use \App\Controller\ExposeMethod;

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

        $moduleModel->set('sequenceNumber', $request->getByType('sequenceNumber', 'Integer'));

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

        $moduleId = \App\Module::getModuleId($sourceModule);

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

        $result = App\Fields\RecordNumber::getInstance($request->getByType('sourceModule', 2))->updateRecords();

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

        $request->getModule(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

        $response = new Vtiger_Response();

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

        $this->exposeMethod('saveModuleCustomNumberingData');

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

        $moduleModel->set('postfix', $request->getByType('postfix', 'Text'));

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

            $message = App\Language::translate('LBL_PREFIX_IN_USE', $qualifiedModuleName);

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

            $response->setResult(App\Language::translate('LBL_SUCCESSFULLY_UPDATED', $qualifiedModuleName));

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

        $result = $moduleModel->setModuleSequence();

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

     */

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

    {

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

        $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

        $moduleModel = Settings_RecordNumbering_Module_Model::getInstance($request->getByType('sourceModule', 2));

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

        $sourceModule = $request->getByType('sourceModule', 2);

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

            $response->setError(false, App\Language::translate('LBL_ERROR_WHILE_UPDATING', $sourceModule));

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

        parent::__construct();

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

        $this->exposeMethod('updateRecordsWithSequenceNumber');

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

    public function saveModuleCustomNumberingData(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

        if ($result['success']) {

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

        $picklistValues = $request->getArray('sequenceNumber', 'Integer');

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

            foreach ($picklistValues as $picklistKey => $picklistSequence) {

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

        $moduleModel->set('leading_zeros', $request->getByType('leading_zeros', 'Integer'));

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

     * @param App\Request $request

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

            $response->setResult(App\Language::translate('LBL_SUCCESSFULLY_UPDATED', $sourceModule));

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

    {

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

     * Function save module advanced numbering data.

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

        $updated = false;

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

            $response->setError($message);

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

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

        }

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

class Settings_RecordNumbering_SaveAjax_Action extends Settings_Vtiger_Index_Action

There are no issues that match your filters.

Category
Status