Completed
Push — php70 ( 83abde...b6aa8a )
by Wim
05:28 queued 02:49
created

ForbiddenFunctionParametersWithSameNameSniff   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 56
rs 10
wmc 6
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
B process() 0 24 5
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_ForbiddenFunctionParametersWithSameName.
4
 *
5
 * PHP version 7.0
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_ForbiddenFunctionParametersWithSameName.
14
 *
15
 * Functions can not have multiple parameters with the same name since PHP 7.0
16
 *
17
 * PHP version 7.0
18
 *
19
 * @category  PHP
20
 * @package   PHPCompatibility
21
 * @author    Wim Godden <[email protected]>
22
 */
23
class PHPCompatibility_Sniffs_PHP_ForbiddenFunctionParametersWithSameNameSniff extends PHPCompatibility_Sniff
24
{
25
26
    /**
27
     * If true, an error will be thrown; otherwise a warning.
28
     *
29
     * @var bool
30
     */
31
    protected $error = true;
32
33
    /**
34
     * Returns an array of tokens this test wants to listen for.
35
     *
36
     * @return array
37
     */
38
    public function register()
39
    {
40
        return array(T_FUNCTION);
41
42
    }//end register()
43
44
    /**
45
     * Processes this test, when one of its tokens is encountered.
46
     *
47
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
48
     * @param int                  $stackPtr  The position of the current token
49
     *                                        in the stack passed in $tokens.
50
     *
51
     * @return void
52
     */
53
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
54
    {
55
        if ($this->supportsAbove('7.0')) {
56
            $tokens = $phpcsFile->getTokens();
57
            $token  = $tokens[$stackPtr];
58
            // Skip function without body.
59
            if (isset($token['scope_opener']) === false) {
60
                return;
61
            }
62
63
            // Get function name.
64
            $methodName = $phpcsFile->getDeclarationName($stackPtr);
0 ignored issues
show
Unused Code introduced by
$methodName 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...
65
            
66
            // Get all parameters from method signature.
67
            $paramNames = array();
68
            foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
69
                $paramNames[] = strtolower($param['name']);
70
            }
71
            
72
            if (count($paramNames) != count(array_unique($paramNames))) {
73
                $phpcsFile->addError('Functions can not have multiple parameters with the same name since PHP 7.0', $stackPtr);
74
            }
75
        }
76
    }//end process()
77
78
}//end class
79