YetiForceCompany/YetiForceCRM

View on GitHub
modules/IStorages/models/Module.php

Summary

Maintainability
A
2 hrs
Test Coverage
B
80%

Method setQtyInStock has 40 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public static function setQtyInStock(string $moduleName, array $data, int $storageId, string $action)
    {
        $db = App\Db::getInstance();
        $qtyInStock = [];
        foreach ($data as $product) {
Severity: Minor
Found in modules/IStorages/models/Module.php - About 1 hr to fix

    Function setQtyInStock has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
    Open

        public static function setQtyInStock(string $moduleName, array $data, int $storageId, string $action)
        {
            $db = App\Db::getInstance();
            $qtyInStock = [];
            foreach ($data as $product) {
    Severity: Minor
    Found in modules/IStorages/models/Module.php - About 45 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 getOperator has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
    Open

        public static function getOperator($moduleName, $action)
        {
            if (\in_array($moduleName, self::$modulesToCalculate['add'])) {
                if ('add' == $action) {
                    return '+';
    Severity: Minor
    Found in modules/IStorages/models/Module.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

    Reduce the number of returns of this function 4, down to the maximum allowed 3.
    Open

        public static function getOperator($moduleName, $action)

    Having too many return statements in a function increases the function's essential complexity because the flow of execution is broken each time a return statement is encountered. This makes it harder to read and understand the logic of the function.

    Noncompliant Code Example

    With the default threshold of 3:

    function myFunction(){ // Noncompliant as there are 4 return statements
      if (condition1) {
        return true;
      } else {
        if (condition2) {
          return false;
        } else {
          return true;
        }
      }
      return false;
    }
    

    Missing class import via use statement (line '50', column '19').
    Open

            $relData = (new App\Db\Query())->select([$referenceInfo['rel'], 'qtyinstock'])
    Severity: Minor
    Found in modules/IStorages/models/Module.php by phpmd

    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 '57', column '26').
    Open

                        'qtyinstock' => new yii\db\Expression('qtyinstock ' . $operator . ' ' . $value),
    Severity: Minor
    Found in modules/IStorages/models/Module.php by phpmd

    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 '67', column '23').
    Open

            $eventHandler = new App\EventHandler();
    Severity: Minor
    Found in modules/IStorages/models/Module.php by phpmd

    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 '47', column '72').
    Open

            $db->createCommand()->update('vtiger_products', ['qtyinstock' => new yii\db\Expression($expression)], ['productid' => array_keys($qtyInStock)])->execute();
    Severity: Minor
    Found in modules/IStorages/models/Module.php by phpmd

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

            $db = App\Db::getInstance();
    Severity: Minor
    Found in modules/IStorages/models/Module.php by phpmd

    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_Relation_Model' in method 'setQtyInStock'.
    Open

            $referenceInfo = Vtiger_Relation_Model::getReferenceTableInfo('Products', 'IStorages');
    Severity: Minor
    Found in modules/IStorages/models/Module.php by phpmd

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

                } else {
                    $db->createCommand()->insert($referenceInfo['table'], [
                        $referenceInfo['base'] => $storageId,
                        $referenceInfo['rel'] => $id,
                        'qtyinstock' => '+' === $operator ? $value : $operator . $value,
    Severity: Minor
    Found in modules/IStorages/models/Module.php by phpmd

    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

    Define a constant instead of duplicating this literal "table" 3 times.
    Open

                ->from($referenceInfo['table'])
    Severity: Critical
    Found in modules/IStorages/models/Module.php by sonar-php

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

            $db->createCommand()->update('vtiger_products', ['qtyinstock' => new yii\db\Expression($expression)], ['productid' => array_keys($qtyInStock)])->execute();
    Severity: Critical
    Found in modules/IStorages/models/Module.php by sonar-php

    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 __construct from undeclared class \yii\db\Expression
    Open

                        'qtyinstock' => new yii\db\Expression('qtyinstock ' . $operator . ' ' . $value),
    Severity: Critical
    Found in modules/IStorages/models/Module.php by phan

    Call to undeclared method \App\Db::createCommand
    Open

                    $db->createCommand()->insert($referenceInfo['table'], [
    Severity: Critical
    Found in modules/IStorages/models/Module.php by phan

    Call to undeclared method \App\Db::quoteValue
    Open

                $expression .= " WHEN {$db->quoteColumnName('productid')} = {$db->quoteValue($id)} THEN (qtyinstock {$operator} {$db->quoteValue($value)})";
    Severity: Critical
    Found in modules/IStorages/models/Module.php by phan

    Call to undeclared method \App\Db::createCommand
    Open

            $db->createCommand()->update('vtiger_products', ['qtyinstock' => new yii\db\Expression($expression)], ['productid' => array_keys($qtyInStock)])->execute();
    Severity: Critical
    Found in modules/IStorages/models/Module.php by phan

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

            $relData = (new App\Db\Query())->select([$referenceInfo['rel'], 'qtyinstock'])
    Severity: Critical
    Found in modules/IStorages/models/Module.php by phan

    Call to method __construct from undeclared class \yii\db\Expression
    Open

            $db->createCommand()->update('vtiger_products', ['qtyinstock' => new yii\db\Expression($expression)], ['productid' => array_keys($qtyInStock)])->execute();
    Severity: Critical
    Found in modules/IStorages/models/Module.php by phan

    Call to undeclared method \App\Db::quoteColumnName
    Open

                $expression .= " WHEN {$db->quoteColumnName('productid')} = {$db->quoteValue($id)} THEN (qtyinstock {$operator} {$db->quoteValue($value)})";
    Severity: Critical
    Found in modules/IStorages/models/Module.php by phan

    Call to undeclared method \App\Db::createCommand
    Open

                    $db->createCommand()->update($referenceInfo['table'], [
    Severity: Critical
    Found in modules/IStorages/models/Module.php by phan

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

    class IStorages_Module_Model extends Vtiger_Module_Model

    The class IStorages_Module_Model is not named in CamelCase.
    Open

    class IStorages_Module_Model extends Vtiger_Module_Model
    {
        public static $modulesToCalculate = ['add' => ['IGRN', 'IIDN', 'ISTRN', 'IGRNC'], 'remove' => ['IGDN', 'IGIN', 'IPreOrder', 'ISTDN', 'IGDNC']];
    
        public static function getOperator($moduleName, $action)
    Severity: Minor
    Found in modules/IStorages/models/Module.php by phpmd

    CamelCaseClassName

    Since: 0.2

    It is considered best practice to use the CamelCase notation to name classes.

    Example

    class class_name {
    }

    Source

    Avoid variables with short names like $db. Configured minimum length is 3.
    Open

            $db = App\Db::getInstance();
    Severity: Minor
    Found in modules/IStorages/models/Module.php by phpmd

    ShortVariable

    Since: 0.2

    Detects when a field, local, or parameter has a very short name.

    Example

    class Something {
        private $q = 15; // VIOLATION - Field
        public static function main( array $as ) { // VIOLATION - Formal
            $r = 20 + $this->q; // VIOLATION - Local
            for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
                $r += $this->q;
            }
        }
    }

    Source https://phpmd.org/rules/naming.html#shortvariable

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

                    return '+';

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

                return '+';

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

            $qtyInStock = [];

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

                    return '-';

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

            }

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

            }

    Line exceeds 120 characters; contains 147 characters
    Open

        public static $modulesToCalculate = ['add' => ['IGRN', 'IIDN', 'ISTRN', 'IGRNC'], 'remove' => ['IGDN', 'IGIN', 'IPreOrder', 'ISTDN', 'IGDNC']];

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

            if (\in_array($moduleName, self::$modulesToCalculate['remove'])) {

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

                $qtyInStock[$product['name']] += $product['qty'];

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

                if ('add' == $action) {

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

                return '-';

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

            foreach ($qtyInStock as $id => $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

                if ('add' == $action) {

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

        public static function setQtyInStock(string $moduleName, array $data, int $storageId, string $action)

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

        }

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

            $operator = self::getOperator($moduleName, $action);

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

            foreach ($data as $product) {

    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 (!isset($qtyInStock[$product['name']])) {

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

                    $qtyInStock[$product['name']] = 0;

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

            if (\in_array($moduleName, self::$modulesToCalculate['add'])) {

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

            $expression = 'CASE ';

    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 $modulesToCalculate = ['add' => ['IGRN', 'IIDN', 'ISTRN', 'IGRNC'], 'remove' => ['IGDN', 'IGIN', 'IPreOrder', 'ISTDN', 'IGDNC']];

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

        public static function getOperator($moduleName, $action)

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

            $db = App\Db::getInstance();

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

            // Update qtyinstock in Products

    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

                $expression .= " WHEN {$db->quoteColumnName('productid')} = {$db->quoteValue($id)} THEN (qtyinstock {$operator} {$db->quoteValue($value)})";

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

                ->from($referenceInfo['table'])

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

                ->createCommand()->queryAllByGroup(0);

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

                    ], [$referenceInfo['base'] => $storageId, $referenceInfo['rel'] => $id])->execute();

    Line exceeds 120 characters; contains 163 characters
    Open

            $db->createCommand()->update('vtiger_products', ['qtyinstock' => new yii\db\Expression($expression)], ['productid' => array_keys($qtyInStock)])->execute();

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

            foreach ($qtyInStock as $id => $value) {

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

                    $db->createCommand()->insert($referenceInfo['table'], [

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

                        $referenceInfo['rel'] => $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

            $eventHandler->setParams([

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

            $db->createCommand()->update('vtiger_products', ['qtyinstock' => new yii\db\Expression($expression)], ['productid' => array_keys($qtyInStock)])->execute();

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

                ->where([$referenceInfo['base'] => $storageId, $referenceInfo['rel'] => array_keys($qtyInStock)])

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

                        'qtyinstock' => '+' === $operator ? $value : $operator . $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

            $expression .= ' END';

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

            // Saving the amount of product in stock.

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

                    ])->execute();

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

                        $referenceInfo['base'] => $storageId,

    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

                        'qtyinstock' => new yii\db\Expression('qtyinstock ' . $operator . ' ' . $value),

    Line exceeds 120 characters; contains 152 characters
    Open

                $expression .= " WHEN {$db->quoteColumnName('productid')} = {$db->quoteValue($id)} THEN (qtyinstock {$operator} {$db->quoteValue($value)})";

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

            $eventHandler->setModuleName('IStorages');

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

            ]);

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

            $referenceInfo = Vtiger_Relation_Model::getReferenceTableInfo('Products', 'IStorages');

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

                'storageId' => $storageId,

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

            $eventHandler->trigger('IStoragesAfterUpdateStock');

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

            $relData = (new App\Db\Query())->select([$referenceInfo['rel'], 'qtyinstock'])

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

                if (\array_key_exists($id, $relData)) {

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

                    $db->createCommand()->update($referenceInfo['table'], [

    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

            $eventHandler = new App\EventHandler();

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

                'products' => $qtyInStock,

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

    class IStorages_Module_Model extends Vtiger_Module_Model

    There are no issues that match your filters.

    Category
    Status