Method getLinks
has 44 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function getLinks(\Vtiger_Record_Model $recordModel, string $source): ?\Vtiger_Link_Model
{
$fields = $recordModel->getModule()->getFieldsByType('serverAccess', true);
$isActive = 0;
foreach ($fields as $fieldName => $fieldModel) {
Function getLinks
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public static function getLinks(\Vtiger_Record_Model $recordModel, string $source): ?\Vtiger_Link_Model
{
$fields = $recordModel->getModule()->getFieldsByType('serverAccess', true);
$isActive = 0;
foreach ($fields as $fieldName => $fieldModel) {
- 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
The method getLinks() has a Cyclomatic Complexity of 10. The configured cyclomatic complexity threshold is 10. Open
public static function getLinks(\Vtiger_Record_Model $recordModel, string $source): ?\Vtiger_Link_Model
{
$fields = $recordModel->getModule()->getFieldsByType('serverAccess', true);
$isActive = 0;
foreach ($fields as $fieldName => $fieldModel) {
- Read upRead up
- Exclude checks
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
Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed. Open
public static function getLinks(\Vtiger_Record_Model $recordModel, string $source): ?\Vtiger_Link_Model
- 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 '96', column '15'). Open
$row = (new \App\Db\Query())->from('w_#__servers')->where(['id' => $serverId])->one(\App\Db::getInstance('webservice')) ?: [];
- 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\Cache' in method 'get'. Open
if (\App\Cache::has(__METHOD__, $serverId)) {
- 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 '\App\Cache' in method 'get'. Open
return \App\Cache::get(__METHOD__, $serverId);
- 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 '\App\Db' in method 'get'. Open
$row = (new \App\Db\Query())->from('w_#__servers')->where(['id' => $serverId])->one(\App\Db::getInstance('webservice')) ?: [];
- 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 '\App\Language' in method 'getLinks'. Open
$label = \App\Language::translate($isActive ? 'BTN_DISABLE_SHARE_RECORD_IN' : 'BTN_SHARE_RECORD_IN') . ' ' . ($webServiceApp['name'] ?? '');
- 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 '\App\Json' in method 'getLinks'. Open
'edit-fields' => \App\Json::encode(array_keys($fields)),
- 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_Link_Model' in method 'getLinks'. Open
$return = \Vtiger_Link_Model::getInstanceFromValues([
'linktype' => 'LIST_VIEW_ACTIONS_RECORD_LEFT_SIDE',
'linklabel' => 'BTN_SERVER_ACCESS',
'linkicon' => ($isActive ? 'yfi-share-portal-record' : 'yfi-unshare-portal-record'),
'linkclass' => 'btn-sm js-quick-edit-modal ' . self::BTN_CLASS[$source][$isActive],
- 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 getLinks uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$return = \Vtiger_Link_Model::getInstanceFromValues([
'linktype' => 'LIST_VIEW_ACTIONS_RECORD_LEFT_SIDE',
'linklabel' => 'BTN_SERVER_ACCESS',
'linkicon' => ($isActive ? 'yfi-share-portal-record' : 'yfi-unshare-portal-record'),
- 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 '\Vtiger_Link_Model' in method 'getLinks'. Open
$return = \Vtiger_Link_Model::getInstanceFromValues([
'linktype' => 'LIST_VIEW_ACTIONS_RECORD_LEFT_SIDE',
'linklabel' => 'BTN_SERVER_ACCESS',
'linkhint' => $label,
'linkicon' => ($isActive ? 'yfi-share-portal-record' : 'yfi-unshare-portal-record'),
- 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 '\App\Language' in method 'getLinks'. Open
'modal-title' => \App\Language::translate('BTN_SERVER_ACCESS'),
- 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 '\App\Cache' in method 'get'. Open
\App\Cache::save(__METHOD__, $serverId, $row, \App\Cache::MEDIUM);
- 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 "BTN_SERVER_ACCESS" 3 times. Open
'linklabel' => 'BTN_SERVER_ACCESS',
- 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.
Argument 2 (key)
is int
but \App\Cache::get()
takes string
defined at /code/app/Cache.php:61
Open
return \App\Cache::get(__METHOD__, $serverId);
- Exclude checks
Call to undeclared method \App\Db\Query::from
Open
$row = (new \App\Db\Query())->from('w_#__servers')->where(['id' => $serverId])->one(\App\Db::getInstance('webservice')) ?: [];
- Exclude checks
Argument 2 (key)
is int
but \App\Cache::save()
takes string
defined at /code/app/Cache.php:89
Open
\App\Cache::save(__METHOD__, $serverId, $row, \App\Cache::MEDIUM);
- Exclude checks
Call to undeclared function \array_key_first()
Open
$fieldName = array_key_first($fields);
- Exclude checks
Argument 2 (key)
is int
but \App\Cache::has()
takes string
defined at /code/app/Cache.php:74
Open
if (\App\Cache::has(__METHOD__, $serverId)) {
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
'module' => $recordModel->getModuleName(),
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
* Get web service application details by id.
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
if (\App\Cache::has(__METHOD__, $serverId)) {
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
}
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
$row = (new \App\Db\Query())->from('w_#__servers')->where(['id' => $serverId])->one(\App\Db::getInstance('webservice')) ?: [];
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
'record' => $recordModel->getId(),
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
{
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
return \App\Cache::get(__METHOD__, $serverId);
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
'linkdata' => [
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
'show-layout' => 'vertical',
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
'modal-title' => \App\Language::translate('BTN_SERVER_ACCESS'),
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
/**
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
'linkdata' => ['add-btn-icon' => 1, 'source-view' => $source],
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
]);
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
* @return array
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
$return = \Vtiger_Link_Model::getInstanceFromValues([
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
}
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
\App\Cache::save(__METHOD__, $serverId, $row, \App\Cache::MEDIUM);
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
return $return;
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
*/
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
} else {
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
'edit-fields' => \App\Json::encode(array_keys($fields)),
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
]);
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
'linklabel' => 'BTN_SERVER_ACCESS',
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
}
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
*
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
'linkclass' => 'btn-sm js-quick-edit-modal ' . self::BTN_CLASS[$source][$isActive],
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
*
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
return $row;
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
public static function get(int $serverId): array
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
}
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
'linktype' => 'LIST_VIEW_ACTIONS_RECORD_LEFT_SIDE',
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
'linkicon' => ($isActive ? 'yfi-share-portal-record' : 'yfi-unshare-portal-record'),
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
],
- Exclude checks
Spaces must be used for alignment; tabs are not allowed Open
* @param int $serverId
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
const BTN_CLASS = [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'ModComments' => [0 => 'text-secondary', 1 => 'text-success'],
- 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
if ($recordModel->getValueByField($fieldName)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fieldModel = $fields[$fieldName];
- 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 \Vtiger_Record_Model $recordModel
- 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
'linklabel' => 'BTN_SERVER_ACCESS',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'RelatedList' => [0 => 'btn-secondary', 1 => 'btn-success'],
- 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 \Vtiger_Link_Model|null
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'List' => [0 => 'btn-secondary', 1 => 'btn-success'],
- 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
$isActive = 0;
- 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
$fields = $recordModel->getModule()->getFieldsByType('serverAccess', true);
- Exclude checks
Line exceeds 120 characters; contains 152 characters Open
$label = \App\Language::translate($isActive ? 'BTN_DISABLE_SHARE_RECORD_IN' : 'BTN_SHARE_RECORD_IN') . ' ' . ($webServiceApp['name'] ?? '');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return null;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (empty($fields)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'linkdata' => ['add-btn-icon' => 1, 'source-view' => $source],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Get links to share the record in external services (Web service - Applications).
- 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
if (!$fieldModel->isEditable()) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$label = \App\Language::translate($isActive ? 'BTN_DISABLE_SHARE_RECORD_IN' : 'BTN_SHARE_RECORD_IN') . ' ' . ($webServiceApp['name'] ?? '');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'linkhint' => $label,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @var array Class mapping for different button places
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$isActive = 1;
- 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 = null;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'linkclass' => 'js-action-confirm btn-sm ' . self::BTN_CLASS[$source][$isActive],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getLinks(\Vtiger_Record_Model $recordModel, string $source): ?\Vtiger_Link_Model
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
unset($fields[$fieldName]);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'linkicon' => ($isActive ? 'yfi-share-portal-record' : 'yfi-unshare-portal-record'),
- Exclude checks
Line exceeds 120 characters; contains 175 characters Open
'dataUrl' => "index.php?module={$recordModel->getModuleName()}&action=SaveAjax&record={$recordModel->getId()}&field={$fieldName}&value=" . ($isActive ? 0 : 1),
- 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
'dataUrl' => "index.php?module={$recordModel->getModuleName()}&action=SaveAjax&record={$recordModel->getId()}&field={$fieldName}&value=" . ($isActive ? 0 : 1),
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fieldName = array_key_first($fields);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$webServiceApp = self::get($fieldModel->get('fieldparams'));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$return = \Vtiger_Link_Model::getInstanceFromValues([
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'linktype' => 'LIST_VIEW_ACTIONS_RECORD_LEFT_SIDE',
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param string $source
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (1 === \count($fields)) {
- Exclude checks
Line exceeds 120 characters; contains 134 characters Open
$row = (new \App\Db\Query())->from('w_#__servers')->where(['id' => $serverId])->one(\App\Db::getInstance('webservice')) ?: [];
- Exclude checks