Method getInstance
has 36 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function getInstance($qualifiedId)
{
$idComponents = self::getIdComponentsFromQualifiedId($qualifiedId);
$type = $idComponents[0];
$memberId = $idComponents[1];
Function getInstance
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
public static function getInstance($qualifiedId)
{
$idComponents = self::getIdComponentsFromQualifiedId($qualifiedId);
$type = $idComponents[0];
$memberId = $idComponents[1];
- 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
Avoid too many return
statements within this method. Open
return false;
Reduce the number of returns of this function 5, down to the maximum allowed 3. Open
public static function getInstance($qualifiedId)
- 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; }
Missing class import via use statement (line '72', column '16'). Open
$row = (new App\Db\Query())->from('vtiger_users')
- 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 '62', column '16'). Open
$row = (new App\Db\Query())->from('vtiger_groups')
- 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 'getInstance'. Open
$row = App\PrivilegeUtil::getRoleDetail($memberId);
- 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 'Settings_Roles_Record_Model' in method 'getAll'. Open
$allRoles = Settings_Roles_Record_Model::getAll();
- 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 'Settings_Groups_Record_Model' in method 'getAll'. Open
$allGroups = Settings_Groups_Record_Model::getAll();
- 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 'Users_Record_Model' in method 'getAll'. Open
$allUsers = Users_Record_Model::getAll();
- 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\PrivilegeUtil' in method 'getInstance'. Open
$row = App\PrivilegeUtil::getRoleDetail($memberId);
- 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
Argument 2 (str)
is int
but \explode()
takes string
Open
return explode(':', $id);
- Exclude checks
Call to undeclared method \App\Db\Query::from
Open
$row = (new App\Db\Query())->from('vtiger_users')
- Exclude checks
Saw unextractable annotation for comment '* @return <array> - Array of Settings_SharingAccess_RuleMember_Model instances'</array>
Open
* @return <Array> - Array of Settings_SharingAccess_RuleMember_Model instances
- Exclude checks
Call to undeclared method \App\Db\Query::from
Open
$row = (new App\Db\Query())->from('vtiger_groups')
- Exclude checks
Saw unextractable annotation for comment '* @return <number> Id'</number>
Open
* @return <Number> Id
- Exclude checks
Returning type false
but getInstance()
is declared to return \Settings_SharingAccess_RuleMember_Model
Open
return false;
- Exclude checks
Similar blocks of code found in 2 locations. Consider refactoring. Open
foreach ($allRoles as $roleId => $roleModel) {
$qualifiedId = self::getQualifiedId(self::RULE_MEMBER_TYPE_ROLES, $roleId);
$rule = new self();
$rules[self::RULE_MEMBER_TYPE_ROLES][$qualifiedId] = $rule->set('id', $qualifiedId)->set('name', $roleModel->getName());
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 148.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Each class must be in a namespace of at least one level (a top-level vendor name) Open
class Settings_SharingAccess_RuleMember_Model extends \App\Base
- Exclude checks
Avoid variables with short names like $id. Configured minimum length is 3. Open
public static function getIdComponentsFromQualifiedId($id)
- Read upRead up
- Exclude checks
ShortVariable
Since: 0.2
Detects when a field, local, or parameter has a very short name.
Example
class Something {
private $q = 15; // VIOLATION - Field
public static function main( array $as ) { // VIOLATION - Formal
$r = 20 + $this->q; // VIOLATION - Local
for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
$r += $this->q;
}
}
}
Source https://phpmd.org/rules/naming.html#shortvariable
The class Settings_SharingAccess_RuleMember_Model is not named in CamelCase. Open
class Settings_SharingAccess_RuleMember_Model extends \App\Base
{
const RULE_MEMBER_TYPE_GROUPS = 'Groups';
const RULE_MEMBER_TYPE_ROLES = 'Roles';
const RULE_MEMBER_TYPE_ROLE_AND_SUBORDINATES = 'RoleAndSubordinates';
- Read upRead up
- Exclude checks
CamelCaseClassName
Since: 0.2
It is considered best practice to use the CamelCase notation to name classes.
Example
class class_name {
}
Source
Avoid variables with short names like $id. Configured minimum length is 3. Open
public static function getQualifiedId($type, $id)
- 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
public function getId()
- 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 ($row) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->one();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$memberId = $idComponents[1];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (self::RULE_MEMBER_TYPE_GROUPS === $type) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return (new self())->set('id', $qualifiedId)->set('name', $row['rolename']);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$row = App\PrivilegeUtil::getRoleDetail($memberId);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $this->get('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
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->where(['groupid' => $memberId])
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$idComponents = self::getIdComponentsFromQualifiedId($qualifiedId);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($row) {
- 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
* Function to get the Group Name.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getIdComponentsFromQualifiedId($id)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public static function getQualifiedId($type, $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
$qualifiedId = self::getQualifiedId(self::RULE_MEMBER_TYPE_USERS, $row['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
public function 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
$row = (new App\Db\Query())->from('vtiger_groups')
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $this->get('name');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Function to get instance of class.
- 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 RULE_MEMBER_TYPE_USERS = '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
->one();
- 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
const RULE_MEMBER_TYPE_GROUPS = 'Groups';
- 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 explode(':', $id);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$row = (new App\Db\Query())->from('vtiger_users')
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($row) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$qualifiedId = self::getQualifiedId(self::RULE_MEMBER_TYPE_ROLES, $roleId);
- 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 (new self())->set('id', $qualifiedId)->set('name', $row['groupname']);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
->where(['id' => $memberId])
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$rules[self::RULE_MEMBER_TYPE_ROLE_AND_SUBORDINATES][$qualifiedId] = $rule->set('id', $qualifiedId)->set('name', $roleModel->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
public static function getInstance($qualifiedId)
- 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
$qualifiedId = self::getQualifiedId(self::RULE_MEMBER_TYPE_ROLE_AND_SUBORDINATES, $row['roleid']);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$qualifiedId = self::getQualifiedId(self::RULE_MEMBER_TYPE_GROUPS, $groupId);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
const RULE_MEMBER_TYPE_ROLES = 'Roles';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
const RULE_MEMBER_TYPE_ROLE_AND_SUBORDINATES = 'RoleAndSubordinates';
- 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 string
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return self
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$qualifiedId = self::getQualifiedId(self::RULE_MEMBER_TYPE_ROLES, $row['roleid']);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$rules[self::RULE_MEMBER_TYPE_ROLES][$qualifiedId] = $rule->set('id', $qualifiedId)->set('name', $roleModel->getName());
- Exclude checks
Line exceeds 120 characters; contains 132 characters Open
$rules[self::RULE_MEMBER_TYPE_ROLES][$qualifiedId] = $rule->set('id', $qualifiedId)->set('name', $roleModel->getName());
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$qualifiedId = self::getQualifiedId(self::RULE_MEMBER_TYPE_ROLE_AND_SUBORDINATES, $roleId);
- 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 (self::RULE_MEMBER_TYPE_ROLES === $type) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (self::RULE_MEMBER_TYPE_ROLE_AND_SUBORDINATES === $type) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Function to get all the rule members.
- 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> - Array of Settings_SharingAccess_RuleMember_Model instances
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$rule = new self();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$rule = new self();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @return <Number> 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
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* @param int $qualifiedId
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
/**
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $type . ':' . $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
$type = $idComponents[0];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$qualifiedId = self::getQualifiedId(self::RULE_MEMBER_TYPE_GROUPS, $row['groupid']);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return (new self())->set('id', $qualifiedId)->set('name', $row['first_name'] . ' ' . $row['last_name']);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return (new self())->set('id', $qualifiedId)->set('name', $row['rolename']);
- 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 getAll()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($allGroups as $groupId => $groupModel) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$rule = new self();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$rule = new self();
- Exclude checks
Line exceeds 120 characters; contains 139 characters Open
$rules[self::RULE_MEMBER_TYPE_USERS][$qualifiedId] = $rule->set('id', $qualifiedId)->set('name', $userModel->getDisplayName());
- Exclude checks
Line exceeds 120 characters; contains 134 characters Open
$rules[self::RULE_MEMBER_TYPE_GROUPS][$qualifiedId] = $rule->set('id', $qualifiedId)->set('name', $groupModel->getName());
- Exclude checks
Line exceeds 120 characters; contains 148 characters Open
$rules[self::RULE_MEMBER_TYPE_ROLE_AND_SUBORDINATES][$qualifiedId] = $rule->set('id', $qualifiedId)->set('name', $roleModel->getName());
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$allUsers = Users_Record_Model::getAll();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Function to get the Qualified Id of the Group RuleMember.
- 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 (self::RULE_MEMBER_TYPE_USERS === $type) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($row) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$row = App\PrivilegeUtil::getRoleDetail($memberId);
- 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 $rules;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$allGroups = Settings_Groups_Record_Model::getAll();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($allUsers as $userId => $userModel) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$rules[self::RULE_MEMBER_TYPE_USERS][$qualifiedId] = $rule->set('id', $qualifiedId)->set('name', $userModel->getDisplayName());
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$rules = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$rules[self::RULE_MEMBER_TYPE_GROUPS][$qualifiedId] = $rule->set('id', $qualifiedId)->set('name', $groupModel->getName());
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($allRoles as $roleId => $roleModel) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$qualifiedId = self::getQualifiedId(self::RULE_MEMBER_TYPE_USERS, $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
$allRoles = Settings_Roles_Record_Model::getAll();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Class name "Settings_SharingAccess_RuleMember_Model" is not in camel caps format Open
class Settings_SharingAccess_RuleMember_Model extends \App\Base
- Exclude checks