Function isPermitted
has a Cognitive Complexity of 58 (exceeds 5 allowed). Consider refactoring. Open
public static function isPermitted(string $moduleName, $actionName = null, $record = false, $userId = false)
{
if (!$userId) {
$user = \App\User::getCurrentUserModel();
} else {
- 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 isPermitted
has 103 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function isPermitted(string $moduleName, $actionName = null, $record = false, $userId = false)
{
if (!$userId) {
$user = \App\User::getCurrentUserModel();
} else {
Avoid too many return
statements within this method. Open
return true;
Avoid too many return
statements within this method. Open
return true;
Avoid too many return
statements within this method. Open
return false;
Avoid too many return
statements within this method. Open
return false;
Avoid too many return
statements within this method. Open
return true;
Avoid too many return
statements within this method. Open
return true;
Avoid too many return
statements within this method. Open
return true;
Avoid too many return
statements within this method. Open
return false;
Avoid too many return
statements within this method. Open
return $permission;
The method isPermitted() has an NPath complexity of 12636000. The configured NPath complexity threshold is 200. Open
public static function isPermitted(string $moduleName, $actionName = null, $record = false, $userId = false)
{
if (!$userId) {
$user = \App\User::getCurrentUserModel();
} else {
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method isPermitted() has 108 lines of code. Current threshold is set to 100. Avoid really long methods. Open
public static function isPermitted(string $moduleName, $actionName = null, $record = false, $userId = false)
{
if (!$userId) {
$user = \App\User::getCurrentUserModel();
} else {
- Exclude checks
The method isPermitted() has a Cyclomatic Complexity of 37. The configured cyclomatic complexity threshold is 10. Open
public static function isPermitted(string $moduleName, $actionName = null, $record = false, $userId = false)
{
if (!$userId) {
$user = \App\User::getCurrentUserModel();
} else {
- 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 58 to the 15 allowed. Open
public static function isPermitted(string $moduleName, $actionName = null, $record = false, $userId = false)
- 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
Reduce the number of returns of this function 13, down to the maximum allowed 3. Open
public static function isPermitted(string $moduleName, $actionName = null, $record = false, $userId = false)
- 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; }
The class Privilege has a coupling between objects value of 14. Consider to reduce the number of dependencies under 13. Open
class Privilege
{
/**
* Permissions based on user.
*/
- Read upRead up
- Exclude checks
CouplingBetweenObjects
Since: 1.1.0
A class with too many dependencies has negative impacts on several quality aspects of a class. This includes quality criteria like stability, maintainability and understandability
Example
class Foo {
/**
* @var \foo\bar\X
*/
private $x = null;
/**
* @var \foo\bar\Y
*/
private $y = null;
/**
* @var \foo\bar\Z
*/
private $z = null;
public function setFoo(\Foo $foo) {}
public function setBar(\Bar $bar) {}
public function setBaz(\Baz $baz) {}
/**
* @return \SplObjectStorage
* @throws \OutOfRangeException
* @throws \InvalidArgumentException
* @throws \ErrorException
*/
public function process(\Iterator $it) {}
// ...
}
Source https://phpmd.org/rules/design.html#couplingbetweenobjects
Missing class import via use statement (line '178', column '14'). Open
throw new \Api\Core\Exception('No permission to X-PARENT-ID', 403);
- 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 '170', column '21'). Open
$hierarchy = new \Api\WebservicePremium\BaseModule\Hierarchy();
- 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 isPermitted has a boolean flag argument $record, which is a certain sign of a Single Responsibility Principle violation. Open
public static function isPermitted(string $moduleName, $actionName = null, $record = false, $userId = 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
The method isPermitted has a boolean flag argument $userId, which is a certain sign of a Single Responsibility Principle violation. Open
public static function isPermitted(string $moduleName, $actionName = null, $record = false, $userId = 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 '71', column '15'). Open
throw new \Api\Core\Exception('Invalid permissions ', 400);
- 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\Privilege' in method 'isPermitted'. Open
return \App\Privilege::checkPermission($moduleName, $actionName, $record, $userId);
- 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 'isPermitted'. Open
$relationModel = \Vtiger_Relation_Model::getInstanceById($relationId);
- 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\Request' in method 'getParentCrmId'. Open
if (($parentId = (int) \App\Request::_getHeader('x-parent-id')) && $parentApiId !== $parentId) {
- 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 assigning values to variables in if clauses and the like (line '119', column '7'). Open
public static function isPermitted(string $moduleName, $actionName = null, $record = false, $userId = false)
{
if (!$userId) {
$user = \App\User::getCurrentUserModel();
} else {
- Read upRead up
- Exclude checks
IfStatementAssignment
Since: 2.7.0
Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.
Example
class Foo
{
public function bar($flag)
{
if ($foo = 'bar') { // possible typo
// ...
}
if ($baz = 0) { // always false
// ...
}
}
}
Source http://phpmd.org/rules/cleancode.html#ifstatementassignment
Avoid using static access to class '\App\Record' in method 'isPermitted'. Open
$parentModule = \App\Record::getType($parentRecordId) ?? '';
- 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 '\Documents_Record_Model' in method 'isPermitted'. Open
foreach (\Documents_Record_Model::getReferenceModuleByDocId($record) as $parentModuleName) {
- 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\User' in method 'isPermitted'. Open
$user = \App\User::getUserModel($userId);
- 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\Record' in method 'isPermitted'. Open
$parentRecordId = \App\Record::getParentRecord($user->get('permission_crmid'));
- 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\Record' in method 'getParentCrmId'. Open
$hierarchy->moduleName = \App\Record::getType($parentApiId);
- 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\Record' in method 'isPermitted'. Open
if (\App\Record::isExists($relRecordId, $relModuleName) && \App\Record::getType($relRecordId) === $relModuleName && \Vtiger_Record_Model::getInstanceById($relRecordId, $relModuleName)->get($referenceField->getName()) === $parentRecordId) {
- 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\Record' in method 'isPermitted'. Open
if (\App\Record::isExists($relRecordId, $relModuleName) && \App\Record::getType($relRecordId) === $relModuleName && \Vtiger_Record_Model::getInstanceById($relRecordId, $relModuleName)->get($referenceField->getName()) === $parentRecordId) {
- 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 isPermitted uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$user = \App\User::getUserModel($userId);
}
- 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_RelationListView_Model' in method 'isPermitted'. Open
$relationListView = \Vtiger_RelationListView_Model::getInstance($recordModel, $parentModuleName);
- 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 assigning values to variables in if clauses and the like (line '73', column '42'). Open
public static function isPermitted(string $moduleName, $actionName = null, $record = false, $userId = false)
{
if (!$userId) {
$user = \App\User::getCurrentUserModel();
} else {
- Read upRead up
- Exclude checks
IfStatementAssignment
Since: 2.7.0
Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.
Example
class Foo
{
public function bar($flag)
{
if ($foo = 'bar') { // possible typo
// ...
}
if ($baz = 0) { // always false
// ...
}
}
}
Source http://phpmd.org/rules/cleancode.html#ifstatementassignment
Avoid using static access to class '\App\ModuleHierarchy' in method 'isPermitted'. Open
if (0 === \App\ModuleHierarchy::getModuleLevel($moduleName)) {
- 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_Module_Model' in method 'isPermitted'. Open
$relModuleModel = \Vtiger_Module_Model::getInstance($relModuleName);
- 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\Privilege' in method 'isPermitted'. Open
return \App\Privilege::checkPermission($moduleName, $actionName, $record, $userId);
- 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\Relation' in method 'isPermitted'. Open
foreach (array_keys(\App\Relation::getByModule($parentModule, true, $moduleName)) as $relationId) {
- 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_Record_Model' in method 'isPermitted'. Open
$relationModel->set('parentRecord', \Vtiger_Record_Model::getInstanceById($parentRecordId, $parentModule));
- 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 '\Api\Core\Module' in method 'isPermitted'. Open
if ('ModComments' !== $moduleName && !($permissionFieldInfo = \Api\Core\Module::getApiFieldPermission($moduleName, $user->get('permission_app')))) {
- 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\User' in method 'isPermitted'. Open
$user = \App\User::getCurrentUserModel();
- 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_Record_Model' in method 'isPermitted'. Open
$recordModel = \Vtiger_Record_Model::getInstanceById($record, $moduleName);
- 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\Privilege' in method 'isPermitted'. Open
if (\App\Privilege::isPermitted($parentModuleName, 'DetailView', $id, $user->getId())) {
- 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 assigning values to variables in if clauses and the like (line '169', column '8'). Open
public static function getParentCrmId(\App\User $user): int
{
$contactId = $user->get('permission_crmid');
$parentApiId = \App\Record::getParentRecord($contactId);
if (($parentId = (int) \App\Request::_getHeader('x-parent-id')) && $parentApiId !== $parentId) {
- Read upRead up
- Exclude checks
IfStatementAssignment
Since: 2.7.0
Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.
Example
class Foo
{
public function bar($flag)
{
if ($foo = 'bar') { // possible typo
// ...
}
if ($baz = 0) { // always false
// ...
}
}
}
Source http://phpmd.org/rules/cleancode.html#ifstatementassignment
Avoid using static access to class '\App\Privilege' in method 'isPermitted'. Open
if (!\App\Privilege::checkPermission($moduleName, $actionName, $record, $userId)) {
- 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\Record' in method 'getParentCrmId'. Open
$parentApiId = \App\Record::getParentRecord($contactId);
- 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 "permission_type" 3 times. Open
if (empty($record) || !$user->has('permission_type')) {
- 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 "fieldname" 3 times. Open
if ('ModComments' !== $moduleName && !$recordModel->get($permissionFieldInfo['fieldname'])) {
- 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 readColumn
from undeclared class \yii\db\DataReader
Open
while ($id = $dataReader->readColumn(0)) {
- Exclude checks
Call to method get
from undeclared class \App\User
(Did you mean class \Tests\App\User) Open
$contactId = $user->get('permission_crmid');
- Exclude checks
Call to method getCurrentUserModel
from undeclared class \App\User
(Did you mean class \Tests\App\User) Open
$user = \App\User::getCurrentUserModel();
- Exclude checks
Call to method getUserModel
from undeclared class \App\User
(Did you mean class \Tests\App\User) Open
$user = \App\User::getUserModel($userId);
- Exclude checks
Argument 1 (recordId)
is ?int
but \App\Record::getType()
takes int
defined at /code/app/Record.php:279
Open
$parentModule = \App\Record::getType($parentRecordId) ?? '';
- Exclude checks
Call to method clearFields
from undeclared class \App\QueryGenerator
(Did you mean class \Tests\App\QueryGenerator) Open
$queryGenerator->clearFields()->setFields(['id'])->addCondition('id', $record, 'e');
- Exclude checks
Call to method setFields
from undeclared class \App\QueryGenerator
(Did you mean class \Tests\App\QueryGenerator) Open
$relationListView->getQueryGenerator()->setFields(['id'])->setLimit(10)->permissions = false;
- Exclude checks
Argument 1 (recordId)
is ?int
but \Vtiger_Record_Model::getInstanceById()
takes int
defined at /code/modules/Vtiger/models/Record.php:763
Open
$relationModel->set('parentRecord', \Vtiger_Record_Model::getInstanceById($parentRecordId, $parentModule));
- Exclude checks
Call to method createCommand
from undeclared class \App\QueryGenerator
(Did you mean class \Tests\App\QueryGenerator) Open
$dataReader = $relationListView->getRelationQuery()->createCommand()->query();
- Exclude checks
Call to method get
from undeclared class \App\User
(Did you mean class \Tests\App\User) Open
$hierarchy->setAllUserData(['crmid' => $contactId, 'type' => $user->get('permission_type')]);
- Exclude checks
Reference to instance property permissions
from undeclared class \App\QueryGenerator
Open
$queryGenerator->permissions = false;
- Exclude checks
Call to method createQuery
from undeclared class \App\QueryGenerator
(Did you mean class \Tests\App\QueryGenerator) Open
if ($queryGenerator->createQuery()->exists()) {
- Exclude checks
Parameter $user
has undeclared type \App\User
(Did you mean class \Tests\App\User) Open
public static function getParentCrmId(\App\User $user): int
- Exclude checks
Avoid variables with short names like $id. Configured minimum length is 3. Open
while ($id = $dataReader->readColumn(0)) {
- 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
switch ($user->get('permission_type')) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$parentRecordId = static::getParentCrmId($user);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
const ACCOUNTS_RELATED_RECORDS_AND_LOWER_IN_HIERARCHY = 3;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Permissions based on user.
- 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 mixed $userId
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function isPermitted(string $moduleName, $actionName = null, $record = false, $userId = false)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Function to check permission for a Module/Action/Record.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$userId = $user->getId();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (empty($record) || !$user->has('permission_type')) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @throws \Api\Core\Exception
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$parentRecordId = \App\Record::getParentRecord($user->get('permission_crmid'));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
throw new \Api\Core\Exception('Invalid permissions ', 400);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
const USER_PERMISSIONS = 1;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* All related records of account assigned directly to contact and accounts from hierarchy.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param bool|int $record
- 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 bool
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
case self::ACCOUNTS_RELATED_RECORDS:
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
default:
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!\App\Privilege::checkPermission($moduleName, $actionName, $record, $userId)) {
- 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
$user = \App\User::getUserModel($userId);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
case self::ACCOUNTS_RELATED_RECORDS_IN_HIERARCHY:
- 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 \App\Privilege::checkPermission($moduleName, $actionName, $record, $userId);
- 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
const ACCOUNTS_RELATED_RECORDS = 2;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* All related records of account assigned directly to contact and accounts lower in hierarchy.
- 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 string $moduleName
- 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
* @param string $actionName
- 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
return \App\Privilege::checkPermission($moduleName, $actionName, $record, $userId);
- 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
*/
- 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
$user = \App\User::getCurrentUserModel();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
break;
- Exclude checks
Line exceeds 120 characters; contains 156 characters Open
if ('ModComments' !== $moduleName && !($permissionFieldInfo = \Api\Core\Module::getApiFieldPermission($moduleName, $user->get('permission_app')))) {
- Exclude checks
Line exceeds 120 characters; contains 162 characters Open
\App\Privilege::$isPermittedLevel = "FIELD_PERMISSION_NO {$permissionFieldInfo['fieldname']}: {$recordModel->get($permissionFieldInfo['fieldname'])}";
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$queryGenerator = $relationModel->getQuery();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach (\Documents_Record_Model::getReferenceModuleByDocId($record) as $parentModuleName) {
- 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
public static function getParentCrmId(\App\User $user): int
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (($parentId = (int) \App\Request::_getHeader('x-parent-id')) && $parentApiId !== $parentId) {
- 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 (0 === \App\ModuleHierarchy::getModuleLevel($moduleName)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$permission = $parentRecordId === $record;
- 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
$relationListView->setFields([]);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
\App\Privilege::$isPermittedLevel = "FIELD_PERMISSION_NO {$permissionFieldInfo['fieldname']}: {$recordModel->get($permissionFieldInfo['fieldname'])}";
- 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
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
$queryGenerator->permissions = false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($fieldModel->getReferenceList() as $relModuleName) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$relModuleModel = \Vtiger_Module_Model::getInstance($relModuleName);
- 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
$relationListView = \Vtiger_RelationListView_Model::getInstance($recordModel, $parentModuleName);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$parentApiId = \App\Record::getParentRecord($contactId);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$hierarchy->findId = $parentId;
- 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 ($queryGenerator->createQuery()->exists()) {
- 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
case self::ACCOUNTS_RELATED_RECORDS_AND_LOWER_IN_HIERARCHY:
- 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
$queryGenerator->clearFields()->setFields(['id'])->addCondition('id', $record, 'e');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\App\Privilege::isPermitted($parentModuleName, 'DetailView', $id, $user->getId())) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (isset($records[$parentId])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
\App\Privilege::$isPermittedLevel = 'RECORD_HIERARCHY_LEVEL_' . ($permission ? 'YES' : 'NO');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$recordModel = \Vtiger_Record_Model::getInstanceById($record, $moduleName);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($relModuleModel->getReferenceFieldsForModule($parentModule) as $referenceField) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
\App\Privilege::$isPermittedLevel = 'ALL_PERMISSION_NO';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
\App\Privilege::$isPermittedLevel = 'RECORD_RELATED_YES';
- 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
\App\Privilege::$isPermittedLevel = $moduleName . '_RELATED_SL_YES';
- 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
$hierarchy = new \Api\WebservicePremium\BaseModule\Hierarchy();
- 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
* All records of account assigned directly to contact.
- 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
case self::USER_PERMISSIONS:
- 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
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$hierarchy->moduleName = \App\Record::getType($parentApiId);
- 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
if (!$fieldModel->isActiveField() || $recordModel->isEmpty($fieldModel->getName())) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$dataReader = $relationListView->getRelationQuery()->createCommand()->query();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
while ($id = $dataReader->readColumn(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
if ($fieldsForParent) {
- 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
\App\Privilege::$isPermittedLevel = 'FIELD_PERMISSION_NOT_EXISTS';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
\App\Privilege::$isPermittedLevel = $moduleName . '_RELATED_YES';
- 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
}
- 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
$relationModel = \Vtiger_Relation_Model::getInstanceById($relationId);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ('Documents' === $moduleName) {
- 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
* @param \App\User $user
- 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
$moduleModel = $recordModel->getModule();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\in_array($moduleName, ['Products', 'Services'])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($recordModel->get($referenceField->getName()) === $parentRecordId) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
continue;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ('Users' === $relModuleName || $relModuleName === $parentModule || $relModuleName === $moduleName) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
continue;
- 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 ('ModComments' !== $moduleName && !$recordModel->get($permissionFieldInfo['fieldname'])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($fieldsForParent as $referenceField) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
\App\Privilege::$isPermittedLevel = 'RECORD_RELATED_NO';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Line exceeds 120 characters; contains 263 characters Open
if (\App\Record::isExists($relRecordId, $relModuleName) && \App\Record::getType($relRecordId) === $relModuleName && \Vtiger_Record_Model::getInstanceById($relRecordId, $relModuleName)->get($referenceField->getName()) === $parentRecordId) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Gets parent 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
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$hierarchy->setAllUserData(['crmid' => $contactId, 'type' => $user->get('permission_type')]);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$records = $hierarchy->get();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
\App\Privilege::$isPermittedLevel = $moduleName . '_SPECIAL_PERMISSION_YES';
- 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
$relRecordId = $recordModel->get($fieldModel->getName());
- 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 int
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
throw new \Api\Core\Exception('No permission to X-PARENT-ID', 403);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $parentApiId;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $permission;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$fieldsForParent = $moduleModel->getReferenceFieldsForModule($parentModule);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach (array_keys(\App\Relation::getByModule($parentModule, true, $moduleName)) as $relationId) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($fields = $moduleModel->getFieldsByReference()) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($fields as $fieldModel) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
\App\Privilege::$isPermittedLevel = "PERMISSION_{$parentModuleName}_YES-{$id}";
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $parentId;
- 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
const ACCOUNTS_RELATED_RECORDS_IN_HIERARCHY = 4;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!$userId) {
- 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 ('ModComments' !== $moduleName && !($permissionFieldInfo = \Api\Core\Module::getApiFieldPermission($moduleName, $user->get('permission_app')))) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$parentModule = \App\Record::getType($parentRecordId) ?? '';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$relationModel->set('parentRecord', \Vtiger_Record_Model::getInstanceById($parentRecordId, $parentModule));
- Exclude checks
Line exceeds 120 characters; contains 123 characters Open
if ('Users' === $relModuleName || $relModuleName === $parentModule || $relModuleName === $moduleName) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (\App\Record::isExists($relRecordId, $relModuleName) && \App\Record::getType($relRecordId) === $relModuleName && \Vtiger_Record_Model::getInstanceById($relRecordId, $relModuleName)->get($referenceField->getName()) === $parentRecordId) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$relationListView->getQueryGenerator()->setFields(['id'])->setLimit(10)->permissions = false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$contactId = $user->get('permission_crmid');
- Exclude checks