• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 17662127818

12 Sep 2025 01:50AM UTC coverage: 78.786%. Remained the same
17662127818

push

github

web-flow
Merge pull request #1241 from PHPCSStandards/phpcs-4.x/feature/155-normalize-some-code-style-rules-3

CS: normalize code style rules [3]

343 of 705 new or added lines in 108 files covered. (48.65%)

3 existing lines in 3 files now uncovered.

19732 of 25045 relevant lines covered (78.79%)

96.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.89
/src/Standards/PSR2/Sniffs/Methods/MethodDeclarationSniff.php
1
<?php
2
/**
3
 * Checks that the method declaration is correct.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8
 */
9

10
namespace PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
14
use PHP_CodeSniffer\Util\Tokens;
15

16
class MethodDeclarationSniff extends AbstractScopeSniff
17
{
18

19

20
    /**
21
     * Constructs a Squiz_Sniffs_Scope_MethodScopeSniff.
22
     */
23
    public function __construct()
3✔
24
    {
25
        parent::__construct(Tokens::OO_SCOPE_TOKENS, [T_FUNCTION]);
3✔
26

27
    }//end __construct()
1✔
28

29

30
    /**
31
     * Processes the function tokens within the class.
32
     *
33
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
34
     * @param int                         $stackPtr  The position where the token was found.
35
     * @param int                         $currScope The current scope opener token.
36
     *
37
     * @return void
38
     */
39
    protected function processTokenWithinScope(File $phpcsFile, int $stackPtr, int $currScope)
3✔
40
    {
41
        $tokens = $phpcsFile->getTokens();
3✔
42

43
        // Determine if this is a function which needs to be examined.
44
        $conditions = $tokens[$stackPtr]['conditions'];
3✔
45
        end($conditions);
3✔
46
        $deepestScope = key($conditions);
3✔
47
        if ($deepestScope !== $currScope) {
3✔
48
            return;
3✔
49
        }
50

51
        $methodName = $phpcsFile->getDeclarationName($stackPtr);
3✔
52
        if ($methodName === '') {
3✔
53
            // Ignore live coding.
54
            return;
3✔
55
        }
56

57
        if ($methodName[0] === '_' && isset($methodName[1]) === true && $methodName[1] !== '_') {
3✔
58
            $error = 'Method name "%s" should not be prefixed with an underscore to indicate visibility';
3✔
59
            $data  = [$methodName];
3✔
60
            $phpcsFile->addWarning($error, $stackPtr, 'Underscore', $data);
3✔
61
        }
62

63
        $visibility = 0;
3✔
64
        $static     = 0;
3✔
65
        $abstract   = 0;
3✔
66
        $final      = 0;
3✔
67

68
        $find = (Tokens::METHOD_MODIFIERS + Tokens::EMPTY_TOKENS);
3✔
69
        $prev = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
3✔
70

71
        $prefix = $stackPtr;
3✔
72
        while (($prefix = $phpcsFile->findPrevious(Tokens::METHOD_MODIFIERS, ($prefix - 1), $prev)) !== false) {
3✔
73
            switch ($tokens[$prefix]['code']) {
3✔
74
            case T_STATIC:
3✔
75
                $static = $prefix;
3✔
76
                break;
3✔
77
            case T_ABSTRACT:
3✔
78
                $abstract = $prefix;
3✔
79
                break;
3✔
80
            case T_FINAL:
3✔
81
                $final = $prefix;
3✔
82
                break;
3✔
83
            default:
84
                $visibility = $prefix;
3✔
85
                break;
3✔
86
            }
87
        }
88

89
        $fixes = [];
3✔
90

91
        if ($visibility !== 0 && $final > $visibility) {
3✔
92
            $error = 'The final declaration must precede the visibility declaration';
3✔
93
            $fix   = $phpcsFile->addFixableError($error, $final, 'FinalAfterVisibility');
3✔
94
            if ($fix === true) {
3✔
95
                $fixes[$final]       = '';
3✔
96
                $fixes[($final + 1)] = '';
3✔
97
                if (isset($fixes[$visibility]) === true) {
3✔
NEW
98
                    $fixes[$visibility] = 'final ' . $fixes[$visibility];
×
99
                } else {
100
                    $fixes[$visibility] = 'final ' . $tokens[$visibility]['content'];
3✔
101
                }
102
            }
103
        }
104

105
        if ($visibility !== 0 && $abstract > $visibility) {
3✔
106
            $error = 'The abstract declaration must precede the visibility declaration';
3✔
107
            $fix   = $phpcsFile->addFixableError($error, $abstract, 'AbstractAfterVisibility');
3✔
108
            if ($fix === true) {
3✔
109
                $fixes[$abstract]       = '';
3✔
110
                $fixes[($abstract + 1)] = '';
3✔
111
                if (isset($fixes[$visibility]) === true) {
3✔
112
                    $fixes[$visibility] = 'abstract ' . $fixes[$visibility];
3✔
113
                } else {
114
                    $fixes[$visibility] = 'abstract ' . $tokens[$visibility]['content'];
3✔
115
                }
116
            }
117
        }
118

119
        if ($static !== 0 && $static < $visibility) {
3✔
120
            $error = 'The static declaration must come after the visibility declaration';
3✔
121
            $fix   = $phpcsFile->addFixableError($error, $static, 'StaticBeforeVisibility');
3✔
122
            if ($fix === true) {
3✔
123
                $fixes[$static]       = '';
3✔
124
                $fixes[($static + 1)] = '';
3✔
125
                if (isset($fixes[$visibility]) === true) {
3✔
126
                    $fixes[$visibility] .= ' static';
3✔
127
                } else {
128
                    $fixes[$visibility] = $tokens[$visibility]['content'] . ' static';
3✔
129
                }
130
            }
131
        }
132

133
        // Batch all the fixes together to reduce the possibility of conflicts.
134
        if (empty($fixes) === false) {
3✔
135
            $phpcsFile->fixer->beginChangeset();
3✔
136
            foreach ($fixes as $stackPtr => $content) {
3✔
137
                $phpcsFile->fixer->replaceToken($stackPtr, $content);
3✔
138
            }
139

140
            $phpcsFile->fixer->endChangeset();
3✔
141
        }
142

143
    }//end processTokenWithinScope()
1✔
144

145

146
    /**
147
     * Processes a token that is found within the scope that this test is
148
     * listening to.
149
     *
150
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
151
     * @param int                         $stackPtr  The position in the stack where this
152
     *                                               token was found.
153
     *
154
     * @return void
155
     */
156
    protected function processTokenOutsideScope(File $phpcsFile, int $stackPtr)
×
157
    {
158

159
    }//end processTokenOutsideScope()
×
160

161

162
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc