The class Rating has an overall complexity of 62 which is very high. The configured complexity threshold is 50. Open
class Rating extends database_object
{
protected const DB_TABLENAME = 'rating';
private const RATING_TYPES = array(
'artist',
- Exclude checks
Function build_cache
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
public static function build_cache($type, $ids, $user_id = null): bool
{
if (empty($ids)) {
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 save_rating
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
public static function save_rating($object_id, $object_type, $new_rating, $user_id): void
{
$rating = new Rating($object_id, $object_type);
$user = new User($user_id);
if ($rating->id) {
- 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 show
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
public static function show($object_id, $type, $show_global_rating = false): string
{
// If ratings aren't enabled don't do anything
if (!AmpConfig::get('ratings') || !in_array($type, self::RATING_TYPES)) {
return '';
- 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 garbage_collection
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public static function garbage_collection($object_type = null, $object_id = null): void
{
$types = array(
'album',
'album_disk',
- 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 get_highest_sql
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public static function get_highest_sql($input_type, $user_id = null): string
{
$type = Stats::validate_type($input_type);
$user_id = (int)($user_id);
$sql = "SELECT MAX(`id`) AS `table_id`, MIN(`rating`.`object_id`) AS `id`, ROUND(AVG(`rating`.`rating`), 2) AS `rating`, COUNT(DISTINCT(`rating`.`user`)) AS `count` FROM `rating`";
- 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
The method garbage_collection uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
foreach ($types as $type) {
Dba::write("DELETE FROM `rating` WHERE `object_type` = '$type' AND `rating`.`object_id` NOT IN (SELECT `$type`.`id` FROM `$type`);");
}
}
- 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
The method show uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
/* HINT: object rating */
$ratedText = sprintf(T_('%s of 5'), $rate);
}
- 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
The method build_cache uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$rating = (int)$user_ratings[$object_id];
}
- 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
The method garbage_collection uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
debug_event(self::class, 'Garbage collect on type `' . $object_type . '` is not supported.', 1);
}
- 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
The method set_rating uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$sql = "REPLACE INTO `rating` (`object_id`, `object_type`, `rating`, `user`, `date`) VALUES (?, ?, ?, ?, ?)";
$params = array($this->id, $this->type, $rating, $user_id, $time);
static::getUserActivityPoster()->post((int) $user_id, 'rating', $this->type, (int) $this->id, $time);
- 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
The method build_cache uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$rating = round($ratings[$object_id], 1);
}
- 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
syntax error, unexpected 'int' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
Open
public int $id; // The object_id of the object rated
- Exclude checks
Similar blocks of code found in 2 locations. Consider refactoring. Open
if ($rating->id) {
foreach (Plugin::get_plugins('save_rating') as $plugin_name) {
try {
$plugin = new Plugin($plugin_name);
if ($plugin->_plugin !== null && $plugin->load($user)) {
- 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 129.
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
The parameter $object_type is not named in camelCase. Open
public static function save_rating($object_id, $object_type, $new_rating, $user_id): void
{
$rating = new Rating($object_id, $object_type);
$user = new User($user_id);
if ($rating->id) {
- Read upRead up
- Exclude checks
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 $user_id is not named in camelCase. Open
public static function get_highest($input_type, $count = 0, $offset = 0, $user_id = null): array
{
if ($count === 0) {
$count = AmpConfig::get('popular_threshold', 10);
}
- Read upRead up
- Exclude checks
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 show($object_id, $type, $show_global_rating = false): string
{
// If ratings aren't enabled don't do anything
if (!AmpConfig::get('ratings') || !in_array($type, self::RATING_TYPES)) {
return '';
- Read upRead up
- Exclude checks
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 $user_id is not named in camelCase. Open
public static function get_highest_sql($input_type, $user_id = null): string
{
$type = Stats::validate_type($input_type);
$user_id = (int)($user_id);
$sql = "SELECT MAX(`id`) AS `table_id`, MIN(`rating`.`object_id`) AS `id`, ROUND(AVG(`rating`.`rating`), 2) AS `rating`, COUNT(DISTINCT(`rating`.`user`)) AS `count` FROM `rating`";
- Read upRead up
- Exclude checks
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 $old_object_id is not named in camelCase. Open
public static function migrate($object_type, $old_object_id, $new_object_id)
{
$sql = "UPDATE IGNORE `rating` SET `object_id` = ? WHERE `object_type` = ? AND `object_id` = ?";
return Dba::write($sql, array($new_object_id, $object_type, $old_object_id));
- Read upRead up
- Exclude checks
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 $rating_id is not named in camelCase. Open
public function __construct($rating_id, $type)
{
$this->id = (int)$rating_id;
$this->type = $type;
}
- Read upRead up
- Exclude checks
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 $user_id is not named in camelCase. Open
public static function build_cache($type, $ids, $user_id = null): bool
{
if (empty($ids)) {
return false;
}
- Read upRead up
- Exclude checks
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 $user_id is not named in camelCase. Open
public static function save_rating($object_id, $object_type, $new_rating, $user_id): void
{
$rating = new Rating($object_id, $object_type);
$user = new User($user_id);
if ($rating->id) {
- Read upRead up
- Exclude checks
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 $user_id is not named in camelCase. Open
public function set_rating($rating, $user_id = null): bool
{
if ($user_id === null) {
$user = Core::get_global('user');
$user_id = $user->id ?? 0;
- Read upRead up
- Exclude checks
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 $show_global_rating is not named in camelCase. Open
public static function show($object_id, $type, $show_global_rating = false): string
{
// If ratings aren't enabled don't do anything
if (!AmpConfig::get('ratings') || !in_array($type, self::RATING_TYPES)) {
return '';
- Read upRead up
- Exclude checks
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 $user_id is not named in camelCase. Open
public function get_user_rating($user_id = null): ?int
{
if ($user_id === null) {
$user = Core::get_global('user');
$user_id = $user->id ?? 0;
- Read upRead up
- Exclude checks
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 $new_object_id is not named in camelCase. Open
public static function migrate($object_type, $old_object_id, $new_object_id)
{
$sql = "UPDATE IGNORE `rating` SET `object_id` = ? WHERE `object_type` = ? AND `object_id` = ?";
return Dba::write($sql, array($new_object_id, $object_type, $old_object_id));
- Read upRead up
- Exclude checks
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
Avoid variables with short names like $id. Configured minimum length is 3. Open
public int $id; // The object_id of the object rated
- 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 parameter $object_type is not named in camelCase. Open
public static function garbage_collection($object_type = null, $object_id = null): void
{
$types = array(
'album',
'album_disk',
- Read upRead up
- Exclude checks
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 save_rating($object_id, $object_type, $new_rating, $user_id): void
{
$rating = new Rating($object_id, $object_type);
$user = new User($user_id);
if ($rating->id) {
- Read upRead up
- Exclude checks
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 $input_type is not named in camelCase. Open
public static function get_highest_sql($input_type, $user_id = null): string
{
$type = Stats::validate_type($input_type);
$user_id = (int)($user_id);
$sql = "SELECT MAX(`id`) AS `table_id`, MIN(`rating`.`object_id`) AS `id`, ROUND(AVG(`rating`.`rating`), 2) AS `rating`, COUNT(DISTINCT(`rating`.`user`)) AS `count` FROM `rating`";
- Read upRead up
- Exclude checks
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 $new_rating is not named in camelCase. Open
public static function save_rating($object_id, $object_type, $new_rating, $user_id): void
{
$rating = new Rating($object_id, $object_type);
$user = new User($user_id);
if ($rating->id) {
- Read upRead up
- Exclude checks
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 $input_type is not named in camelCase. Open
public static function get_highest($input_type, $count = 0, $offset = 0, $user_id = null): array
{
if ($count === 0) {
$count = AmpConfig::get('popular_threshold', 10);
}
- Read upRead up
- Exclude checks
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 garbage_collection($object_type = null, $object_id = null): void
{
$types = array(
'album',
'album_disk',
- Read upRead up
- Exclude checks
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 migrate($object_type, $old_object_id, $new_object_id)
{
$sql = "UPDATE IGNORE `rating` SET `object_id` = ? WHERE `object_type` = ? AND `object_id` = ?";
return Dba::write($sql, array($new_object_id, $object_type, $old_object_id));
- Read upRead up
- Exclude checks
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) {
}
}