ampache/ampache

View on GitHub
src/Repository/Model/Random.php

Summary

Maintainability
B
6 hrs
Test Coverage

Function advanced_results has a Cognitive Complexity of 31 (exceeds 5 allowed). Consider refactoring.
Open

    private static function advanced_results($sql_query, $sql_params, $data): array
    {
        // Run the query generated above so we can while it
        $db_results = Dba::read($sql_query, $sql_params);
        $results    = [];
Severity: Minor
Found in src/Repository/Model/Random.php - About 4 hrs to fix

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

The class Random has an overall complexity of 74 which is very high. The configured complexity threshold is 50.
Open

class Random
{
    public const VALID_TYPES = array(
        'song',
        'album',
Severity: Minor
Found in src/Repository/Model/Random.php by phpmd

Function advanced_sql has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
Open

    private static function advanced_sql($data, $type, $limit_sql): array
    {
        $search = new Search(0, $type);
        $search->set_rules($data);

Severity: Minor
Found in src/Repository/Model/Random.php - About 1 hr to fix

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 get_artist has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
Open

    public static function get_artist($limit, $user = null): array
    {
        $results = array();

        if (empty($user)) {
Severity: Minor
Found in src/Repository/Model/Random.php - About 25 mins to fix

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

The method advanced_results() has an NPath complexity of 244. The configured NPath complexity threshold is 200.
Open

    private static function advanced_results($sql_query, $sql_params, $data): array
    {
        // Run the query generated above so we can while it
        $db_results = Dba::read($sql_query, $sql_params);
        $results    = [];
Severity: Minor
Found in src/Repository/Model/Random.php by phpmd

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 advanced_sql() has a Cyclomatic Complexity of 15. The configured cyclomatic complexity threshold is 10.
Open

    private static function advanced_sql($data, $type, $limit_sql): array
    {
        $search = new Search(0, $type);
        $search->set_rules($data);

Severity: Minor
Found in src/Repository/Model/Random.php by phpmd

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

The method advanced_results() has a Cyclomatic Complexity of 14. The configured cyclomatic complexity threshold is 10.
Open

    private static function advanced_results($sql_query, $sql_params, $data): array
    {
        // Run the query generated above so we can while it
        $db_results = Dba::read($sql_query, $sql_params);
        $results    = [];
Severity: Minor
Found in src/Repository/Model/Random.php by phpmd

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 19 to the 15 allowed.
Open

    private static function advanced_sql($data, $type, $limit_sql): array
Severity: Critical
Found in src/Repository/Model/Random.php by sonar-php

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 4, down to the maximum allowed 3.
Open

    public static function get_songs($type, $results): array
Severity: Major
Found in src/Repository/Model/Random.php by sonar-php

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 28 to the 15 allowed.
Open

    private static function advanced_results($sql_query, $sql_params, $data): array
Severity: Critical
Found in src/Repository/Model/Random.php by sonar-php

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

Call to method fetch_assoc from undeclared class \Ampache\Module\System\Dba
Open

        $results    = Dba::fetch_assoc($db_results);
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to deprecated function \debug_event() defined at /code/src/Config/functions.php:651
Open

        debug_event(self::class, $user->id . " doesn't have access to search:" . $search_id, 5);
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Call to method fetch_assoc from undeclared class \Ampache\Module\System\Dba
Open

        $results    = Dba::fetch_assoc($db_results);
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Reference to instance property type from undeclared class \Ampache\Repository\Model\Search
Open

        if ($search->has_access($user->id) || $search->type == 'public') {
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Call to deprecated function \Ampache\Repository\Model\Random::getSongRepository() defined at /code/src/Repository/Model/Random.php:460
Open

                    $songs = array_merge($songs, static::getSongRepository()->getByAlbum($object_id));
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Parameter $user has undeclared type \Ampache\Repository\Model\User
Open

    public static function get_single_song($random_type, $user, $object_id = 0): int
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Parameter $user has undeclared type \Ampache\Repository\Model\User
Open

    public static function get_playlist($user, $playlist_id = 0): array
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Reference to instance property id from undeclared class \Ampache\Repository\Model\User
Open

        if ($search->has_access($user->id) || $search->type == 'public') {
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Call to method get_catalogs from undeclared class \Ampache\Repository\Model\Catalog
Open

        $sql = "SELECT `song`.`id` FROM `song` WHERE `song`.`catalog` IN (" . implode(',', Catalog::get_catalogs('', $user_id, true)) . ") ";
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method fetch_assoc from undeclared class \Ampache\Module\System\Dba
Open

        while ($row = Dba::fetch_assoc($db_results)) {
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method read from undeclared class \Ampache\Module\System\Dba
Open

        $db_results = Dba::read($sql);
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method escape from undeclared class \Ampache\Module\System\Dba
Open

        $limit_sql = "LIMIT " . Dba::escape($limit);
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to deprecated function \Ampache\Repository\Model\Random::getSongRepository() defined at /code/src/Repository/Model/Random.php:460
Open

                    $songs = array_merge($songs, static::getSongRepository()->getByArtist($object_id));
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Call to method __construct from undeclared class \Ampache\Repository\Model\Search
Open

        $search = new Search(0, $type);
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method read from undeclared class \Ampache\Module\System\Dba
Open

        $db_results = Dba::read($sql);
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method fetch_assoc from undeclared class \Ampache\Module\System\Dba
Open

        while ($row = Dba::fetch_assoc($db_results)) {
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method set_rules from undeclared class \Ampache\Repository\Model\Search
Open

        $search->set_rules($data);
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method read from undeclared class \Ampache\Module\System\Dba
Open

        $db_results = Dba::read($sql);
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method read from undeclared class \Ampache\Module\System\Dba
Open

        $db_results = Dba::read($sql);
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Parameter $user has undeclared type \Ampache\Repository\Model\User
Open

    public static function get_search(User $user, $search_id = 0): array
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Checking instanceof against undeclared class \Ampache\Repository\Model\User
Open

        $user_id = ($user instanceof User) ? $user->id : null;
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method fetch_assoc from undeclared class \Ampache\Module\System\Dba
Open

        while ($row = Dba::fetch_assoc($db_results)) {
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method get_catalogs from undeclared class \Ampache\Repository\Model\Catalog
Open

        $sql     = "SELECT `artist`.`id` FROM `artist` LEFT JOIN `catalog_map` ON `catalog_map`.`object_type` = 'artist' AND `catalog_map`.`object_id` = `artist`.`id` WHERE `catalog_map`.`catalog_id` IN (" . implode(',', Catalog::get_catalogs('', $user_id, true)) . ") ";
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Parameter $user has undeclared type \Ampache\Repository\Model\User
Open

    public static function get_artist($limit, $user = null): array
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Reference to instance property id from undeclared class \Ampache\Repository\Model\User
Open

        if ($playlist->has_access($user->id)) {
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Call to undeclared method \Ampache\Repository\Model\Playlist::has_access
Open

        if ($playlist->has_access($user->id)) {
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method has_access from undeclared class \Ampache\Repository\Model\Search
Open

        if ($search->has_access($user->id) || $search->type == 'public') {
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Reference to instance property id from undeclared class \Ampache\Repository\Model\User
Open

        debug_event(self::class, $user->id . " doesn't have access to search:" . $search_id, 5);
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Call to method get_catalogs from undeclared class \Ampache\Repository\Model\Catalog
Open

        $sql     = "SELECT `song`.`id` FROM `song` WHERE `song`.`catalog` IN (" . implode(',', Catalog::get_catalogs('', $user_id, true)) . ") ";
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Reference to instance property id from undeclared class \Ampache\Repository\Model\User
Open

        $user_id = ($user instanceof User) ? $user->id : null;
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Call to method get_random_items from undeclared class \Ampache\Repository\Model\Search
Open

            foreach ($search->get_random_items('1') as $songs) {
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Parameter $user has undeclared type \Ampache\Repository\Model\User
Open

    public static function get_default($limit, $user = null): array
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Checking instanceof against undeclared class \Ampache\Repository\Model\User
Open

        if ($rating_filter > 0 && $rating_filter <= 5 && $user instanceof User) {
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method read from undeclared class \Ampache\Module\System\Dba
Open

        $db_results = Dba::read($sql_query, $sql_params);
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Checking instanceof against undeclared class \Ampache\Repository\Model\User
Open

        if ($rating_filter > 0 && $rating_filter <= 5 && Core::get_global('user') instanceof User && Core::get_global('user')->id > 0) {
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Checking instanceof against undeclared class \Ampache\Repository\Model\User
Open

        if (!$user instanceof User) {
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method __construct from undeclared class \Ampache\Repository\Model\Search
Open

        $search  = new Search($search_id, 'song', $user);
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Call to method to_sql from undeclared class \Ampache\Repository\Model\Search
Open

        $search_info     = $search->to_sql();
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Reference to instance property id from undeclared class \Ampache\Repository\Model\User
Open

        $user_id   = $user->id;
Severity: Minor
Found in src/Repository/Model/Random.php by phan

Call to method get_recently_played from undeclared class \Ampache\Repository\Model\User
Open

        $data      = $user->get_recently_played('artist', 1);
Severity: Critical
Found in src/Repository/Model/Random.php by phan

Define a constant instead of duplicating this literal "where_sql" 6 times.
Open

                if (!empty($search_info['where_sql'])) {
Severity: Critical
Found in src/Repository/Model/Random.php by sonar-php

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.

Remove this commented out code.
Open

        //debug_event(self::class, "get_default " . $sql , 5);
Severity: Major
Found in src/Repository/Model/Random.php by sonar-php

Programmers should not comment out code as it bloats programs and reduces readability.

Unused code should be deleted and can be retrieved from source control history if required.

See

  • MISRA C:2004, 2.4 - Sections of code should not be "commented out".
  • MISRA C++:2008, 2-7-2 - Sections of code shall not be "commented out" using C-style comments.
  • MISRA C++:2008, 2-7-3 - Sections of code should not be "commented out" using C++ comments.
  • MISRA C:2012, Dir. 4.4 - Sections of code should not be "commented out"

Define a constant instead of duplicating this literal "limit" 3 times.
Open

        $limit     = (int)($data['limit'] ?? -1);
Severity: Critical
Found in src/Repository/Model/Random.php by sonar-php

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 "artist" 5 times.
Open

        'artist',
Severity: Critical
Found in src/Repository/Model/Random.php by sonar-php

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 "length" 4 times.
Open

        $length     = (array_key_exists('length', $data) && $data['length'] > 0);
Severity: Critical
Found in src/Repository/Model/Random.php by sonar-php

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 ($type) {
Severity: Critical
Found in src/Repository/Model/Random.php by sonar-php

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

Remove this commented out code.
Open

        //debug_event(self::class, 'advanced ' . print_r($search, true), 5);
Severity: Major
Found in src/Repository/Model/Random.php by sonar-php

Programmers should not comment out code as it bloats programs and reduces readability.

Unused code should be deleted and can be retrieved from source control history if required.

See

  • MISRA C:2004, 2.4 - Sections of code should not be "commented out".
  • MISRA C++:2008, 2-7-2 - Sections of code shall not be "commented out" using C-style comments.
  • MISRA C++:2008, 2-7-3 - Sections of code should not be "commented out" using C++ comments.
  • MISRA C:2012, Dir. 4.4 - Sections of code should not be "commented out"

Define a constant instead of duplicating this literal "parameters" 3 times.
Open

        $results = self::advanced_results($search['sql'], $search['parameters'], $data);
Severity: Critical
Found in src/Repository/Model/Random.php by sonar-php

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 "video" 3 times.
Open

        'video'
Severity: Critical
Found in src/Repository/Model/Random.php by sonar-php

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 "size_limit" 4 times.
Open

        $size_limit = (array_key_exists('size_limit', $data) && $data['size_limit'] > 0);
Severity: Critical
Found in src/Repository/Model/Random.php by sonar-php

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 "table_sql" 4 times.
Open

                if (!empty($search_info['table_sql'])) {
Severity: Critical
Found in src/Repository/Model/Random.php by sonar-php

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 "album" 3 times.
Open

        'album',
Severity: Critical
Found in src/Repository/Model/Random.php by sonar-php

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.

Remove this commented out code.
Open

        //debug_event(__CLASS__, "get_single_song:" . $song, 5);
Severity: Major
Found in src/Repository/Model/Random.php by sonar-php

Programmers should not comment out code as it bloats programs and reduces readability.

Unused code should be deleted and can be retrieved from source control history if required.

See

  • MISRA C:2004, 2.4 - Sections of code should not be "commented out".
  • MISRA C++:2008, 2-7-2 - Sections of code shall not be "commented out" using C-style comments.
  • MISRA C++:2008, 2-7-3 - Sections of code should not be "commented out" using C++ comments.
  • MISRA C:2012, Dir. 4.4 - Sections of code should not be "commented out"

The parameter $playlist_id is not named in camelCase.
Open

    public static function get_playlist($user, $playlist_id = 0): array
    {
        $results  = array();
        $playlist = new Playlist($playlist_id);
        if ($playlist->has_access($user->id)) {
Severity: Minor
Found in src/Repository/Model/Random.php by phpmd

CamelCaseParameterName

Since: 0.2

It is considered best practice to use the camelCase notation to name parameters.

Example

class ClassName {
    public function doSomething($user_name) {
    }
}

Source

The parameter $object_id is not named in camelCase.
Open

    public static function get_single_song($random_type, $user, $object_id = 0): int
    {
        switch ($random_type) {
            case 'artist':
                $song_ids = self::get_artist(1, $user);
Severity: Minor
Found in src/Repository/Model/Random.php by phpmd

CamelCaseParameterName

Since: 0.2

It is considered best practice to use the camelCase notation to name parameters.

Example

class ClassName {
    public function doSomething($user_name) {
    }
}

Source

The parameter $object_type is not named in camelCase.
Open

    public static function get_play_url($object_type, $object_id): string
    {
        $user = Core::get_global('user');
        $link = Stream::get_base_url(false, $user->streamtoken) . 'uid=' . scrub_out((string)$user->id) . '&random=1&random_type=' . scrub_out($object_type) . '&random_id=' . scrub_out((string)$object_id);

Severity: Minor
Found in src/Repository/Model/Random.php by phpmd

CamelCaseParameterName

Since: 0.2

It is considered best practice to use the camelCase notation to name parameters.

Example

class ClassName {
    public function doSomething($user_name) {
    }
}

Source

The parameter $sql_query is not named in camelCase.
Open

    private static function advanced_results($sql_query, $sql_params, $data): array
    {
        // Run the query generated above so we can while it
        $db_results = Dba::read($sql_query, $sql_params);
        $results    = [];
Severity: Minor
Found in src/Repository/Model/Random.php by phpmd

CamelCaseParameterName

Since: 0.2

It is considered best practice to use the camelCase notation to name parameters.

Example

class ClassName {
    public function doSomething($user_name) {
    }
}

Source

The parameter $limit_sql is not named in camelCase.
Open

    private static function advanced_sql($data, $type, $limit_sql): array
    {
        $search = new Search(0, $type);
        $search->set_rules($data);

Severity: Minor
Found in src/Repository/Model/Random.php by phpmd

CamelCaseParameterName

Since: 0.2

It is considered best practice to use the camelCase notation to name parameters.

Example

class ClassName {
    public function doSomething($user_name) {
    }
}

Source

The parameter $random_type is not named in camelCase.
Open

    public static function get_single_song($random_type, $user, $object_id = 0): int
    {
        switch ($random_type) {
            case 'artist':
                $song_ids = self::get_artist(1, $user);
Severity: Minor
Found in src/Repository/Model/Random.php by phpmd

CamelCaseParameterName

Since: 0.2

It is considered best practice to use the camelCase notation to name parameters.

Example

class ClassName {
    public function doSomething($user_name) {
    }
}

Source

The parameter $search_id is not named in camelCase.
Open

    public static function get_search(User $user, $search_id = 0): array
    {
        $results = array();
        $search  = new Search($search_id, 'song', $user);
        if ($search->has_access($user->id) || $search->type == 'public') {
Severity: Minor
Found in src/Repository/Model/Random.php by phpmd

CamelCaseParameterName

Since: 0.2

It is considered best practice to use the camelCase notation to name parameters.

Example

class ClassName {
    public function doSomething($user_name) {
    }
}

Source

The parameter $sql_params is not named in camelCase.
Open

    private static function advanced_results($sql_query, $sql_params, $data): array
    {
        // Run the query generated above so we can while it
        $db_results = Dba::read($sql_query, $sql_params);
        $results    = [];
Severity: Minor
Found in src/Repository/Model/Random.php by phpmd

CamelCaseParameterName

Since: 0.2

It is considered best practice to use the camelCase notation to name parameters.

Example

class ClassName {
    public function doSomething($user_name) {
    }
}

Source

The parameter $object_id is not named in camelCase.
Open

    public static function get_play_url($object_type, $object_id): string
    {
        $user = Core::get_global('user');
        $link = Stream::get_base_url(false, $user->streamtoken) . 'uid=' . scrub_out((string)$user->id) . '&random=1&random_type=' . scrub_out($object_type) . '&random_id=' . scrub_out((string)$object_id);

Severity: Minor
Found in src/Repository/Model/Random.php by phpmd

CamelCaseParameterName

Since: 0.2

It is considered best practice to use the camelCase notation to name parameters.

Example

class ClassName {
    public function doSomething($user_name) {
    }
}

Source

There are no issues that match your filters.

Category
Status