YetiForceCompany/YetiForceCRM

View on GitHub
modules/FInvoice/handlers/CheckingQuantityAvailableProduct.php

Summary

Maintainability
A
1 hr
Test Coverage
F
0%

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

    public function editViewPreSave(App\EventHandler $eventHandler)
    {
        $recordModel = $eventHandler->getRecordModel();
        $response = ['result' => true];
        $dataQty = $productsName = [];
Severity: Minor
Found in modules/FInvoice/handlers/CheckingQuantityAvailableProduct.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

Avoid using static access to class 'App\Language' in method 'editViewPreSave'.
Open

                $response = ['result' => false, 'message' => App\Language::translateArgs('LBL_AVAILABLE_QUANTITY_PRODUCT', $recordModel->getModuleName(), implode(' ', $productsName))];

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

            $productRecordModel = Vtiger_Record_Model::getInstanceById($value['name'], 'Products');

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

                $dataQty[$productRecordModel->getId()]['qtyInventory'] = ($dataQty[$productRecordModel->getId()]['qtyInventory'] ?? 0) + $value['qty'];

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.

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

class FInvoice_CheckingQuantityAvailableProduct_Handler
{
    /**
     * EditViewPreSave handler function.
     *

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 FInvoice_CheckingQuantityAvailableProduct_Handler

The class FInvoice_CheckingQuantityAvailableProduct_Handler is not named in CamelCase.
Open

class FInvoice_CheckingQuantityAvailableProduct_Handler
{
    /**
     * EditViewPreSave 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 to indent lines; tabs are not allowed
Open

     * EditViewPreSave handler function.

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($dataQty)) {

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

     *

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

        $dataQty = $productsName = [];

Line exceeds 120 characters; contains 151 characters
Open

                $dataQty[$productRecordModel->getId()]['qtyInventory'] = ($dataQty[$productRecordModel->getId()]['qtyInventory'] ?? 0) + $value['qty'];

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 $response;

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

        $recordModel = $eventHandler->getRecordModel();

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

            if ($productRecordModel->isEditable() && $productRecordModel->getModuleName() === 'Products' && $productRecordModel->getField('qtyinstock')->isActiveField()) {

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

            }

Line exceeds 120 characters; contains 184 characters
Open

                $response = ['result' => false, 'message' => App\Language::translateArgs('LBL_AVAILABLE_QUANTITY_PRODUCT', $recordModel->getModuleName(), implode(' ', $productsName))];

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 editViewPreSave(App\EventHandler $eventHandler)

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

                $dataQty[$productRecordModel->getId()]['qtyRecord'] = $productRecordModel->get('qtyinstock');

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

            if (!empty($productsName)) {

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

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

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

            $productRecordModel = Vtiger_Record_Model::getInstanceById($value['name'], 'Products');

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

                    $productsName[] = $value['productName'];

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

            foreach ($dataQty as $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

     * @param App\EventHandler $eventHandler

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 ((float) $value['qtyInventory'] > (float) $value['qtyRecord']) {

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

                $dataQty[$productRecordModel->getId()]['qtyInventory'] = ($dataQty[$productRecordModel->getId()]['qtyInventory'] ?? 0) + $value['qty'];

Line exceeds 120 characters; contains 171 characters
Open

            if ($productRecordModel->isEditable() && $productRecordModel->getModuleName() === 'Products' && $productRecordModel->getField('qtyinstock')->isActiveField()) {

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

                $dataQty[$productRecordModel->getId()]['productName'] = "<br />{$productRecordModel->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

                $response = ['result' => false, 'message' => App\Language::translateArgs('LBL_AVAILABLE_QUANTITY_PRODUCT', $recordModel->getModuleName(), implode(' ', $productsName))];

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

        foreach ($recordModel->getInventoryData() as $value) {

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

class FInvoice_CheckingQuantityAvailableProduct_Handler

There are no issues that match your filters.

Category
Status