Completed
Pull Request — master (#178)
by Juliette
02:41
created

process()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 30
rs 6.7272
cc 7
eloc 15
nc 7
nop 2
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
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...
24
{
25
26
    /**
27
     * Returns an array of tokens this test wants to listen for.
28
     *
29
     * @return array
30
     */
31
    public function register()
32
    {
33
        return array(T_FUNCTION);
34
35
    }//end register()
36
37
    /**
38
     * Processes this test, when one of its tokens is encountered.
39
     *
40
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
41
     * @param int                  $stackPtr  The position of the current token
42
     *                                        in the stack passed in $tokens.
43
     *
44
     * @return void
45
     */
46
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
47
    {
48
        if ($this->supportsAbove('7.0') === false) {
49
            return;
50
        }
51
52
        $tokens = $phpcsFile->getTokens();
53
        $token  = $tokens[$stackPtr];
54
        // Skip function without body.
55
        if (isset($token['scope_opener']) === false) {
56
            return;
57
        }
58
59
        // Get all parameters from method signature.
60
        $parameters = $phpcsFile->getMethodParameters($stackPtr);
61
        if (empty($parameters) || is_array($parameters) === false) {
62
            return;
63
        }
64
65
66
        $paramNames = array();
67
        foreach ($parameters as $param) {
68
            $paramNames[] = strtolower($param['name']);
69
        }
70
71
        if (count($paramNames) != count(array_unique($paramNames))) {
72
            $phpcsFile->addError('Functions can not have multiple parameters with the same name since PHP 7.0', $stackPtr);
73
        }
74
75
    }//end process()
76
77
}//end class
78