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

RobotsDisallowAllRule::validate()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 23
Code Lines 14

Duplication

Lines 23
Ratio 100 %

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 23
loc 23
rs 8.5907
cc 5
eloc 14
nc 7
nop 1
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