Function getRelationField
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring. Open
public function getRelationField()
{
$relationField = $this->get('relationField');
if (!$relationField) {
$relationField = false;
- 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
Method getRelationField
has 27 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function getRelationField()
{
$relationField = $this->get('relationField');
if (!$relationField) {
$relationField = false;
Refactor this function to reduce its Cognitive Complexity from 18 to the 15 allowed. Open
public function getRelationField()
- Read upRead up
- Exclude checks
Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.
See
Missing class import via use statement (line '29', column '90'). Open
$db->createCommand()->update('vtiger_campaign_records', ['campaignrelstatusid' => new yii\db\Expression($case)], ['campaignid' => $sourceRecordId])->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 'updateStatus'. 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 "relationField" 3 times. Open
$relationField = $this->get('relationField');
- 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 undeclared method \App\Db::createCommand
Open
$db->createCommand()->update('vtiger_campaign_records', ['campaignrelstatusid' => new yii\db\Expression($case)], ['campaignid' => $sourceRecordId])->execute();
- Exclude checks
Call to undeclared method \App\Db::quoteValue
Open
$case .= " WHEN {$db->quoteValue($relatedRecordId)} THEN {$db->quoteValue($status)}";
- Exclude checks
Call to method __construct
from undeclared class \yii\db\Expression
Open
$db->createCommand()->update('vtiger_campaign_records', ['campaignrelstatusid' => new yii\db\Expression($case)], ['campaignid' => $sourceRecordId])->execute();
- Exclude checks
Each class must be in a namespace of at least one level (a top-level vendor name) Open
class Campaigns_Relation_Model extends Vtiger_Relation_Model
- Exclude checks
The class Campaigns_Relation_Model is not named in CamelCase. Open
class Campaigns_Relation_Model extends Vtiger_Relation_Model
{
/**
* Function to update the status of relation.
*
- 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
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($relatedModelFields as $fieldName => $fieldModel) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$relationFieldArray[$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
foreach ($statusDetails as $relatedRecordId => $status) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->set('relationField', $fieldModel);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!$relationField && $relationFieldArray) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function updateStatus($sourceRecordId, $statusDetails = [])
- Exclude checks
Line exceeds 120 characters; contains 178 characters Open
if ($sourceRecordId && $statusDetails && \in_array($this->getRelationModuleModel()->getName(), ['Accounts', 'Leads', 'Vendors', 'Contacts', 'Partners', 'Competition'])) {
- Exclude checks
Line exceeds 120 characters; contains 171 characters Open
$db->createCommand()->update('vtiger_campaign_records', ['campaignrelstatusid' => new yii\db\Expression($case)], ['campaignid' => $sourceRecordId])->execute();
- 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 ('userCreator' !== !$fieldModel->getFieldDataType()) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
break;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$case = ' CASE crmid ';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($fieldModel->isReferenceField()) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\in_array($parentModule->getName(), $referenceList)) {
- 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
reset($relationFieldArray);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$relationField = false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$relationFieldArray = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param int $sourceRecordId
- 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
$db = App\Db::getInstance();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$case .= 'ELSE campaignrelstatusid END';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return Vtiger_Field_Model
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$parentModule = $this->getParentModuleModel();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$relatedModelFields = $relatedModel->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
* @param array $statusDetails
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$relationField = $this->get('relationField');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$relatedModel = $this->getRelationModuleModel();
- 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
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $relationField;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->set('relationField', current($relationFieldArray));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Function to update the status of relation.
- 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
$referenceList = $fieldModel->getReferenceList();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$db->createCommand()->update('vtiger_campaign_records', ['campaignrelstatusid' => new yii\db\Expression($case)], ['campaignid' => $sourceRecordId])->execute();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!$relationField) {
- 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 get relation field for relation module and parent 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 function getRelationField()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$relationField = current($relationFieldArray);
- 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 ($sourceRecordId && $statusDetails && \in_array($this->getRelationModuleModel()->getName(), ['Accounts', 'Leads', 'Vendors', 'Contacts', 'Partners', 'Competition'])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$case .= " WHEN {$db->quoteValue($relatedRecordId)} THEN {$db->quoteValue($status)}";
- 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
$relationField = $fieldModel;
- Exclude checks
Class name "Campaigns_Relation_Model" is not in camel caps format Open
class Campaigns_Relation_Model extends Vtiger_Relation_Model
- Exclude checks