Device
has 121 functions (exceeds 20 allowed). Consider refactoring. Open
class Device extends BaseModel
{
use PivotEventTrait, HasFactory;
public $timestamps = false;
File Device.php
has 798 lines of code (exceeds 250 allowed). Consider refactoring. Open
<?php
namespace App\Models;
use App\View\SimpleTemplate;
The class Device has 107 non-getter- and setter-methods. Consider refactoring Device to keep number of methods under 25. Open
class Device extends BaseModel
{
use PivotEventTrait, HasFactory;
public $timestamps = false;
- Read upRead up
- Exclude checks
TooManyMethods
Since: 0.1
A class with too many methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.
By default it ignores methods starting with 'get' or 'set'.
The default was changed from 10 to 25 in PHPMD 2.3.
Example
Source https://phpmd.org/rules/codesize.html#toomanymethods
The class Device has an overall complexity of 183 which is very high. The configured complexity threshold is 50. Open
class Device extends BaseModel
{
use PivotEventTrait, HasFactory;
public $timestamps = false;
- Exclude checks
The class Device has 107 public methods. Consider refactoring Device to keep number of public methods under 10. Open
class Device extends BaseModel
{
use PivotEventTrait, HasFactory;
public $timestamps = false;
- Read upRead up
- Exclude checks
TooManyPublicMethods
Since: 0.1
A class with too many public methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.
By default it ignores methods starting with 'get' or 'set'.
Example
Source https://phpmd.org/rules/codesize.html#toomanypublicmethods
Function setLocation
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
public function setLocation($new_location, bool $doLookup = false)
{
$new_location = $new_location instanceof Location ? $new_location : new Location(['location' => $new_location]);
$new_location->location = $new_location->location ? Rewrite::location($new_location->location) : null;
$coord = array_filter($new_location->only(['lat', 'lng']));
- 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
Method findByIp
has 30 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function findByIp(?string $ip): ?Device
{
if (! IP::isValid($ip)) {
return null;
}
The class Device has 122 public methods and attributes. Consider reducing the number of public items to less than 45. Open
class Device extends BaseModel
{
use PivotEventTrait, HasFactory;
public $timestamps = false;
- Read upRead up
- Exclude checks
ExcessivePublicCount
Since: 0.1
A large number of public methods and attributes declared in a class can indicate the class may need to be broken up as increased effort will be required to thoroughly test it.
Example
public class Foo {
public $value;
public $something;
public $var;
// [... more more public attributes ...]
public function doWork() {}
public function doMoreWork() {}
public function doWorkAgain() {}
// [... more more public methods ...]
}
Source https://phpmd.org/rules/codesize.html#excessivepubliccount
Function findByIp
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public static function findByIp(?string $ip): ?Device
{
if (! IP::isValid($ip)) {
return 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 null;
Avoid too many return
statements within this method. Open
return $query->where('device_id', $deviceSpec);
Avoid too many return
statements within this method. Open
return $query->where('hostname', $deviceSpec);
Avoid too many return
statements within this method. Open
return $query->where('hostname', 'like', str_replace('*', '%', $deviceSpec));
Function updateMaxDepth
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public function updateMaxDepth($exclude = null)
{
// optimize for memory instead of time
$query = $this->parents()->getQuery();
if (! is_null($exclude)) {
- 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 scopeWhereDeviceSpec
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public function scopeWhereDeviceSpec(Builder $query, ?string $deviceSpec): Builder
{
if (empty($deviceSpec)) {
return $query;
} elseif ($deviceSpec == 'all') {
- 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 class Device has 1012 lines of code. Current threshold is 1000. Avoid really long classes. Open
class Device extends BaseModel
{
use PivotEventTrait, HasFactory;
public $timestamps = false;
- Exclude checks
The method setLocation() has a Cyclomatic Complexity of 12. The configured cyclomatic complexity threshold is 10. Open
public function setLocation($new_location, bool $doLookup = false)
{
$new_location = $new_location instanceof Location ? $new_location : new Location(['location' => $new_location]);
$new_location->location = $new_location->location ? Rewrite::location($new_location->location) : null;
$coord = array_filter($new_location->only(['lat', 'lng']));
- 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
The class Device has a coupling between objects value of 97. Consider to reduce the number of dependencies under 13. Open
class Device extends BaseModel
{
use PivotEventTrait, HasFactory;
public $timestamps = false;
- Read upRead up
- Exclude checks
CouplingBetweenObjects
Since: 1.1.0
A class with too many dependencies has negative impacts on several quality aspects of a class. This includes quality criteria like stability, maintainability and understandability
Example
class Foo {
/**
* @var \foo\bar\X
*/
private $x = null;
/**
* @var \foo\bar\Y
*/
private $y = null;
/**
* @var \foo\bar\Z
*/
private $z = null;
public function setFoo(\Foo $foo) {}
public function setBar(\Bar $bar) {}
public function setBaz(\Baz $baz) {}
/**
* @return \SplObjectStorage
* @throws \OutOfRangeException
* @throws \InvalidArgumentException
* @throws \ErrorException
*/
public function process(\Iterator $it) {}
// ...
}
Source https://phpmd.org/rules/design.html#couplingbetweenobjects
The method formatDownUptime has a boolean flag argument $short, which is a certain sign of a Single Responsibility Principle violation. Open
public function formatDownUptime($short = false): string
- Read upRead up
- Exclude checks
BooleanArgumentFlag
Since: 1.4.0
A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.
Example
class Foo {
public function bar($flag = true) {
}
}
Source https://phpmd.org/rules/cleancode.html#booleanargumentflag
The method setLocation has a boolean flag argument $doLookup, which is a certain sign of a Single Responsibility Principle violation. Open
public function setLocation($new_location, bool $doLookup = false)
- Read upRead up
- Exclude checks
BooleanArgumentFlag
Since: 1.4.0
A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.
Example
class Foo {
public function bar($flag = true) {
}
}
Source https://phpmd.org/rules/cleancode.html#booleanargumentflag
Remove error control operator '@' on line 473. Open
public function getIpAttribute($ip): ?string
{
if (empty($ip)) {
return null;
}
- Read upRead up
- Exclude checks
ErrorControlOperator
Error suppression should be avoided if possible as it doesn't just suppress the error, that you are trying to stop, but will also suppress errors that you didn't predict would ever occur. Consider changing error_reporting() level and/or setting up your own error handler.
Example
function foo($filePath) {
$file = @fopen($filPath); // hides exceptions
$key = @$array[$notExistingKey]; // assigns null to $key
}
Source http://phpmd.org/rules/cleancode.html#errorcontroloperator
The method updateMaxDepth uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$parents_max_depth = $query->max('max_depth');
$this->max_depth = $parents_max_depth + 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 pollerTarget uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
return $device['hostname'];
}
- 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 updateMaxDepth uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$this->max_depth = 1; // has children
}
- 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
FIXME found Open
// FIXME does not include global read
- Exclude checks
Avoid variables with short names like $ip. Configured minimum length is 3. Open
public function setIpAttribute($ip): void
- 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
Avoid variables with short names like $ip. Configured minimum length is 3. Open
public function getIpAttribute($ip): ?string
- 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
Avoid variables with short names like $ip. Configured minimum length is 3. Open
public static function findByIp(?string $ip): ?Device
- 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
Only one argument is allowed per line in a multi-line function call Open
$query->qualifyColumn('device_id'), function ($query) use ($deviceGroup) {
- Exclude checks
Only one argument is allowed per line in a multi-line function call Open
$query->qualifyColumn('device_id'), function ($query) use ($serviceTemplate) {
- Exclude checks
Only one argument is allowed per line in a multi-line function call Open
$query->qualifyColumn('device_id'), function ($query) use ($serviceTemplate) {
- Exclude checks
Only one argument is allowed per line in a multi-line function call Open
$query->qualifyColumn('device_id'), function ($query) use ($deviceGroup) {
- Exclude checks