YetiForceCompany/YetiForceCRM

View on GitHub
api/webservice/WebserviceStandard/Fields.php

Summary

Maintainability
A
1 hr
Test Coverage
F
45%

Function loadWebserviceByField has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
Open

    public static function loadWebserviceByField(\Vtiger_Field_Model $fieldModel, \Api\Core\BaseAction $actionModel, ?array $fieldData = null): void
    {
        if (null === $fieldData) {
            $fieldData = self::getFields($actionModel->controller->app['id'], $fieldModel->getModuleId())[$fieldModel->getName()] ?? [];
        }
Severity: Minor
Found in api/webservice/WebserviceStandard/Fields.php - About 35 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

Function getDefaultValue has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
Open

    private static function getDefaultValue(\Vtiger_Field_Model $fieldModel, array $fieldData, \Api\Core\BaseAction $actionModel)
    {
        $value = $fieldData['default_value'];
        $list = \App\Field::getCustomListForDefaultValue($fieldModel);
        if (isset($list[$value])) {
Severity: Minor
Found in api/webservice/WebserviceStandard/Fields.php - About 35 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 '81', column '61').
Open

        self::$webserviceAppsFields[$cacheKey] = $response = (new \App\Db\Query())->select(['vtiger_field.fieldname', 'w_#__fields_server.*'])

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\Field' in method 'getDefaultValue'.
Open

        $list = \App\Field::getCustomListForDefaultValue($fieldModel);

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\Db' in method 'getFields'.
Open

            ->all(\App\Db::getInstance('webservice')) ?: [];

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\Cache' in method 'getFields'.
Open

            return \App\Cache::get('WebserviceAppsFields', $cacheKey);

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\Record' in method 'getDefaultValue'.
Open

                        $value = \App\Record::getParentRecord($actionModel->getUserCrmId());

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\Cache' in method 'getFields'.
Open

        \App\Cache::save('WebserviceAppsFields', $cacheKey, $response);

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\Record' in method 'getDefaultValue'.
Open

                    $value = \App\Record::getParentRecord($actionModel->getUserCrmId());

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\Cache' in method 'getFields'.
Open

        if (\App\Cache::has('WebserviceAppsFields', $cacheKey)) {

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

        if (\App\Cache::has('WebserviceAppsFields', $cacheKey)) {

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.

Add a "case default" clause to this "switch" statement.
Open

            switch ($value) {

The requirement for a final case default clause is defensive programming. The clause should either take appropriate action, or contain a suitable comment as to why no action is taken. Even when the switch covers all current values of an enum, a default case should still be used because there is no guarantee that the enum won't be extended.

Noncompliant Code Example

switch ($param) {  //missing default clause
  case 0:
    do_something();
    break;
  case 1:
    do_something_else();
    break;
}

switch ($param) {
  default: // default clause should be the last one
    error();
    break;
  case 0:
    do_something();
    break;
  case 1:
    do_something_else();
    break;
}

Compliant Solution

switch ($param) {
  case 0:
    do_something();
    break;
  case 1:
    do_something_else();
    break;
  default:
    error();
    break;
}

See

  • MISRA C:2004, 15.0 - The MISRA C switch syntax shall be used.
  • MISRA C:2004, 15.3 - The final clause of a switch statement shall be the default clause
  • MISRA C++:2008, 6-4-3 - A switch statement shall be a well-formed switch statement.
  • MISRA C++:2008, 6-4-6 - The final clause of a switch statement shall be the default-clause
  • MISRA C:2012, 16.1 - All switch statements shall be well-formed
  • MISRA C:2012, 16.4 - Every switch statement shall have a default label
  • MISRA C:2012, 16.5 - A default label shall appear as either the first or the last switch label of a switch statement
  • MITRE, CWE-478 - Missing Default Case in Switch Statement
  • CERT, MSC01-C. - Strive for logical completeness
  • CERT, MSC01-CPP. - Strive for logical completeness

Call to undeclared method \App\Db\Query::select
Open

        self::$webserviceAppsFields[$cacheKey] = $response = (new \App\Db\Query())->select(['vtiger_field.fieldname', 'w_#__fields_server.*'])

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

     * @param \Api\Core\BaseAction $actionModel

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

        }

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

                case 'accountOnContact':

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

    public static function loadWebserviceByField(\Vtiger_Field_Model $fieldModel, \Api\Core\BaseAction $actionModel, ?array $fieldData = null): void

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

                $fieldModel->set('defaultvalue', self::getDefaultValue($fieldModel, $fieldData, $actionModel));

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

                $fieldModel->set('displaytype', $fieldData['visibility']);

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

    private static function getDefaultValue(\Vtiger_Field_Model $fieldModel, array $fieldData, \Api\Core\BaseAction $actionModel)

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

                    break;

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

     * Load custom field data for the webservice app.

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

                case 'accountLoggedContact':

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

     *

Line exceeds 120 characters; contains 136 characters
Open

            $fieldData = self::getFields($actionModel->controller->app['id'], $fieldModel->getModuleId())[$fieldModel->getName()] ?? [];

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

            }

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

    /**

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

     * @param \Vtiger_Field_Model  $fieldModel

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

                    break;

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

    private static $webserviceAppsFields = [];

Line exceeds 120 characters; contains 123 characters
Open

        foreach (self::getFields($actionModel->controller->app['id'], $moduleModel->getId()) as $fieldName => $fieldData) {

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

     *

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

     * @param \Vtiger_Field_Model  $fieldModel

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

     * @return void

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

            $fieldData = self::getFields($actionModel->controller->app['id'], $fieldModel->getModuleId())[$fieldModel->getName()] ?? [];

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

        if ($fieldData) {

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

            if (1 !== $actionModel->getUserData('type') && !empty($fieldData['is_default'])) {

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

     * Get fields for current webservice app.

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('w_#__fields_server')

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

     * Get default value for specified field object.

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

    public static function loadWebserviceFields(\Vtiger_Module_Model $moduleModel, \Api\Core\BaseAction $actionModel): void

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

     *

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

        $cacheKey = "{$serverId}_{$moduleId}";

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

        if (\App\Cache::has('WebserviceAppsFields', $cacheKey)) {

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

            return \App\Cache::get('WebserviceAppsFields', $cacheKey);

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

     * Load custom fields data for the webservice app.

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

     * @param int $serverId

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 (isset(self::$webserviceAppsFields[$cacheKey])) {

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

     * @param array                $fieldData

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

    {

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

                    $value = $actionModel->getUserData('crmid');

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

            switch ($value) {

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

                case 'loggedContact':

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

    /**

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

     *

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

     * @param array|null           $fieldData

Line exceeds 120 characters; contains 148 characters
Open

    public static function loadWebserviceByField(\Vtiger_Field_Model $fieldModel, \Api\Core\BaseAction $actionModel, ?array $fieldData = null): void

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

            }

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

        self::$webserviceAppsFields[$cacheKey] = $response = (new \App\Db\Query())->select(['vtiger_field.fieldname', 'w_#__fields_server.*'])

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

     * @return void

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

            return self::$webserviceAppsFields[$cacheKey];

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

        $value = $fieldData['default_value'];

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

        $list = \App\Field::getCustomListForDefaultValue($fieldModel);

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

            ->innerJoin('vtiger_field', 'w_#__fields_server.fieldid = vtiger_field.fieldid')

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

        \App\Cache::save('WebserviceAppsFields', $cacheKey, $response);

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

     * @param \Api\Core\BaseAction $actionModel

Line exceeds 120 characters; contains 123 characters
Open

    public static function loadWebserviceFields(\Vtiger_Module_Model $moduleModel, \Api\Core\BaseAction $actionModel): void

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 (!empty($fieldData['visibility'])) {

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

        }

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

            ->indexBy('fieldname')

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

        return $response;

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 mixed

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

        $fields = $moduleModel->getFields();

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

                self::loadWebserviceByField($fields[$fieldName], $actionModel, $fieldData);

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

     *

Line exceeds 120 characters; contains 142 characters
Open

        self::$webserviceAppsFields[$cacheKey] = $response = (new \App\Db\Query())->select(['vtiger_field.fieldname', 'w_#__fields_server.*'])

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 (isset($list[$value])) {

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

                    break;

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

                    $value = $actionModel->controller->request->getHeader('x-parent-id');

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

        }

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

    /** @var array Webservice apps fields data. */

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

        foreach (self::getFields($actionModel->controller->app['id'], $moduleModel->getId()) as $fieldName => $fieldData) {

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 (null === $fieldData) {

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

     * @return array

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

    public static function getFields(int $serverId, int $moduleId): array

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

        return $value;

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

            }

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

                        $value = \App\Record::getParentRecord($actionModel->getUserCrmId());

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

            ->where(['w_#__fields_server.serverid' => $serverId, 'vtiger_field.tabid' => $moduleId])

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

     * @param \Api\Core\BaseAction $actionModel

Line exceeds 120 characters; contains 129 characters
Open

    private static function getDefaultValue(\Vtiger_Field_Model $fieldModel, array $fieldData, \Api\Core\BaseAction $actionModel)

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

                    $value = \App\Record::getParentRecord($actionModel->getUserCrmId());

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

     * @param \Vtiger_Module_Model $moduleModel

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

            if (isset($fields[$fieldName])) {

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

     * @param int $moduleId

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

            ->all(\App\Db::getInstance('webservice')) ?: [];

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

                    if (!$value) {

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

    }

There are no issues that match your filters.

Category
Status