Completed
Push — master ( ffd8fa...8034c6 )
by Nils
06:36
created

RobotsDisallowAllRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 5
c 4
b 1
f 2
lcom 0
cbo 2
dl 26
loc 26
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 23 23 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace whm\Smoke\Rules\Seo;
4
5
use whm\Smoke\Http\Response;
6
use whm\Smoke\Rules\Rule;
7
use whm\Smoke\Rules\ValidationFailedException;
8
9
/**
10
 * This rule checks if a given json file is valid.
11
 */
12 View Code Duplication
class RobotsDisallowAllRule implements Rule
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
{
14
    public function validate(Response $response)
15
    {
16
        $url = (string) $response->getUri();
17
18
        if (substr_count($url, '/') === 2) {
19
            $filename = $robotsUrl = $url . '/robots.txt';
0 ignored issues
show
Unused Code introduced by
$robotsUrl is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
20
        } elseif (substr_count($url, '/') === 3) {
21
            $filename = $robotsUrl = $url . 'robots.txt';
0 ignored issues
show
Unused Code introduced by
$robotsUrl is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
22
        } else {
23
            return;
24
        }
25
26
        if(!file_exists($filename) ){
27
            return;
28
        }
29
30
        $content = file_get_contents($filename);
31
        $normalizedContent = str_replace(' ', '', $content);
32
33
        if (strpos($normalizedContent, 'Disallow:/' . PHP_EOL) !== false) {
34
            throw new ValidationFailedException('The robots.txt contains disallow all (Disallow: /)');
35
        }
36
    }
37
}
38