Method process
has 30 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function process(string $line, array &$root, string &$currentUserAgent = '*', string $prevLine = '') {
$parts = explode(':', $line);
$entry = trim($parts[1]);
$directive = $this->getDirectiveName();
Avoid unused parameters such as '$prevLine'. Open
public function process(string $line, array &$root, string &$currentUserAgent = '*', string $prevLine = '') {
- Read upRead up
- Exclude checks
UnusedFormalParameter
Since: 0.2
Avoid passing parameters to methods or constructors and then not using those parameters.
Example
class Foo
{
private function bar($howdy)
{
// $howdy is not used
}
}
Source https://phpmd.org/rules/unusedcode.html#unusedformalparameter
The method process uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$this->log(strtr('{directive} with value {faulty} skipped as already exists for {useragent}', [
'{directive}' => $directive,
'{faulty}' => $entry,
'{useragent}' => $currentUserAgent,
- 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
Define a constant instead of duplicating this literal "{useragent}" 3 times. Open
'{useragent}' => $currentUserAgent,
- 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.
Define a constant instead of duplicating this literal "{directive}" 3 times. Open
'{directive}' => $directive,
- 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.
Opening brace of a class must be on the line after the definition Open
abstract class AbstractAllowanceProcessor extends AbstractDirectiveProcessor implements DirectiveProcessorInterface {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
]));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->log(strtr('{directive} with invalid value "{faulty}" found for {useragent}, skipping', [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->log(strtr('{directive} with empty value found for {useragent}, skipping', [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
]));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$root[$currentUserAgent][$directive] = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'{useragent}' => $currentUserAgent,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!in_array($entry, $root[$currentUserAgent][$directive])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
]));
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} else {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$parts = explode(':', $line);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$root[$currentUserAgent][$directive][] = $entry;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->log(strtr('{directive} with value {faulty} skipped as already exists for {useragent}', [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function process(string $line, array &$root, string &$currentUserAgent = '*', string $prevLine = '') {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$entry = trim($parts[1]);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!preg_match("/^\//", $entry)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'{useragent}' => $currentUserAgent,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'{faulty}' => $entry,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'{directive}' => $directive,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'{useragent}' => $currentUserAgent,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'{directive}' => $directive,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$directive = $this->getDirectiveName();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'{faulty}' => $entry,
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (!isset($root[$currentUserAgent][$directive])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (empty($entry)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'{directive}' => $directive,
- Exclude checks
Opening brace should be on a new line Open
public function process(string $line, array &$root, string &$currentUserAgent = '*', string $prevLine = '') {
- Exclude checks