Completed
Push — master ( 7cb383...bf8a98 )
by Alexander
02:20
created

ClassDeclarationSniff::processOpen()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 2
dl 0
loc 33
ccs 0
cts 28
cp 0
crap 42
rs 8.7697
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 36 and the first side effect is on line 18.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * CodingStandard_Sniffs_Classes_ClassDeclarationSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category PHP
8
 * @package  PHP_CodeSniffer
9
 * @author   Greg Sherwood <[email protected]>
10
 * @author   Marc McIntyre <[email protected]>
11
 * @author   Alexander Obuhovich <[email protected]>
12
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
13
 * @link     https://github.com/aik099/CodingStandard
14
 */
15
16
// @codeCoverageIgnoreStart
17
if (class_exists('PSR2_Sniffs_Classes_ClassDeclarationSniff', true) === false) {
18
    $error = 'Class PSR2_Sniffs_Classes_ClassDeclarationSniff not found';
19
    throw new PHP_CodeSniffer_Exception($error);
20
}
21
// @codeCoverageIgnoreEnd
22
23
/**
24
 * Class Declaration Test.
25
 *
26
 * Checks the declaration of the class and its inheritance is correct.
27
 *
28
 * @category PHP
29
 * @package  PHP_CodeSniffer
30
 * @author   Greg Sherwood <[email protected]>
31
 * @author   Marc McIntyre <[email protected]>
32
 * @author   Alexander Obuhovich <[email protected]>
33
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
34
 * @link     https://github.com/aik099/CodingStandard
35
 */
36
class CodingStandard_Sniffs_Classes_ClassDeclarationSniff extends PSR2_Sniffs_Classes_ClassDeclarationSniff
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...
37
{
38
39
40
    /**
41
     * Processes this test, when one of its tokens is encountered.
42
     *
43
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
44
     * @param int                  $stackPtr  The position of the current token
45
     *                                         in the stack passed in $tokens.
46
     *
47
     * @return void
48
     */
49
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
50
    {
51
        // We want all the errors from the PEAR standard, plus some of our own.
52
        parent::process($phpcsFile, $stackPtr);
53
54
    }//end process()
55
56
57
    /**
58
     * Processes the opening section of a class declaration.
59
     *
60
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
61
     * @param int                  $stackPtr  The position of the current token
62
     *                                        in the stack passed in $tokens.
63
     *
64
     * @return void
65
     */
66
    public function processOpen(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
67
    {
68
        parent::processOpen($phpcsFile, $stackPtr);
69
70
        $tokens = $phpcsFile->getTokens();
71
72
        if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) {
73
            $prevContent = $tokens[($stackPtr - 1)]['content'];
74
            if ($prevContent !== $phpcsFile->eolChar) {
75
                $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
76
                $spaces     = strlen($blankSpace);
77
78
                if (in_array($tokens[($stackPtr - 2)]['code'], array(T_ABSTRACT, T_FINAL)) === false) {
79
                    if ($spaces !== 0) {
80
                        $type  = strtolower($tokens[$stackPtr]['content']);
81
                        $error = 'Expected 0 spaces before %s keyword; %s found';
82
                        $data  = array(
83
                                  $type,
84
                                  $spaces,
85
                                 );
86
87
                        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeKeyword', $data);
88
                        if ($fix === true) {
89
                            $phpcsFile->fixer->beginChangeset();
90
                            $phpcsFile->fixer->replaceToken(($stackPtr - 1), '');
91
                            $phpcsFile->fixer->endChangeset();
92
                        }
93
                    }
94
                }
95
            }//end if
96
        }//end if
97
98
    }//end processOpen()
99
100
101
    /**
102
     * Processes the closing section of a class declaration.
103
     *
104
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
105
     * @param int                  $stackPtr  The position of the current token
106
     *                                        in the stack passed in $tokens.
107
     *
108
     * @return void
109
     */
110
    public function processClose(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
111
    {
112
        $tokens = $phpcsFile->getTokens();
113
114
        // Just in case.
115
        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
116
            return;
117
        }
118
119
        $closeBrace = $tokens[$stackPtr]['scope_closer'];
120
        if ($tokens[($closeBrace - 1)]['code'] === T_WHITESPACE) {
121
            $prevContent = $tokens[($closeBrace - 1)]['content'];
122
            if ($prevContent !== $phpcsFile->eolChar) {
123
                $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
124
                $spaces     = strlen($blankSpace);
125
                if ($spaces !== 0) {
126
                    if ($tokens[($closeBrace - 1)]['line'] !== $tokens[$closeBrace]['line']) {
127
                        $error = 'Expected 0 spaces before closing brace; newline found';
128
                        $fix   = $phpcsFile->addFixableError($error, $closeBrace, 'NewLineBeforeCloseBrace');
129
                    } else {
130
                        $error = 'Expected 0 spaces before closing brace; %s found';
131
                        $data  = array($spaces);
132
                        $fix   = $phpcsFile->addFixableError($error, $closeBrace, 'SpaceBeforeCloseBrace', $data);
133
                    }//end if
134
135
                    if ($fix === true) {
136
                        $phpcsFile->fixer->beginChangeset();
137
                        $phpcsFile->fixer->replaceToken(($closeBrace - 1), '');
138
                        $phpcsFile->fixer->endChangeset();
139
                    }
140
                }//end if
141
            }//end if
142
        }//end if
143
144
        // Check that the closing brace has one blank line after it.
145
        $nextContent = $phpcsFile->findNext(array(T_WHITESPACE), ($closeBrace + 1), null, true);
146
        if ($nextContent !== false) {
147
            $difference = ($tokens[$nextContent]['line'] - $tokens[$closeBrace]['line'] - 1);
148
            if ($difference < 0) {
149
                $difference = 0;
150
            }
151
152
            if ($difference !== 1) {
153
                $error = 'Closing brace of a %s must be followed by a single blank line; found %s';
154
                $data  = array(
155
                          $tokens[$stackPtr]['content'],
156
                          $difference,
157
                         );
158
                $fix   = $phpcsFile->addFixableError($error, $closeBrace, 'NewlinesAfterCloseBrace', $data);
159
                if ($fix === true) {
160
                    $phpcsFile->fixer->beginChangeset();
161
162
                    if ($difference > 1) {
163
                        for ($i = ($closeBrace + 1); $i < $nextContent; $i++) {
164
                            if ($tokens[$i]['line'] === $tokens[$nextContent]['line']) {
165
                                // Keep existing indentation.
166
                                break;
167
                            }
168
169
                            $phpcsFile->fixer->replaceToken($i, '');
170
                        }
171
                    }
172
173
                    $phpcsFile->fixer->addNewline($closeBrace);
174
                    $phpcsFile->fixer->endChangeset();
175
                }
176
            }//end if
177
        }//end if
178
179
    }//end processClose()
180
181
182
}//end class
183