Completed
Pull Request — master (#176)
by Juliette
02:44
created

ParameterShadowSuperGlobalsSniffTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 68
Duplicated Lines 14.71 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 1
dl 10
loc 68
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testParameterShadowSuperGlobal() 10 10 1
A dataParameterShadowSuperGlobals() 0 15 1
A testValidParameter() 0 4 1

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
 * ParameterShadowSuperGlobalsSniffTest
4
 *
5
 * @package PHPCompatibility
6
 */
7
8
9
/**
10
 * ParameterShadowSuperGlobalsSniffTest
11
 *
12
 * @uses    BaseSniffTest
13
 * @package PHPCompatibility
14
 * @author  Juliette Reinders Folmer <[email protected]>
15
 */
16
class ParameterShadowSuperGlobalsSniffTest extends BaseSniffTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
19
    const TEST_FILE = 'sniff-examples/parameter_shadow_superglobals.php';
20
21
    /**
22
     * testParameterShadowSuperGlobals
23
     *
24
     * @group parameterShadowSuperGlobals
25
     *
26
     * @dataProvider dataParameterShadowSuperGlobals
27
     *
28
     * @param int    $line  Line number where the error should occur.
29
     * @param string $octal (Start of) Binary number as a string.
0 ignored issues
show
Bug introduced by
There is no parameter named $octal. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
30
     * @param bool   $testNoViolation Whether or not to test for noViolation.
0 ignored issues
show
Bug introduced by
There is no parameter named $testNoViolation. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
31
     *               Defaults to true. Set to false if another error is
32
     *               expected on the same line (invalid binary)
33
     *
34
     * @return void
35
     */
36 View Code Duplication
    public function testParameterShadowSuperGlobal($superglobal, $line)
0 ignored issues
show
Duplication introduced by
This method 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...
37
    {
38
39
        $file = $this->sniffFile(self::TEST_FILE, '5.3');
40
        $this->assertNoViolation($file, $line);
41
42
43
        $file = $this->sniffFile(self::TEST_FILE, '5.4');
44
        $this->assertError($file, $line, "Parameter shadowing super global ({$superglobal}) causes fatal error since PHP 5.4");
45
    }
46
47
    /**
48
     * dataParameterShadowSuperGlobals
49
     *
50
     * @see testParameterShadowSuperGlobals()
51
     *
52
     * @return array
53
     */
54
    public function dataParameterShadowSuperGlobals() {
55
        return array(
56
            array('$GLOBALS', 4),
57
            array('$_SERVER', 5),
58
            array('$_GET', 6),
59
            array('$_POST', 7),
60
            array('$_FILES', 8),
61
            array('$_COOKIE', 9),
62
            array('$_SESSION', 10),
63
            array('$_REQUEST', 11),
64
            array('$_ENV', 12),
65
            array('$globals', 15),
66
            array('$_post', 16),
67
        );
68
    }
69
70
71
    /**
72
     * testValidParameter
73
     *
74
     * @group parameterShadowSuperGlobals
75
     *
76
     * @return void
77
     */
78
    public function testValidParameter() {
79
        $file = $this->sniffFile(self::TEST_FILE);
80
        $this->assertNoViolation($file , 19);
81
    }
82
83
}
84