__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 60
ccs 8
cts 8
cp 1
rs 9.0472
cc 1
nc 1
nop 0
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace HtaccessCapabilityTester\Testers;
4
5
/**
6
 * Class for testing if an environment variable can be set in a rewrite rule and received in PHP.
7
 *
8
 * @package    HtaccessCapabilityTester
9
 * @author     Bjørn Rosell <[email protected]>
10
 * @since      Class available since 0.7
11
 */
12
class PassInfoFromRewriteToScriptThroughEnvTester extends CustomTester
13
{
14
15
    /**
16
     * Constructor.
17
     *
18
     * @return void
19
     */
20 6
    public function __construct()
21
    {
22
        $htaccessFile = <<<'EOD'
23 6
<IfModule mod_rewrite.c>
24
25
    # Testing if we can pass environment variable from .htaccess to script in a RewriteRule
26
    # We pass document root, because that can easily be checked by the script
27
28
    RewriteEngine On
29
    RewriteRule ^test\.php$ - [E=PASSTHROUGHENV:%{DOCUMENT_ROOT},L]
30
31
</IfModule>
32
EOD;
33
34
        $phpFile = <<<'EOD'
35 6
<?php
36
37
/**
38
 *  Get environment variable set with mod_rewrite module
39
 *  Return false if the environment variable isn't found
40
 */
41
function getEnvPassedInRewriteRule($envName) {
42
    // Environment variables passed through the REWRITE module have "REWRITE_" as a prefix
43
    // (in Apache, not Litespeed, if I recall correctly).
44
    // Multiple iterations causes multiple REWRITE_ prefixes, and we get many environment variables set.
45
    // We simply look for an environment variable that ends with what we are looking for.
46
    // (so make sure to make it unique)
47
    $len = strlen($envName);
48
    foreach ($_SERVER as $key => $item) {
49
        if (substr($key, -$len) == $envName) {
50
            return $item;
51
        }
52
    }
53
    return false;
54
}
55
56
$result = getEnvPassedInRewriteRule('PASSTHROUGHENV');
57
if ($result === false) {
58
    echo '0';
59
    exit;
60
}
61
echo ($result == $_SERVER['DOCUMENT_ROOT'] ? '1' : '0');
62
EOD;
63
64
        $test = [
65 6
            'subdir' => 'pass-info-from-rewrite-to-script-through-env',
66
            'files' => [
67 6
                ['.htaccess', $htaccessFile],
68 6
                ['test.php', $phpFile],
69
            ],
70 6
            'request' => 'test.php',
71
            'interpretation' => [
72
                ['success', 'body', 'equals', '1'],
73
                ['failure', 'body', 'equals', '0'],
74
                ['inconclusive', 'body', 'begins-with', '<' . '?php'],
75
                ['inconclusive']
76
            ]
77
        ];
78
79 6
        parent::__construct($test);
80 6
    }
81
}
82