YetiForceCompany/YetiForceCRM

View on GitHub
modules/Products/handlers/Calculations.php

Summary

Maintainability
A
1 hr
Test Coverage
C
71%

Consider simplifying this complex logical expression.
Open

        if ($resultField && ($purchaseField = $recordModel->getField('purchase')) && $purchaseField->isActiveField()
            && ($marginField = $recordModel->getField('commissionrate')) && $marginField->isActiveField()
            && ($unitPriceField = $recordModel->getField('unit_price')) && $unitPriceField->isActiveField()) {
            $uiTypeModel = $unitPriceField->getName() === $resultField ? $purchaseField->getUITypeModel() : $unitPriceField->getUITypeModel();
            $value = $recordModel->get($uiTypeModel->getFieldModel()->getName());
Severity: Major
Found in modules/Products/handlers/Calculations.php - About 1 hr to fix

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

        public function entityBeforeSave(App\EventHandler $eventHandler)
        {
            $recordModel = $eventHandler->getRecordModel();
            $resultField = \App\Config::module($eventHandler->getModuleName(), 'CALCULATION_FIELD', 'commissionrate');
            if ($resultField && ($purchaseField = $recordModel->getField('purchase')) && $purchaseField->isActiveField()
    Severity: Minor
    Found in modules/Products/handlers/Calculations.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

    The method entityBeforeSave() has a Cyclomatic Complexity of 12. The configured cyclomatic complexity threshold is 10.
    Open

        public function entityBeforeSave(App\EventHandler $eventHandler)
        {
            $recordModel = $eventHandler->getRecordModel();
            $resultField = \App\Config::module($eventHandler->getModuleName(), 'CALCULATION_FIELD', 'commissionrate');
            if ($resultField && ($purchaseField = $recordModel->getField('purchase')) && $purchaseField->isActiveField()

    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

    Avoid using static access to class '\App\Json' in method 'getValueField'.
    Open

                $value = \App\Json::encode(['currencies' => [$currencyId => ['price' => $value]], 'currencyId' => $currencyId]);

    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\Config' in method 'entityBeforeSave'.
    Open

            $resultField = \App\Config::module($eventHandler->getModuleName(), 'CALCULATION_FIELD', 'commissionrate');

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

                } else {
                    $value = empty($unitPrice) ? 0 : ($unitPrice * 100) / ($margin + 1);
                }

    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 assigning values to variables in if clauses and the like (line '22', column '24').
    Open

        public function entityBeforeSave(App\EventHandler $eventHandler)
        {
            $recordModel = $eventHandler->getRecordModel();
            $resultField = \App\Config::module($eventHandler->getModuleName(), 'CALCULATION_FIELD', 'commissionrate');
            if ($resultField && ($purchaseField = $recordModel->getField('purchase')) && $purchaseField->isActiveField()

    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

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

            } else {
                if (null === $unitPrice) {
                    $value = empty($purchase) ? 0 : ($margin / 100) * $purchase + $purchase;
                } else {
                    $value = empty($unitPrice) ? 0 : ($unitPrice * 100) / ($margin + 1);

    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 assigning values to variables in if clauses and the like (line '23', column '8').
    Open

        public function entityBeforeSave(App\EventHandler $eventHandler)
        {
            $recordModel = $eventHandler->getRecordModel();
            $resultField = \App\Config::module($eventHandler->getModuleName(), 'CALCULATION_FIELD', 'commissionrate');
            if ($resultField && ($purchaseField = $recordModel->getField('purchase')) && $purchaseField->isActiveField()

    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 assigning values to variables in if clauses and the like (line '24', column '8').
    Open

        public function entityBeforeSave(App\EventHandler $eventHandler)
        {
            $recordModel = $eventHandler->getRecordModel();
            $resultField = \App\Config::module($eventHandler->getModuleName(), 'CALCULATION_FIELD', 'commissionrate');
            if ($resultField && ($purchaseField = $recordModel->getField('purchase')) && $purchaseField->isActiveField()

    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\Fields\Currency' in method 'entityBeforeSave'.
    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

    Call to undeclared method \Vtiger_Base_UIType::getBaseCurrency
    Open

                $currencyId = $uiTypeModel->getBaseCurrency($value) ?? \App\Fields\Currency::getDefault()['id'];
    Severity: Critical
    Found in modules/Products/handlers/Calculations.php by phan

    Call to undeclared method \Vtiger_Base_UIType::getValueForCurrency
    Open

                $value = (float) $uiTypeModel->getValueForCurrency($value, $currencyId);
    Severity: Critical
    Found in modules/Products/handlers/Calculations.php by phan

    Call to undeclared method \Vtiger_Base_UIType::getValueForCurrency
    Open

                        $purchaseValue = $purchaseField->getUITypeModel()->getValueForCurrency($recordModel->get($purchaseField->getName()) ?? '', $currencyId);
    Severity: Critical
    Found in modules/Products/handlers/Calculations.php by phan

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

    class Products_Calculations_Handler

    The class Products_Calculations_Handler is not named in CamelCase.
    Open

    class Products_Calculations_Handler
    {
        /**
         * EntityBeforeSave 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

         *

    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

         * @param float|null $unitPrice

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

        private function getValueField(int $currencyId, ?float $purchase = null, ?float $unitPrice = null, ?float $margin = null)

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

        }

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

         * EntityBeforeSave 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

                        $value = $this->getValueField($currencyId, $value, null, (float) $recordModel->get($marginField->getName()));

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

                $uiTypeModel = $unitPriceField->getName() === $resultField ? $purchaseField->getUITypeModel() : $unitPriceField->getUITypeModel();

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

                $currencyId = $uiTypeModel->getBaseCurrency($value) ?? \App\Fields\Currency::getDefault()['id'];

    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

                        break;

    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 ($resultField && ($purchaseField = $recordModel->getField('purchase')) && $purchaseField->isActiveField()

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

                && ($unitPriceField = $recordModel->getField('unit_price')) && $unitPriceField->isActiveField()) {

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

                        $value = $this->getValueField($currencyId, (float) $purchaseValue, $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

                }

    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 float|string

    Line exceeds 120 characters; contains 125 characters
    Open

        private function getValueField(int $currencyId, ?float $purchase = null, ?float $unitPrice = null, ?float $margin = null)

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

                    default:

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

                $value = $recordModel->get($uiTypeModel->getFieldModel()->getName());

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

                $value = (float) $uiTypeModel->getValueForCurrency($value, $currencyId);

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

         * @param float|null $margin

    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

            $recordModel = $eventHandler->getRecordModel();

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

                    case $marginField->getName():

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

                    case $unitPriceField->getName():

    Line exceeds 120 characters; contains 156 characters
    Open

                        $purchaseValue = $purchaseField->getUITypeModel()->getValueForCurrency($recordModel->get($purchaseField->getName()) ?? '', $currencyId);

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

                }

    Line exceeds 120 characters; contains 129 characters
    Open

                        $value = $this->getValueField($currencyId, $value, null, (float) $recordModel->get($marginField->getName()));

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

                    case $purchaseField->getName():

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

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

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

         * Calculation.

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

         * @param int        $currencyId

    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

        public function entityBeforeSave(App\EventHandler $eventHandler)

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

                switch ($resultField) {

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

                        $purchaseValue = $purchaseField->getUITypeModel()->getValueForCurrency($recordModel->get($purchaseField->getName()) ?? '', $currencyId);

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

            if (null === $margin) {

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

                $value = empty($purchase) ? 0 : round(100 * ($unitPrice - $purchase) / $purchase, 3);

    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

         */

    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

         * @param float|null $purchase

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

                if (null === $unitPrice) {

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

                    $value = empty($purchase) ? 0 : ($margin / 100) * $purchase + $purchase;

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

                $value = \App\Json::encode(['currencies' => [$currencyId => ['price' => $value]], 'currencyId' => $currencyId]);

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

            $resultField = \App\Config::module($eventHandler->getModuleName(), 'CALCULATION_FIELD', 'commissionrate');

    Line exceeds 120 characters; contains 142 characters
    Open

                $uiTypeModel = $unitPriceField->getName() === $resultField ? $purchaseField->getUITypeModel() : $unitPriceField->getUITypeModel();

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

                        $value = $this->getValueField($currencyId, null, $value, (float) $recordModel->get($marginField->getName()));

    Line exceeds 120 characters; contains 129 characters
    Open

                        $value = $this->getValueField($currencyId, null, $value, (float) $recordModel->get($marginField->getName()));

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

            }

    Line exceeds 120 characters; contains 124 characters
    Open

                $value = \App\Json::encode(['currencies' => [$currencyId => ['price' => $value]], 'currencyId' => $currencyId]);

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

        }

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

                && ($marginField = $recordModel->getField('commissionrate')) && $marginField->isActiveField()

    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

                    $value = empty($unitPrice) ? 0 : ($unitPrice * 100) / ($margin + 1);

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

    class Products_Calculations_Handler

    There are no issues that match your filters.

    Category
    Status