Completed
Push — adapting-test-suite-for-change... ( f1ec16 )
by Alexander
02:34
created

WrongParentCallSniff   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
B process() 0 20 5
1
<?php
2
/**
3
 * CodingStandard_Sniffs_CodeAnalysis_WrongParentCallSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category PHP
8
 * @package  PHP_CodeSniffer
9
 * @author   Peter Philipp <[email protected]>
10
 * @author   Alexander Obuhovich <[email protected]>
11
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
12
 * @link     https://github.com/aik099/CodingStandard
13
 */
14
15
/**
16
 * CodingStandard_Sniffs_CodeAnalysis_WrongParentCallSniff.
17
 *
18
 * Checks that method is invoking it's own parent and not other function.
19
 *
20
 * Correct:
21
 * function nameOne() {
22
 *     parent::nameOne();
23
 *
24
 *     ....
25
 * }
26
 *
27
 * Wrong:
28
 * function nameOne() {
29
 *     parent::nameTwo();
30
 *
31
 *     ....
32
 * }
33
 *
34
 * @category PHP
35
 * @package  PHP_CodeSniffer
36
 * @author   Alexander Obuhovich <[email protected]>
37
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
38
 * @link     https://github.com/aik099/CodingStandard
39
 */
40
class CodingStandard_Sniffs_CodeAnalysis_WrongParentCallSniff implements PHP_CodeSniffer_Sniff
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
41
{
42
43
44
    /**
45
     * Returns an array of tokens this test wants to listen for.
46
     *
47
     * @return integer[]
48
     */
49 1
    public function register()
50
    {
51 1
        return array(T_PARENT);
52
53
    }//end register()
54
55
56
    /**
57
     * Processes this test, when one of its tokens is encountered.
58
     *
59
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
60
     * @param int                  $stackPtr  The position of the current token in the
61
     *                                        stack passed in $tokens.
62
     *
63
     * @return void
64
     */
65 1
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
66
    {
67 1
        $functionPtr = $phpcsFile->findPrevious(T_FUNCTION, ($stackPtr - 1));
68
69 1
        if ($functionPtr !== false) {
70 1
            $doubleColonPtr = $phpcsFile->findNext(T_DOUBLE_COLON, ($stackPtr + 1));
71
72 1
            if ($doubleColonPtr !== false) {
73 1
                $tokens        = $phpcsFile->getTokens();
74 1
                $functionName  = $phpcsFile->getDeclarationName($functionPtr);
0 ignored issues
show
Bug introduced by
It seems like $functionPtr defined by $phpcsFile->findPrevious...UNCTION, $stackPtr - 1) on line 67 can also be of type boolean; however, PHP_CodeSniffer_File::getDeclarationName() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
75 1
                $methodNamePtr = $phpcsFile->findNext(T_STRING, ($stackPtr + 1));
76
77 1
                if ($methodNamePtr !== false && $tokens[$methodNamePtr]['content'] !== $functionName) {
78 1
                    $error = 'Method name mismatch in parent:: call';
79 1
                    $phpcsFile->addError($error, $stackPtr, 'WrongName');
80 1
                }
81 1
            }
82 1
        }//end if
83
84 1
    }//end process()
85
86
87
}//end class
88