Function checkPermissions
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public static function checkPermissions($record, $moduleName, $userId)
{
$privileges = static::get($moduleName);
if (false === $privileges) {
return 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
Function reloadCache
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public static function reloadCache()
{
$db = Db::getInstance('admin');
$query = (new Db\Query())->from('a_#__adv_permission')->where(['status' => 0])->orderBy(['priority' => SORT_DESC]);
$dataReader = $query->createCommand($db)->query();
- 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 '90', column '17'). Open
$test = (new \VTJsonCondition())->evaluate($privilege['conditions'], $recordModel);
- 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\PrivilegeUtil' in method 'reloadCache'. Open
$users = array_merge($users, PrivilegeUtil::getUserByMember($member));
- 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 'reloadCache'. Open
$db = Db::getInstance('admin');
- 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\Log' in method 'checkPermissions'. Open
Log::trace("Check advanced permissions test OK,action: {$privilege['action']},id: $id");
- 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\Log' in method 'checkPermissions'. Open
Log::trace("Check advanced permissions test FALSE , id: $id");
- 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 'reloadCache'. Open
$members = \App\Json::decode($row['members']);
- 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\Utils' in method 'reloadCache'. Open
$content = '<?php return ' . Utils::varExport($cache) . ';' . PHP_EOL;
- 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 'reloadCache'. Open
\App\Cache::resetFileCache(static::$cacheFile);
- 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 'checkPermissions'. 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\Module' in method 'get'. Open
$tabid = Module::getModuleId($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\Log' in method 'checkPermissions'. Open
Log::trace("Check advanced permissions: $record,$moduleName,$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
Define a constant instead of duplicating this literal "members" 3 times. Open
$members = \App\Json::decode($row['members']);
- 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 "conditions" 3 times. Open
'conditions' => $row['conditions'],
- 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 "action" 4 times. Open
'action' => $row['action'],
- 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\Query::from
Open
$query = (new Db\Query())->from('a_#__adv_permission')->where(['status' => 0])->orderBy(['priority' => SORT_DESC]);
- Exclude checks
Call to method trace
from undeclared class \App\Log
Open
Log::trace("Check advanced permissions test FALSE , id: $id");
- Exclude checks
Call to method trace
from undeclared class \App\Log
Open
Log::trace("Check advanced permissions test OK,action: {$privilege['action']},id: $id");
- Exclude checks
false
to array comparison Open
if (false === $privileges) {
- Exclude checks
Returning type false
but get()
is declared to return array
Open
return static::$cache[$tabid] ?? false;
- Exclude checks
Suspicious array access to false
Open
return static::$cache[$tabid] ?? false;
- Exclude checks
Call to method trace
from undeclared class \App\Log
Open
Log::trace("Check advanced permissions: $record,$moduleName,$userId");
- Exclude checks
A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 16 and the first side effect is on line 5. Open
<?php
- Exclude checks
Avoid variables with short names like $db. Configured minimum length is 3. Open
$db = Db::getInstance('admin');
- 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
* Update advanced permissions cache.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$users = array_unique($users);
- 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
while ($row = $dataReader->read()) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!empty($members)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($members as &$member) {
- 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
static::$webservice = true;
- 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
$dataReader = $query->createCommand($db)->query();
- 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
$content = '<?php return ' . Utils::varExport($cache) . ';' . PHP_EOL;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Load advanced permission rules for specific module.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$members = \App\Json::decode($row['members']);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Check advanced 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
}
- 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 (false === static::$cache) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
static::$cache = require static::$cacheFile;
- 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
protected static $cacheFile = 'user_privileges/advancedPermission.php';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
*
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!isset($privilege['members'][$userId])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return 0 === $privilege['action'] ? 1 : 0;
- 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 reloadCache()
- Exclude checks
Line exceeds 120 characters; contains 123 characters Open
$query = (new Db\Query())->from('a_#__adv_permission')->where(['status' => 0])->orderBy(['priority' => SORT_DESC]);
- 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|int
- 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
protected static $cache = 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
$cache[$row['tabid']][$row['id']] = [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param int $record
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static $webservice = true;
- 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
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return array
- 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
$users = array_merge($users, PrivilegeUtil::getUserByMember($member));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'action' => $row['action'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$users = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'conditions' => $row['conditions'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
\App\Cache::resetFileCache(static::$cacheFile);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function get($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
if (false === $privileges) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($privileges as $id => &$privilege) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$query = (new Db\Query())->from('a_#__adv_permission')->where(['status' => 0])->orderBy(['priority' => SORT_DESC]);
- 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
$tabid = Module::getModuleId($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
* @param string $moduleName
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
static::$webservice = false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Log::trace("Check advanced permissions test OK,action: {$privilege['action']},id: $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
$cache = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'members' => array_flip($users),
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
file_put_contents(static::$cacheFile, $content, LOCK_EX);
- 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
Log::trace("Check advanced permissions: $record,$moduleName,$userId");
- 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
$test = (new \VTJsonCondition())->evaluate($privilege['conditions'], $recordModel);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
Log::trace("Check advanced permissions test FALSE , id: $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
$db = Db::getInstance('admin');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return static::$cache[$tabid] ?? false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function checkPermissions($record, $moduleName, $userId)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$privileges = static::get($moduleName);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($test) {
- 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
continue;
- Exclude checks