@@ -44,22 +44,57 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
44
44
}
45
45
}
46
46
47
- if (empty ($ tokens [$ parenthesisIndex ]['parenthesis_closer ' ])) {
48
- return ;
49
- }
50
-
51
47
$ parenthesisEndIndex = $ tokens [$ parenthesisIndex ]['parenthesis_closer ' ];
52
48
53
49
$ braceStartIndex = $ phpcsFile ->findNext (T_WHITESPACE , ($ parenthesisEndIndex + 1 ), null , true );
54
50
if ($ tokens [$ braceStartIndex ]['code ' ] !== T_OPEN_CURLY_BRACKET ) {
55
51
return ;
56
52
}
57
53
58
- if ($ braceStartIndex - $ parenthesisEndIndex === 2 && $ tokens [$ braceStartIndex - 1 ]['content ' ] === ' ' ) {
54
+ $ braceEndIndex = $ tokens [$ braceStartIndex ]['bracket_closer ' ];
55
+ $ nextContentIndex = $ phpcsFile ->findNext (T_WHITESPACE , ($ braceStartIndex + 1 ), null , true );
56
+ if ($ nextContentIndex === $ braceEndIndex ) {
57
+ $ this ->assertNoAdditionalNewlinesForEmptyBody ($ phpcsFile , $ braceStartIndex , $ braceEndIndex );
59
58
return ;
60
59
}
61
60
62
- //TODO: beginning and end of method: no newlines
61
+ if ($ tokens [$ nextContentIndex ]['line ' ] - $ tokens [$ braceStartIndex ]['line ' ] > 1 ) {
62
+ $ error = 'There should be no extra newline at beginning of a method ' ;
63
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'ContentAfterOpen ' );
64
+ if ($ fix === true ) {
65
+ $ phpcsFile ->fixer ->replaceToken ($ braceStartIndex + 1 , '' );
66
+ }
67
+ }
68
+
69
+ $ lastContentIndex = $ phpcsFile ->findPrevious (T_WHITESPACE , $ braceEndIndex - 1 , null , true );
70
+
71
+ if ($ tokens [$ braceEndIndex ]['line ' ] - $ tokens [$ lastContentIndex ]['line ' ] > 1 ) {
72
+ $ error = 'There should be no extra newline at the end of a method ' ;
73
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'ContentBeforeClose ' );
74
+ if ($ fix === true ) {
75
+ $ phpcsFile ->fixer ->replaceToken ($ lastContentIndex + 1 , '' );
76
+ }
77
+ }
78
+ }
79
+
80
+ /**
81
+ * @param \PHP_CodeSniffer_File $phpcsFile
82
+ * @param int $from
83
+ * @param int $to
84
+ *
85
+ * @return void
86
+ */
87
+ protected function assertNoAdditionalNewlinesForEmptyBody (PHP_CodeSniffer_File $ phpcsFile , $ from , $ to ) {
88
+ $ tokens = $ phpcsFile ->getTokens ();
89
+
90
+ $ startLine = $ tokens [$ from ]['line ' ];
91
+ $ endLine = $ tokens [$ to ]['line ' ];
92
+ if ($ endLine === $ startLine + 2 ) {
93
+ $ error = 'There should be no extra newline in empty methods ' ;
94
+ if ($ phpcsFile ->addFixableError ($ error , $ from , 'ContentEmpty ' )) {
95
+ $ phpcsFile ->fixer ->replaceToken ($ from + 1 , '' );
96
+ }
97
+ }
63
98
}
64
99
65
100
}
0 commit comments