Completed
Push — master ( a22340...df9c14 )
by Nils
04:26
created

PageSpeedRule::validate()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
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
class PageSpeedRule implements Rule
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