The class Plugin has an overall complexity of 57 which is very high. The configured complexity threshold is 50. Open
class Plugin
{
/** @var null|string $name */
public $name;
public $_plugin;
- Exclude checks
Function get_plugins
has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring. Open
public static function get_plugins($type = ''): array
{
// make static cache for optimization when multiple call
static $plugins_list = array();
if (isset($plugins_list[$type])) {
- 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 update_all
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
public static function update_all(): void
{
foreach (PluginEnum::LIST as $name => $className) {
$plugin = new Plugin($name);
$installed_version = self::get_plugin_version($plugin->_plugin->name);
- 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 is_valid
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
public function is_valid(): bool
{
if ($this->_plugin === null) {
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 is_update_available
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public static function is_update_available(): bool
{
foreach (PluginEnum::LIST as $name => $className) {
$plugin = new Plugin($name);
if ($plugin->_plugin !== null) {
- 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;
Avoid too many return
statements within this method. Open
return false;
Avoid too many return
statements within this method. Open
return false;
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 is_valid() has an NPath complexity of 1024. The configured NPath complexity threshold is 200. Open
public function is_valid(): bool
{
if ($this->_plugin === null) {
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 is_valid() has a Cyclomatic Complexity of 11. The configured cyclomatic complexity threshold is 10. Open
public function is_valid(): bool
{
if ($this->_plugin === null) {
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
Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed. Open
public static function get_plugins($type = ''): array
- 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
Reduce the number of returns of this function 10, down to the maximum allowed 3. Open
public function is_valid(): 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; }
Avoid assigning values to variables in if clauses and the like (line '261', column '13'). Open
public static function get_plugin_version($plugin_name): int
{
$name = Dba::escape('Plugin_' . $plugin_name);
$sql = "SELECT `key`, `value` FROM `update_info` WHERE `key` = ?";
$db_results = Dba::read($sql, array($name));
- Read upRead up
- Exclude checks
IfStatementAssignment
Since: 2.7.0
Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.
Example
class Foo
{
public function bar($flag)
{
if ($foo = 'bar') { // possible typo
// ...
}
if ($baz = 0) { // always false
// ...
}
}
}
Source http://phpmd.org/rules/cleancode.html#ifstatementassignment
Define a constant instead of duplicating this literal "upgrade" 3 times. Open
if (!method_exists($this->_plugin, 'upgrade')) {
- 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.
Merge this if statement with the enclosing one. Open
if ($this->_plugin->uninstall()) {
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if (condition1) { if (condition2) { ... } }
Compliant Solution
if (condition1 && condition2) { ... }
Merge this if statement with the enclosing one. Open
if ($this->_plugin->upgrade()) {
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if (condition1) { if (condition2) { ... } }
Compliant Solution
if (condition1 && condition2) { ... }
Rename "$name" which has the same name as the field declared at line 35. Open
$name = Dba::escape('Plugin_' . $this->_plugin->name);
- Read upRead up
- Exclude checks
Shadowing fields with a local variable is a bad practice that reduces code readability: it makes it confusing to know whether the field or the variable is being used.
Noncompliant Code Example
class Foo { public $myField; public function doSomething() { $myField = 0; ... } }
See
- CERT, DCL51-J. - Do not shadow or obscure identifiers in subscopes
Merge this if statement with the enclosing one. Open
if ($plugin->_plugin !== null && method_exists($plugin->_plugin, 'upgrade')) {
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if (condition1) { if (condition2) { ... } }
Compliant Solution
if (condition1 && condition2) { ... }
Define a constant instead of duplicating this literal "Plugin_" 3 times. Open
$name = Dba::escape('Plugin_' . $plugin_name);
- 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.
Merge this if statement with the enclosing one. Open
if ($plugin->_plugin->upgrade()) {
- Read upRead up
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if (condition1) { if (condition2) { ... } }
Compliant Solution
if (condition1 && condition2) { ... }
Avoid unused local variables such as '$className'. Open
foreach (PluginEnum::LIST as $name => $className) {
- 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
Avoid unused local variables such as '$className'. Open
foreach (PluginEnum::LIST as $name => $className) {
- 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
Avoid unused local variables such as '$className'. Open
foreach (PluginEnum::LIST as $name => $className) {
- 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
TODO found Open
// TODO mark upgrade as required for Ampache 7+
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
$this->set_plugin_version($this->_plugin->version);
- Exclude checks
Call to method load
on non-class type null
Open
return $this->_plugin->load($user);
- Exclude checks
Argument 1 (object)
is null
but \method_exists()
takes object|string
Open
if ($plugin->_plugin !== null && method_exists($plugin->_plugin, 'upgrade')) {
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
if ($installed_version > 0 && $installed_version < $plugin->_plugin->version) {
- Exclude checks
Argument 1 (object)
is null
but \method_exists()
takes object|string
Open
if (!method_exists($this->_plugin, 'install')) {
- Exclude checks
Call to method set_preferences
from undeclared class \Ampache\Repository\Model\User
Open
$user->set_preferences();
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
$installed_version = self::get_plugin_version($plugin->_plugin->name);
- Exclude checks
Call to method upgrade
on non-class type null
Open
if ($plugin->_plugin->upgrade()) {
- Exclude checks
Call to method uninstall
on non-class type null
Open
if ($this->_plugin->uninstall()) {
- Exclude checks
Argument 1 (object)
is null
but \method_exists()
takes object|string
Open
if (!method_exists($plugin->_plugin, $type)) {
- Exclude checks
Argument 1 (object)
is null
but \method_exists()
takes object|string
Open
if (!method_exists($this->_plugin, 'load')) {
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
if ($db_version > $this->_plugin->max_ampache) {
- Exclude checks
Argument 1 (object)
is null
but \method_exists()
takes object|string
Open
if ($this->_plugin !== null && method_exists($this->_plugin, 'uninstall')) {
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
if (!Plugin::is_installed($plugin->_plugin->name)) {
- Exclude checks
Call to deprecated function \debug_event()
defined at /code/src/Config/functions.php:651
Open
debug_event(__CLASS__, 'Plugin ' . $name . ' is not valid, skipping', 6);
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
if (!strlen((string)$this->_plugin->version)) {
- Exclude checks
Call to deprecated function \debug_event()
defined at /code/src/Config/functions.php:651
Open
debug_event(__CLASS__, 'WARNING: Plugin missing upgrade method. ' . $this->_plugin->name . '`.', 1);
- Exclude checks
Parameter $user
has undeclared type \Ampache\Repository\Model\User
Open
public function load($user): bool
- Exclude checks
Call to method escape
from undeclared class \Ampache\Module\System\Dba
Open
$name = Dba::escape('Plugin_' . $plugin_name);
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
$installed_version = self::get_plugin_version($plugin->_plugin->name);
- Exclude checks
Assigning object
to property but \Ampache\Repository\Model\Plugin->_plugin
is null
Open
$this->_plugin = new $controller();
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
if (!strlen((string)$this->_plugin->name)) {
- Exclude checks
Argument 1 (object)
is null
but \method_exists()
takes object|string
Open
if (!method_exists($this->_plugin, 'uninstall')) {
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
debug_event(__CLASS__, 'WARNING: Plugin missing upgrade method. ' . $this->_plugin->name . '`.', 1);
- Exclude checks
Call to method read
from undeclared class \Ampache\Module\System\Dba
Open
$db_results = Dba::read($sql, array($name));
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
if ($installed_version > 0 && $installed_version < $plugin->_plugin->version) {
- Exclude checks
Argument 1 (object)
is null
but \method_exists()
takes object|string
Open
if ($this->_plugin !== null && method_exists($this->_plugin, 'upgrade')) {
- Exclude checks
Call to deprecated function \debug_event()
defined at /code/src/Config/functions.php:651
Open
debug_event(__CLASS__, 'Cannot find plugin `' . $cname . '`.', 1);
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
if (!strlen((string)$this->_plugin->description)) {
- Exclude checks
Call to method fetch_assoc
from undeclared class \Ampache\Module\System\Dba
Open
if ($results = Dba::fetch_assoc($db_results)) {
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
$name = Dba::escape('Plugin_' . $this->_plugin->name);
- Exclude checks
Argument 1 (object)
is null
but \method_exists()
takes object|string
Open
if (!method_exists($this->_plugin, 'upgrade')) {
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
$name = Dba::escape('Plugin_' . $this->_plugin->name);
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
$this->set_plugin_version($this->_plugin->version);
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
if ($db_version < $this->_plugin->min_ampache) {
- Exclude checks
Call to method install
on non-class type null
Open
if ($this->_plugin !== null && $this->_plugin->install()) {
- Exclude checks
Call to method upgrade
on non-class type null
Open
if ($this->_plugin->upgrade()) {
- Exclude checks
Call to method escape
from undeclared class \Ampache\Module\System\Dba
Open
$name = Dba::escape('Plugin_' . $this->_plugin->name);
- Exclude checks
Expected an object instance when accessing an instance property, but saw an expression with type null
Open
$plugin->set_plugin_version($plugin->_plugin->version);
- Exclude checks
Call to method read
from undeclared class \Ampache\Module\System\Dba
Open
$db_results = Dba::read($sql);
- Exclude checks
Call to method write
from undeclared class \Ampache\Module\System\Dba
Open
Dba::write($sql, array($name, $version));
- Exclude checks
Call to method write
from undeclared class \Ampache\Module\System\Dba
Open
Dba::write($sql);
- Exclude checks
Call to method fetch_assoc
from undeclared class \Ampache\Module\System\Dba
Open
$results = Dba::fetch_assoc($db_results);
- Exclude checks
Call to method escape
from undeclared class \Ampache\Module\System\Dba
Open
$name = Dba::escape('Plugin_' . $this->_plugin->name);
- Exclude checks
Call to method escape
from undeclared class \Ampache\Module\System\Dba
Open
$version = (int)Dba::escape($version);
- Exclude checks
The parameter $plugin_name is not named in camelCase. Open
public static function get_plugin_version($plugin_name): int
{
$name = Dba::escape('Plugin_' . $plugin_name);
$sql = "SELECT `key`, `value` FROM `update_info` WHERE `key` = ?";
$db_results = Dba::read($sql, array($name));
- 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 $plugin_name is not named in camelCase. Open
public static function is_installed($plugin_name): int
{
/* All we do is check the version */
return self::get_plugin_version($plugin_name);
}
- 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
Property name "$_plugin" should not be prefixed with an underscore to indicate visibility Open
public $_plugin;
- Exclude checks