Showing 35 of 37 total issues
Controller
has 34 functions (exceeds 20 allowed). Consider refactoring. Open
class Controller extends IlluminateController
{
/**
* Maps request parameters to database columns.
*
File Controller.php
has 318 lines of code (exceeds 250 allowed). Consider refactoring. Open
<?php
namespace SehrGut\Laravel5_Api;
use Illuminate\Database\Eloquent\Builder;
The class Controller has an overall complexity of 53 which is very high. The configured complexity threshold is 50. Open
class Controller extends IlluminateController
{
/**
* Maps request parameters to database columns.
*
- Exclude checks
The class Controller has 29 non-getter- and setter-methods. Consider refactoring Controller to keep number of methods under 25. Open
class Controller extends IlluminateController
{
/**
* Maps request parameters to database columns.
*
- 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
Avoid excessively long variable names like $request_adapter_class. Keep variable name length under 20. Confirmed
protected $request_adapter_class = RequestAdapter::class;
- Read upRead up
- Exclude checks
LongVariable
Since: 0.2
Detects when a field, formal or local variable is declared with a long name.
Example
class Something {
protected $reallyLongIntName = -3; // VIOLATION - Field
public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
$otherReallyLongName = -5; // VIOLATION - Local
for ($interestingIntIndex = 0; // VIOLATION - For
$interestingIntIndex < 10;
$interestingIntIndex++ ) {
}
}
}
Source https://phpmd.org/rules/naming.html#longvariable
Function replaceWithIdsOrClear
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
protected function replaceWithIdsOrClear($model)
{
if (!$this->config['replace_with_ids']) {
// Remove all relatives from all relations on the model
$model->setRelations([]);
- 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 validateInput
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
protected function validateInput($only_present = false, $many = false)
{
$validator = $this->model_mapping->getValidatorFor($this->model);
$raw_rules = $many ? $validator::getRulesMany() : $validator::getRules();
$rules = $this->adaptRules($raw_rules);
- 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 filterByRequest
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
protected function filterByRequest($query, $mapping = null)
{
$mapping = $mapping ?: $this->key_mapping;
foreach ($mapping as $request_key => $db_key) {
- 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 validateInput has a boolean flag argument $only_present, which is a certain sign of a Single Responsibility Principle violation. Open
protected function validateInput($only_present = false, $many = 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
The method validate has a boolean flag argument $only_present, which is a certain sign of a Single Responsibility Principle violation. Confirmed
public static function validate(array $input, $rules = null, $only_present = 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
The method validateInput has a boolean flag argument $many, which is a certain sign of a Single Responsibility Principle violation. Open
protected function validateInput($only_present = false, $many = 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
The method getUrlFor has a boolean flag argument $absolute, which is a certain sign of a Single Responsibility Principle violation. Confirmed
public function getUrlFor(Model $model, $absolute = true)
- 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
Function createResource
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
protected function createResource(array $attributes = null)
{
$input = is_null($attributes) ? $this->context->input : $attributes;
$this->context->resource = new $this->model($input);
- 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 using static access to class '\Illuminate\Support\Facades\Log' in method 'adaptCollectionQuery'. Open
Log::info('TestPlugin: called adaptCollectionQuery', ['context' => $this->context]);
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class '\Illuminate\Support\Facades\Log' in method 'formatCollection'. Open
Log::info('TestPlugin: called formatCollection', ['context' => $this->context]);
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
The method loadPlugin uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Confirmed
} else {
throw new InvalidArgumentException();
}
- 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 addCondition uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Confirmed
} else {
// Search on a model attribute
$query->orWhere($field, 'ilike', "%$condition%");
}
- 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
Avoid using static access to class '\Illuminate\Support\Facades\Log' in method 'testHook'. Open
Log::info('TestPlugin: called testHook', ['context' => $this->context]);
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class '\Illuminate\Support\Facades\DB' in method 'createMany'. Open
DB::transaction(function () {
foreach ($this->context->input as $attributes) {
$this->createResource($attributes);
$this->context->collection->push($this->context->resource);
}
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid unused local variables such as '$only_present'. Open
$this->validateInput($only_present = false, $many = true);
- 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
}
}