Function followers
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public static function followers(array $input, User $user): bool
{
if (!AmpConfig::get('sociable')) {
Api5::error(T_('Enable: sociable'), ErrorCodeEnum::ACCESS_DENIED, self::ACTION, 'system', $input['api_format']);
- 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 true;
Reduce the number of returns of this function 5, down to the maximum allowed 3. Open
public static function followers(array $input, User $user): bool
- 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; }
Call to deprecated function \debug_event()
defined at /code/src/Config/functions.php:651
Open
debug_event(self::class, 'User `' . $username . '` cannot be found.', 1);
- Exclude checks
Call to method error
from undeclared class \Ampache\Module\Api\Api5
Open
Api5::error(sprintf(T_('Not Found: %s'), $username), ErrorCodeEnum::NOT_FOUND, self::ACTION, 'username', $input['api_format']);
- Exclude checks
Call to method check_parameter
from undeclared class \Ampache\Module\Api\Api5
Open
if (!Api5::check_parameter($input, array('username'), self::ACTION)) {
- Exclude checks
Call to method users
from undeclared class \Ampache\Module\Api\Json5_Data
Open
echo Json5_Data::users($results);
- Exclude checks
Call to method users
from undeclared class \Ampache\Module\Api\Xml5_Data
Open
echo Xml5_Data::users($results);
- Exclude checks
Call to method error
from undeclared class \Ampache\Module\Api\Api5
Open
Api5::error(T_('Enable: sociable'), ErrorCodeEnum::ACCESS_DENIED, self::ACTION, 'system', $input['api_format']);
- Exclude checks
Checking instanceof against undeclared class \Ampache\Repository\Model\User
Open
if (!$leader instanceof User || $leader->id < 1) {
- Exclude checks
Call to deprecated function \Ampache\Module\Api\Method\Api5\Followers5Method::getUserFollowerRepository()
defined at /code/src/Module/Api/Method/Api5/Followers5Method.php:96
Open
$results = static::getUserFollowerRepository()->getFollowers($leader->getId());
- Exclude checks
Call to method getId
from undeclared class \Ampache\Repository\Model\User
Open
$results = static::getUserFollowerRepository()->getFollowers($leader->getId());
- Exclude checks
Call to method get_from_username
from undeclared class \Ampache\Repository\Model\User
Open
$leader = User::get_from_username($username);
- Exclude checks
Parameter $user
has undeclared type \Ampache\Repository\Model\User
Open
public static function followers(array $input, User $user): bool
- Exclude checks
Call to method empty
from undeclared class \Ampache\Module\Api\Api5
Open
Api5::empty('user', $input['api_format']);
- Exclude checks
Reference to instance property id
from undeclared class \Ampache\Repository\Model\User
Open
if (!$leader instanceof User || $leader->id < 1) {
- Exclude checks
Define a constant instead of duplicating this literal "api_format" 4 times. Open
Api5::error(T_('Enable: sociable'), ErrorCodeEnum::ACCESS_DENIED, self::ACTION, 'system', $input['api_format']);
- 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 "username" 3 times. Open
if (!Api5::check_parameter($input, array('username'), self::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.
Similar blocks of code found in 2 locations. Consider refactoring. Open
<?php
declare(strict_types=0);
/**
- 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 380.
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