YetiForceCompany/YetiForceCRM

View on GitHub
modules/Vtiger/handlers/FieldsDependency.php

Summary

Maintainability
A
55 mins
Test Coverage
F
0%

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

    public function editViewPreSave(App\EventHandler $eventHandler)
    {
        $recordModel = $eventHandler->getRecordModel();
        $response = ['result' => true];
        $fieldsDependency = \App\FieldsDependency::getByRecordModel(\App\Request::_getByType('fromView'), $recordModel);
Severity: Minor
Found in modules/Vtiger/handlers/FieldsDependency.php - About 55 mins to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Avoid assigning values to variables in if clauses and the like (line '48', column '50').
Open

    public function editViewPreSave(App\EventHandler $eventHandler)
    {
        $recordModel = $eventHandler->getRecordModel();
        $response = ['result' => true];
        $fieldsDependency = \App\FieldsDependency::getByRecordModel(\App\Request::_getByType('fromView'), $recordModel);

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\FieldsDependency' in method 'editViewPreSave'.
Open

        $fieldsDependency = \App\FieldsDependency::getByRecordModel(\App\Request::_getByType('fromView'), $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\FieldsDependency' in method 'editViewChangeValue'.
Open

        $fieldsDependency = \App\FieldsDependency::getByRecordModel(\App\Request::_getByType('fromView'), $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 'Vtiger_Record_Model' in method 'vars'.
Open

                $recordModel = Vtiger_Record_Model::getCleanInstance($moduleName);

StaticAccess

Since: 1.4.0

Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

Example

class Foo
{
    public function bar()
    {
        Bar::baz();
    }
}

Source https://phpmd.org/rules/cleancode.html#staticaccess

Avoid using static access to class '\App\FieldsDependency' in method 'vars'.
Open

}

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\Request' in method 'editViewChangeValue'.
Open

        $fieldsDependency = \App\FieldsDependency::getByRecordModel(\App\Request::_getByType('fromView'), $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\Language' in method 'editViewPreSave'.
Open

                    'message' => \App\Language::translate('LBL_NOT_FILLED_MANDATORY_FIELDS') . ': <br /> - ' . implode('<br /> - ', $mandatoryFields),

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\Request' in method 'editViewPreSave'.
Open

        $fieldsDependency = \App\FieldsDependency::getByRecordModel(\App\Request::_getByType('fromView'), $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 "mandatory" 3 times.
Open

        if ($fieldsDependency['show']['mandatory']) {

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

        if ($fieldsDependency['show']['frontend']) {

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.

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

class Vtiger_FieldsDependency_Handler

The class Vtiger_FieldsDependency_Handler is not named in CamelCase.
Open

class Vtiger_FieldsDependency_Handler
{
    /**
     * EditViewChangeValue handler function.
     *

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 for alignment; tabs are not allowed
Open

    {

Spaces must be used for alignment; tabs are not allowed
Open

                }

Spaces must be used for alignment; tabs are not allowed
Open

            }

Spaces must be used for alignment; tabs are not allowed
Open

     *

Spaces must be used for alignment; tabs are not allowed
Open

        if ($fieldsDependency['show']['mandatory']) {

Spaces must be used for alignment; tabs are not allowed
Open

            $mandatoryFields = [];

Spaces must be used for alignment; tabs are not allowed
Open

                    'result' => false,

Spaces must be used for alignment; tabs are not allowed
Open

                    'message' => \App\Language::translate('LBL_NOT_FILLED_MANDATORY_FIELDS') . ': <br /> - ' . implode('<br /> - ', $mandatoryFields),

Spaces must be used for alignment; tabs are not allowed
Open

    }

Spaces must be used for alignment; tabs are not allowed
Open

            if (empty($recordModel)) {

Spaces must be used for alignment; tabs are not allowed
Open

        }

Spaces must be used for alignment; tabs are not allowed
Open

        return null;

Spaces must be used for alignment; tabs are not allowed
Open

    /**

Spaces must be used for alignment; tabs are not allowed
Open

                    $mandatoryFields[] = $fieldModel->getFullLabelTranslation();

Spaces must be used for alignment; tabs are not allowed
Open

     * @param string $moduleName

Spaces must be used for alignment; tabs are not allowed
Open

 * @license        YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)

Spaces must be used for alignment; tabs are not allowed
Open

 * @author        Mariusz Krzaczkowski <m.krzaczkowski@yetiforce.com>

Spaces must be used for alignment; tabs are not allowed
Open

            $return['showFields'] = $fieldsDependency['show']['frontend'];

Spaces must be used for alignment; tabs are not allowed
Open

        }

Spaces must be used for alignment; tabs are not allowed
Open

            }

Spaces must be used for alignment; tabs are not allowed
Open

        return $response;

Spaces must be used for alignment; tabs are not allowed
Open

     * @return array|null

Spaces must be used for alignment; tabs are not allowed
Open

                $recordModel = Vtiger_Record_Model::getCleanInstance($moduleName);

Spaces must be used for alignment; tabs are not allowed
Open

        }

Spaces must be used for alignment; tabs are not allowed
Open

            $return['hideFields'] = $fieldsDependency['hide']['frontend'];

Spaces must be used for alignment; tabs are not allowed
Open

        $fieldsDependency = \App\FieldsDependency::getByRecordModel(\App\Request::_getByType('fromView'), $recordModel);

Spaces must be used for alignment; tabs are not allowed
Open

            return \App\FieldsDependency::getByRecordModel($view, $recordModel)['conditionsFields'];

Spaces must be used for alignment; tabs are not allowed
Open

 * @copyright    YetiForce S.A.

Spaces must be used for alignment; tabs are not allowed
Open

     * EditViewChangeValue handler function.

Spaces must be used for alignment; tabs are not allowed
Open

     * @param App\EventHandler $eventHandler

Spaces must be used for alignment; tabs are not allowed
Open

        if ($fieldsDependency['hide']['frontend']) {

Spaces must be used for alignment; tabs are not allowed
Open

    }

Spaces must be used for alignment; tabs are not allowed
Open

    {

Spaces must be used for alignment; tabs are not allowed
Open

    /**

Spaces must be used for alignment; tabs are not allowed
Open

     *

Spaces must be used for alignment; tabs are not allowed
Open

                $response = [

Spaces must be used for alignment; tabs are not allowed
Open

                    'hoverField' => reset($fieldsDependency['show']['mandatory']),

Spaces must be used for alignment; tabs are not allowed
Open

                ];

Spaces must be used for alignment; tabs are not allowed
Open

        }

Spaces must be used for alignment; tabs are not allowed
Open

     */

Spaces must be used for alignment; tabs are not allowed
Open

        if (\App\EventHandler::EDIT_VIEW_CHANGE_VALUE === $name) {

Spaces must be used for alignment; tabs are not allowed
Open

     */

Spaces must be used for alignment; tabs are not allowed
Open

        $return = [];

Spaces must be used for alignment; tabs are not allowed
Open

        return $return;

Spaces must be used for alignment; tabs are not allowed
Open

    public function editViewPreSave(App\EventHandler $eventHandler)

Spaces must be used for alignment; tabs are not allowed
Open

            }

Spaces must be used for alignment; tabs are not allowed
Open

     * @param array  $params

Spaces must be used for alignment; tabs are not allowed
Open

        $fieldsDependency = \App\FieldsDependency::getByRecordModel(\App\Request::_getByType('fromView'), $recordModel);

Spaces must be used for alignment; tabs are not allowed
Open

        $recordModel = $eventHandler->getRecordModel();

Spaces must be used for alignment; tabs are not allowed
Open

    {

Spaces must be used for alignment; tabs are not allowed
Open

     * @param App\EventHandler $eventHandler

Spaces must be used for alignment; tabs are not allowed
Open

     */

Spaces must be used for alignment; tabs are not allowed
Open

        $response = ['result' => true];

Spaces must be used for alignment; tabs are not allowed
Open

    }

Spaces must be used for alignment; tabs are not allowed
Open

    public function editViewChangeValue(App\EventHandler $eventHandler)

Spaces must be used for alignment; tabs are not allowed
Open

        $recordModel = $eventHandler->getRecordModel();

Spaces must be used for alignment; tabs are not allowed
Open

     * @param string $name

Spaces must be used for alignment; tabs are not allowed
Open

        if ($fieldsDependency['show']['frontend']) {

Spaces must be used for alignment; tabs are not allowed
Open

     *

Spaces must be used for alignment; tabs are not allowed
Open

    public function vars(string $name, array $params, string $moduleName): ?array

Spaces must be used for alignment; tabs are not allowed
Open

     * EditViewPreSave handler function.

Spaces must be used for alignment; tabs are not allowed
Open

                if ('' === $recordModel->get($fieldName) && ($fieldModel = $recordModel->getField($fieldName)) && $fieldModel->isActiveField()) {

Spaces must be used for alignment; tabs are not allowed
Open

            if ($mandatoryFields) {

Spaces must be used for alignment; tabs are not allowed
Open

            foreach ($fieldsDependency['show']['mandatory'] as $fieldName) {

Spaces must be used for alignment; tabs are not allowed
Open

     *

Spaces must be used for alignment; tabs are not allowed
Open

 * @package        Handler

Spaces must be used for alignment; tabs are not allowed
Open

    /**

Spaces must be used for alignment; tabs are not allowed
Open

     * Get variables for the current event.

Spaces must be used for alignment; tabs are not allowed
Open

            [$recordModel,$view] = $params;

Line exceeds 120 characters; contains 145 characters
Open

                if ('' === $recordModel->get($fieldName) && ($fieldModel = $recordModel->getField($fieldName)) && $fieldModel->isActiveField()) {

Line exceeds 120 characters; contains 150 characters
Open

                    'message' => \App\Language::translate('LBL_NOT_FILLED_MANDATORY_FIELDS') . ': <br /> - ' . implode('<br /> - ', $mandatoryFields),

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

class Vtiger_FieldsDependency_Handler

There are no issues that match your filters.

Category
Status