Function catalog_file
has a Cognitive Complexity of 41 (exceeds 5 allowed). Consider refactoring. Open
public static function catalog_file(array $input, User $user): bool
{
if (!Api::check_access('interface', 50, $user->id, self::ACTION, $input['api_format'])) {
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
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;
The method catalog_file() has 106 lines of code. Current threshold is set to 100. Avoid really long methods. Open
public static function catalog_file(array $input, User $user): bool
{
if (!Api::check_access('interface', 50, $user->id, self::ACTION, $input['api_format'])) {
return false;
}
- Exclude checks
The method catalog_file() has an NPath complexity of 105840. The configured NPath complexity threshold is 200. Open
public static function catalog_file(array $input, User $user): bool
{
if (!Api::check_access('interface', 50, $user->id, self::ACTION, $input['api_format'])) {
return false;
}
- 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 catalog_file() has a Cyclomatic Complexity of 29. The configured cyclomatic complexity threshold is 10. Open
public static function catalog_file(array $input, User $user): bool
{
if (!Api::check_access('interface', 50, $user->id, self::ACTION, $input['api_format'])) {
return false;
}
- 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
Reduce the number of returns of this function 7, down to the maximum allowed 3. Open
public static function catalog_file(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; }
Refactor this function to reduce its Cognitive Complexity from 40 to the 15 allowed. Open
public static function catalog_file(array $input, User $user): bool
- 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
Avoid using undefined variables such as '$SSE_OUTPUT' which will lead to PHP notices. Open
unset($SSE_OUTPUT);
- Read upRead up
- Exclude checks
UndefinedVariable
Since: 2.8.0
Detects when a variable is used that has not been defined before.
Example
class Foo
{
private function bar()
{
// $message is undefined
echo $message;
}
}
Source https://phpmd.org/rules/cleancode.html#undefinedvariable
The method catalog_file uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
Api::error('Not Found', ErrorCodeEnum::NOT_FOUND, self::ACTION, 'catalog', $input['api_format']);
}
- 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
Define a constant instead of duplicating this literal "api_format" 7 times. Open
if (!Api::check_access('interface', 50, $user->id, self::ACTION, $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.
Add a "case default" clause to this "switch" statement. Open
switch ($item) {
- Read upRead up
- Exclude checks
The requirement for a final case default
clause is defensive programming. The clause should either take appropriate action, or contain
a suitable comment as to why no action is taken. Even when the switch
covers all current values of an enum
, a default case
should still be used because there is no guarantee that the enum
won't be extended.
Noncompliant Code Example
switch ($param) { //missing default clause case 0: do_something(); break; case 1: do_something_else(); break; } switch ($param) { default: // default clause should be the last one error(); break; case 0: do_something(); break; case 1: do_something_else(); break; }
Compliant Solution
switch ($param) { case 0: do_something(); break; case 1: do_something_else(); break; default: error(); break; }
See
- MISRA C:2004, 15.0 - The MISRA C switch syntax shall be used.
- MISRA C:2004, 15.3 - The final clause of a switch statement shall be the default clause
- MISRA C++:2008, 6-4-3 - A switch statement shall be a well-formed switch statement.
- MISRA C++:2008, 6-4-6 - The final clause of a switch statement shall be the default-clause
- MISRA C:2012, 16.1 - All switch statements shall be well-formed
- MISRA C:2012, 16.4 - Every switch statement shall have a default label
- MISRA C:2012, 16.5 - A default label shall appear as either the first or the last switch label of a switch statement
- MITRE, CWE-478 - Missing Default Case in Switch Statement
- CERT, MSC01-C. - Strive for logical completeness
- CERT, MSC01-CPP. - Strive for logical completeness
Define a constant instead of duplicating this literal "clean" 3 times. Open
if (!file_exists($file) && !in_array('clean', $task)) {
- 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 "catalog" 4 times. Open
if (!Api::check_parameter($input, array('catalog', 'file', 'task'), 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.
Define a constant instead of duplicating this literal "remove" 3 times. Open
if (!AmpConfig::get('delete_from_disk') && in_array('remove', $task)) {
- 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.
Avoid unused local variables such as '$SSE_OUTPUT'. Open
unset($SSE_OUTPUT);
- Read upRead up
- Exclude checks
UnusedLocalVariable
Since: 0.2
Detects when a local variable is declared and/or assigned, but not used.
Example
class Foo {
public function doSomething()
{
$i = 5; // Unused
}
}
Source https://phpmd.org/rules/unusedcode.html#unusedlocalvariable
Call to method __construct
from undeclared class \Ampache\Repository\Model\Video
Open
$media = new Video(Catalog::get_id_from_file($file, $type));
- Exclude checks
Call to method update_table_counts
from undeclared class \Ampache\Repository\Model\Artist
Open
Artist::update_table_counts();
- Exclude checks
Call to method error
from undeclared class \Ampache\Module\Api\Api
Open
Api::error('Enable: delete_from_disk', ErrorCodeEnum::ACCESS_DENIED, self::ACTION, 'system', $input['api_format']);
- Exclude checks
Call to method __construct
from undeclared class \Ampache\Repository\Model\Podcast_Episode
Open
$media = new Podcast_Episode(Catalog::get_id_from_file($file, $type));
- Exclude checks
Call to method isNew
from undeclared class \Ampache\Repository\Model\Podcast_Episode
Open
if ($media->isNew() === false) {
- Exclude checks
Call to method error
from undeclared class \Ampache\Module\Api\Api
Open
Api::error(sprintf('Bad Request: %s', $item), ErrorCodeEnum::BAD_REQUEST, self::ACTION, 'task', $input['api_format']);
- Exclude checks
Call to method error
from undeclared class \Ampache\Module\Api\Api
Open
Api::error(sprintf('Not Found: %s', $catalog_id), ErrorCodeEnum::NOT_FOUND, self::ACTION, 'catalog', $input['api_format']);
- Exclude checks
Call to method isNew
from undeclared class \Ampache\Repository\Model\Podcast_Episode
Open
if ($media->isNew() === false) {
- Exclude checks
Call to method error
from undeclared class \Ampache\Module\Api\Api
Open
Api::error(sprintf('Not Found: %s', $file), ErrorCodeEnum::NOT_FOUND, self::ACTION, 'file', $input['api_format']);
- Exclude checks
Call to method __construct
from undeclared class \Ampache\Repository\Model\Song
Open
$media = new Song(Catalog::get_id_from_file($file, $type));
- Exclude checks
Call to method message
from undeclared class \Ampache\Module\Api\Api
Open
Api::message('successfully started: ' . $output_task . ' for ' . $file, $input['api_format']);
- Exclude checks
Call to method get_id_from_file
from undeclared class \Ampache\Repository\Model\Catalog
Open
$media = new Podcast_Episode(Catalog::get_id_from_file($file, $type));
- Exclude checks
Call to method isNew
from undeclared class \Ampache\Repository\Model\Podcast_Episode
Open
if ($media->isNew() === false) {
- Exclude checks
Possibly zero references to use statement for classlike/namespace Catalog_local
(\Ampache\Module\Catalog\Catalog_local)
Open
use Ampache\Module\Catalog\Catalog_local;
- Exclude checks
Parameter $user
has undeclared type \Ampache\Repository\Model\User
Open
public static function catalog_file(array $input, User $user): bool
- Exclude checks
Call to method get_id_from_file
from undeclared class \Ampache\Repository\Model\Catalog
Open
$media = new Video(Catalog::get_id_from_file($file, $type));
- Exclude checks
Variable $SSE_OUTPUT
is undeclared Open
unset($SSE_OUTPUT);
- Exclude checks
Call to method remove
from undeclared class \Ampache\Repository\Model\Podcast_Episode
Open
$media->remove();
- Exclude checks
Call to method check_access
from undeclared class \Ampache\Module\Api\Api
Open
if (!Api::check_access('interface', 50, $user->id, self::ACTION, $input['api_format'])) {
- Exclude checks
Call to method update_media_from_tags
from undeclared class \Ampache\Repository\Model\Catalog
Open
Catalog::update_media_from_tags($media, array($type));
- Exclude checks
Call to method check_parameter
from undeclared class \Ampache\Module\Api\Api
Open
if (!Api::check_parameter($input, array('catalog', 'file', 'task'), self::ACTION)) {
- Exclude checks
Reference to instance property id
from undeclared class \Ampache\Repository\Model\User
Open
if (!Api::check_access('interface', 50, $user->id, self::ACTION, $input['api_format'])) {
- Exclude checks
Reference to instance property album
from undeclared class \Ampache\Repository\Model\Song
Open
Album::update_album_count($media->album);
- Exclude checks
Checking instanceof against undeclared class \Ampache\Repository\Model\Song
Open
if ($media instanceof Song) {
- Exclude checks
Call to method error
from undeclared class \Ampache\Module\Api\Api
Open
Api::error('Not Found', ErrorCodeEnum::NOT_FOUND, self::ACTION, 'catalog', $input['api_format']);
- Exclude checks
Call to method create_from_id
from undeclared class \Ampache\Repository\Model\Catalog
Open
$catalog = Catalog::create_from_id($catalog_id);
- Exclude checks
Call to method get_id_from_file
from undeclared class \Ampache\Repository\Model\Catalog
Open
$media = new Song(Catalog::get_id_from_file($file, $type));
- Exclude checks
Call to method update_album_count
from undeclared class \Ampache\Repository\Model\Album
Open
Album::update_album_count($media->album);
- Exclude checks
Call to method isNew
from undeclared class \Ampache\Repository\Model\Podcast_Episode
Open
if ($media->isNew()) {
- Exclude checks
Identical blocks of code found in 2 locations. Consider refactoring. Open
if ($catalog->catalog_type == 'local') {
foreach ($task as $item) {
if (defined('SSE_OUTPUT')) {
unset($SSE_OUTPUT);
}
- 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 205.
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
Identical blocks of code found in 3 locations. Consider refactoring. Open
switch ($catalog->gather_types) {
case 'podcast':
$type = 'podcast_episode';
$media = new Podcast_Episode(Catalog::get_id_from_file($file, $type));
break;
- 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 128.
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
Identical blocks of code found in 2 locations. Consider refactoring. Open
foreach ($task as $item) {
if (!in_array($item, array('add', 'clean', 'verify', 'remove'))) {
/* HINT: Requested object string/id/type ("album", "myusername", "some song title", 1298376) */
Api::error(sprintf('Bad Request: %s', $item), ErrorCodeEnum::BAD_REQUEST, self::ACTION, 'task', $input['api_format']);
- 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 95.
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