Function isAddCustomFieldEnabled
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public function isAddCustomFieldEnabled()
{
$actionNotSupportedModules = ['calendar', 'events', 'faq', 'helpdesk'];
$blocksEliminatedArray = ['calendar' => ['LBL_TASK_INFORMATION', 'LBL_DESCRIPTION_INFORMATION', 'LBL_RELATED_TO', 'LBL_REMINDER_INFORMATION', 'LBL_RECURRENCE_INFORMATION'],
'helpdesk' => ['LBL_TICKET_RESOLUTION', 'LBL_COMMENTS'],
- 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
Missing class import via use statement (line '59', column '22'). Open
'sequence' => new yii\db\Expression($caseSequence),
- 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
The method getInstance has a boolean flag argument $moduleInstance, which is a certain sign of a Single Responsibility Principle violation. Open
public static function getInstance($value, $moduleInstance = false)
- Read upRead up
- Exclude checks
BooleanArgumentFlag
Since: 1.4.0
A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.
Example
class Foo {
public function bar($flag = true) {
}
}
Source https://phpmd.org/rules/cleancode.html#booleanargumentflag
Missing class import via use statement (line '60', column '19'). Open
'block' => new yii\db\Expression($caseBlock),
- 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
The method isAddCustomFieldEnabled uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
return false;
}
- 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
Avoid using static access to class 'App\Db' in method 'updateFieldSequenceNumber'. 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
Define a constant instead of duplicating this literal "fieldid" 3 times. Open
$fieldId = $newFieldSequence['fieldid'];
- 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
'block' => new yii\db\Expression($caseBlock),
- Exclude checks
Call to method __construct
from undeclared class \yii\db\Expression
Open
'sequence' => new yii\db\Expression($caseSequence),
- Exclude checks
Call to undeclared method \App\Db::quoteValue
Open
$caseSequence .= " WHEN fieldid = {$db->quoteValue($fieldId)} THEN {$db->quoteValue($newFieldSequence['sequence'])}";
- Exclude checks
Saw unextractable annotation for comment '* @param <type> $moduleModel - module instance'</type>
Open
* @param <type> $moduleModel - module instance
- Exclude checks
Saw unextractable annotation for comment '* @return <array> - list of Vtiger_Block_Model'</array>
Open
* @return <array> - list of Vtiger_Block_Model
- Exclude checks
Call to undeclared method \App\Db::createCommand
Open
$db->createCommand()->update('vtiger_field', [
- Exclude checks
Call to undeclared method \App\Db::quoteValue
Open
$caseBlock .= " WHEN fieldid = {$db->quoteValue($newFieldSequence['fieldid'])} THEN {$db->quoteValue($newFieldSequence['block'])}";
- Exclude checks
Avoid excessively long variable names like $actionNotSupportedModules. Keep variable name length under 20. Open
$actionNotSupportedModules = ['calendar', 'events', 'faq', 'helpdesk'];
- Read upRead up
- Exclude checks
LongVariable
Since: 0.2
Detects when a field, formal or local variable is declared with a long name.
Example
class Something {
protected $reallyLongIntName = -3; // VIOLATION - Field
public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
$otherReallyLongName = -5; // VIOLATION - Local
for ($interestingIntIndex = 0; // VIOLATION - For
$interestingIntIndex < 10;
$interestingIntIndex++ ) {
}
}
}
Source https://phpmd.org/rules/naming.html#longvariable
Avoid excessively long variable names like $blocksEliminatedArray. Keep variable name length under 20. Open
$blocksEliminatedArray = ['calendar' => ['LBL_TASK_INFORMATION', 'LBL_DESCRIPTION_INFORMATION', 'LBL_RELATED_TO', 'LBL_REMINDER_INFORMATION', 'LBL_RECURRENCE_INFORMATION'],
- Read upRead up
- Exclude checks
LongVariable
Since: 0.2
Detects when a field, formal or local variable is declared with a long name.
Example
class Something {
protected $reallyLongIntName = -3; // VIOLATION - Field
public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
$otherReallyLongName = -5; // VIOLATION - Local
for ($interestingIntIndex = 0; // VIOLATION - For
$interestingIntIndex < 10;
$interestingIntIndex++ ) {
}
}
}
Source https://phpmd.org/rules/naming.html#longvariable
Each class must be in a namespace of at least one level (a top-level vendor name) Open
class Settings_LayoutEditor_Block_Model extends Vtiger_Block_Model
- Exclude checks
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
The class Settings_LayoutEditor_Block_Model is not named in CamelCase. Open
class Settings_LayoutEditor_Block_Model extends Vtiger_Block_Model
{
/**
* Function to check whether adding custom field is allowed or not.
*
- 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
Spaces must be used to indent lines; tabs are not allowed Open
'faq' => ['LBL_COMMENT_INFORMATION']];
- 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
'block' => new yii\db\Expression($caseBlock),
- 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
$fields = $this->getFields();
- 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
'helpdesk' => ['LBL_TICKET_RESOLUTION', 'LBL_COMMENTS'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param array $blockFieldSequence
- 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
}
- Exclude checks
Line exceeds 120 characters; contains 129 characters Open
$caseSequence .= " WHEN fieldid = {$db->quoteValue($fieldId)} THEN {$db->quoteValue($newFieldSequence['sequence'])}";
- 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
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Function to retrieve block instances for a module.
- 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 function getInstance($value, $moduleInstance = false)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return Vtiger_Block_Model
- 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
return true;
- 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
* Function to save sequence number of fields.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fieldId = $newFieldSequence['fieldid'];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($blockFieldSequence as $newFieldSequence) {
- 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
* @return <array> - list of Vtiger_Block_Model
- 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
$blocksEliminatedArray = ['calendar' => ['LBL_TASK_INFORMATION', 'LBL_DESCRIPTION_INFORMATION', 'LBL_RELATED_TO', 'LBL_REMINDER_INFORMATION', 'LBL_RECURRENCE_INFORMATION'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\in_array(strtolower($this->module->name), $actionNotSupportedModules)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*/
- Exclude checks
Line exceeds 120 characters; contains 143 characters Open
$caseBlock .= " WHEN fieldid = {$db->quoteValue($newFieldSequence['fieldid'])} THEN {$db->quoteValue($newFieldSequence['block'])}";
- 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
foreach ($objectProperties as $properName => $propertyValue) {
- 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
$activeFields = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function isAddCustomFieldEnabled()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fieldIdList = [];
- 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
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$blockModel = new self();
- 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
foreach ($fields as $fieldName => $fieldModel) {
- 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
return $activeFields;
- 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
* @param <type> $moduleModel - module instance
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function getLayoutBlockActiveFields()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\in_array($this->get('label'), $blocksEliminatedArray[strtolower($this->module->name)])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function updateFieldSequenceNumber($blockFieldSequence)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$caseSequence = 'CASE';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($blockFieldSequence as $newFieldSequence) {
- 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
$blockModelList = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Function to check whether adding custom field is allowed or not.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!empty($blocksEliminatedArray[strtolower($this->module->name)])) {
- 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
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$caseSequence .= " WHEN fieldid = {$db->quoteValue($fieldId)} THEN {$db->quoteValue($newFieldSequence['sequence'])}";
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$caseSequence .= ' END';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$caseBlock .= ' END';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'sequence' => new yii\db\Expression($caseSequence),
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return self::getInstanceFromBlockObject(parent::getInstance($value, $moduleInstance));
- 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
foreach ($blockObjects as $blockObject) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $blockModelList;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$actionNotSupportedModules = ['calendar', 'events', 'faq', 'helpdesk'];
- Exclude checks
Line exceeds 120 characters; contains 180 characters Open
$blocksEliminatedArray = ['calendar' => ['LBL_TASK_INFORMATION', 'LBL_DESCRIPTION_INFORMATION', 'LBL_RELATED_TO', 'LBL_REMINDER_INFORMATION', 'LBL_RECURRENCE_INFORMATION'],
- 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
* @param vtlib\Block $blockObject - vtlib block object
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$blockModel->{$properName} = $propertyValue;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($fieldModel->isActiveField()) {
- 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
$objectProperties = get_object_vars($blockObject);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $blockModel;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$blockModelList[] = self::getInstanceFromBlockObject($blockObject);
- 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
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$caseBlock = 'CASE';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$caseBlock .= " WHEN fieldid = {$db->quoteValue($newFieldSequence['fieldid'])} THEN {$db->quoteValue($newFieldSequence['block'])}";
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Function to retrieve block instance from vtlib\Block object.
- 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 function getInstanceFromBlockObject(vtlib\Block $blockObject)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getAllForModule(vtlib\ModuleBasic $moduleModel)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($blockObjects) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$db->createCommand()->update('vtiger_field', [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$blockObjects = parent::getAllForModule($moduleModel);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$activeFields[$fieldName] = $fieldModel;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return bool true/false
- 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
return false;
- 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
$fieldIdList[] = $fieldId;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
], ['fieldid' => $fieldIdList])->execute();
- Exclude checks
Class name "Settings_LayoutEditor_Block_Model" is not in camel caps format Open
class Settings_LayoutEditor_Block_Model extends Vtiger_Block_Model
- Exclude checks