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) {
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) {
- Read upRead up
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 '+';
- Read upRead up
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)
- Read upRead up
- Exclude checks
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'])
- Read upRead up
- Exclude checks
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),
- Read upRead up
- Exclude checks
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();
- Read upRead up
- Exclude checks
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();
- Read upRead up
- Exclude checks
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();
- Read upRead up
- Exclude checks
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');
- Read upRead up
- Exclude checks
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,
- Read upRead up
- Exclude checks
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'])
- Read upRead up
- Exclude checks
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();
- Read upRead up
- Exclude checks
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),
- Exclude checks
Call to undeclared method \App\Db::createCommand
Open
$db->createCommand()->insert($referenceInfo['table'], [
- Exclude checks
Call to undeclared method \App\Db::quoteValue
Open
$expression .= " WHEN {$db->quoteColumnName('productid')} = {$db->quoteValue($id)} THEN (qtyinstock {$operator} {$db->quoteValue($value)})";
- Exclude checks
Call to undeclared method \App\Db::createCommand
Open
$db->createCommand()->update('vtiger_products', ['qtyinstock' => new yii\db\Expression($expression)], ['productid' => array_keys($qtyInStock)])->execute();
- Exclude checks
Call to undeclared method \App\Db\Query::select
Open
$relData = (new App\Db\Query())->select([$referenceInfo['rel'], 'qtyinstock'])
- Exclude checks
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();
- Exclude checks
Call to undeclared method \App\Db::quoteColumnName
Open
$expression .= " WHEN {$db->quoteColumnName('productid')} = {$db->quoteValue($id)} THEN (qtyinstock {$operator} {$db->quoteValue($value)})";
- Exclude checks
Call to undeclared method \App\Db::createCommand
Open
$db->createCommand()->update($referenceInfo['table'], [
- Exclude checks
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
- Exclude checks
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)
- Read upRead up
- Exclude checks
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();
- Read upRead up
- Exclude checks
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 '+';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return '+';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$qtyInStock = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return '-';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Line exceeds 120 characters; contains 147 characters Open
public static $modulesToCalculate = ['add' => ['IGRN', 'IIDN', 'ISTRN', 'IGRNC'], 'remove' => ['IGDN', 'IGIN', 'IPreOrder', 'ISTDN', 'IGDNC']];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\in_array($moduleName, self::$modulesToCalculate['remove'])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$qtyInStock[$product['name']] += $product['qty'];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ('add' == $action) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return '-';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($qtyInStock as $id => $value) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ('add' == $action) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function setQtyInStock(string $moduleName, array $data, int $storageId, string $action)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$operator = self::getOperator($moduleName, $action);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($data as $product) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!isset($qtyInStock[$product['name']])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$qtyInStock[$product['name']] = 0;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\in_array($moduleName, self::$modulesToCalculate['add'])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$expression = 'CASE ';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
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']];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getOperator($moduleName, $action)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$db = App\Db::getInstance();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
// Update qtyinstock in Products
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
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)})";
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->from($referenceInfo['table'])
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->createCommand()->queryAllByGroup(0);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
], [$referenceInfo['base'] => $storageId, $referenceInfo['rel'] => $id])->execute();
- Exclude checks
Line exceeds 120 characters; contains 163 characters Open
$db->createCommand()->update('vtiger_products', ['qtyinstock' => new yii\db\Expression($expression)], ['productid' => array_keys($qtyInStock)])->execute();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($qtyInStock as $id => $value) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$db->createCommand()->insert($referenceInfo['table'], [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$referenceInfo['rel'] => $id,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$eventHandler->setParams([
- Exclude checks
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();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->where([$referenceInfo['base'] => $storageId, $referenceInfo['rel'] => array_keys($qtyInStock)])
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'qtyinstock' => '+' === $operator ? $value : $operator . $value,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$expression .= ' END';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
// Saving the amount of product in stock.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
])->execute();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$referenceInfo['base'] => $storageId,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'qtyinstock' => new yii\db\Expression('qtyinstock ' . $operator . ' ' . $value),
- Exclude checks
Line exceeds 120 characters; contains 152 characters Open
$expression .= " WHEN {$db->quoteColumnName('productid')} = {$db->quoteValue($id)} THEN (qtyinstock {$operator} {$db->quoteValue($value)})";
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$eventHandler->setModuleName('IStorages');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
]);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$referenceInfo = Vtiger_Relation_Model::getReferenceTableInfo('Products', 'IStorages');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'storageId' => $storageId,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$eventHandler->trigger('IStoragesAfterUpdateStock');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$relData = (new App\Db\Query())->select([$referenceInfo['rel'], 'qtyinstock'])
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\array_key_exists($id, $relData)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$db->createCommand()->update($referenceInfo['table'], [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} else {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$eventHandler = new App\EventHandler();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'products' => $qtyInStock,
- Exclude checks
Class name "IStorages_Module_Model" is not in camel caps format Open
class IStorages_Module_Model extends Vtiger_Module_Model
- Exclude checks