Skip to content

Commit fed5971

Browse files
committed
Merge branch 'feature/pear-classdeclaration-add-fixed-file' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents cc951df + 56abf7d commit fed5971

File tree

6 files changed

+227
-19
lines changed

6 files changed

+227
-19
lines changed

package.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
851851
</dir>
852852
<dir name="Tests">
853853
<dir name="Classes">
854-
<file baseinstalldir="PHP/CodeSniffer" name="ClassDeclarationUnitTest.inc" role="test" />
854+
<file baseinstalldir="PHP/CodeSniffer" name="ClassDeclarationUnitTest.1.inc" role="test" />
855+
<file baseinstalldir="PHP/CodeSniffer" name="ClassDeclarationUnitTest.1.inc.fixed" role="test" />
856+
<file baseinstalldir="PHP/CodeSniffer" name="ClassDeclarationUnitTest.2.inc" role="test" />
855857
<file baseinstalldir="PHP/CodeSniffer" name="ClassDeclarationUnitTest.php" role="test" />
856858
</dir>
857859
<dir name="Commenting">

src/Standards/PEAR/Sniffs/Classes/ClassDeclarationSniff.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function process(File $phpcsFile, $stackPtr)
7575
$phpcsFile->recordMetric($stackPtr, 'Class opening brace placement', 'new line');
7676

7777
if ($braceLine > ($classLine + 1)) {
78-
$error = 'Opening brace of a %s must be on the line following the %s declaration; found %s line(s)';
78+
$error = 'Opening brace of a %s must be on the line following the %s declaration; found %s blank line(s)';
7979
$data = [
8080
$tokens[$stackPtr]['content'],
8181
$tokens[$stackPtr]['content'],
@@ -101,9 +101,16 @@ public function process(File $phpcsFile, $stackPtr)
101101

102102
if ($tokens[($curlyBrace + 1)]['content'] !== $phpcsFile->eolChar) {
103103
$error = 'Opening %s brace must be on a line by itself';
104-
$fix = $phpcsFile->addFixableError($error, $curlyBrace, 'OpenBraceNotAlone', $errorData);
105-
if ($fix === true) {
106-
$phpcsFile->fixer->addNewline($curlyBrace);
104+
105+
$nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, ($curlyBrace + 1), null, true);
106+
if ($tokens[$nextNonWhitespace]['code'] === T_PHPCS_IGNORE) {
107+
// Don't auto-fix if the next thing is a PHPCS ignore annotation.
108+
$phpcsFile->addError($error, $curlyBrace, 'OpenBraceNotAlone', $errorData);
109+
} else {
110+
$fix = $phpcsFile->addFixableError($error, $curlyBrace, 'OpenBraceNotAlone', $errorData);
111+
if ($fix === true) {
112+
$phpcsFile->fixer->addNewline($curlyBrace);
113+
}
107114
}
108115
}
109116

@@ -112,8 +119,7 @@ public function process(File $phpcsFile, $stackPtr)
112119
if ($prevContent === $phpcsFile->eolChar) {
113120
$spaces = 0;
114121
} else {
115-
$blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
116-
$spaces = strlen($blankSpace);
122+
$spaces = $tokens[($curlyBrace - 1)]['length'];
117123
}
118124

119125
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);

src/Standards/PEAR/Tests/Classes/ClassDeclarationUnitTest.inc renamed to src/Standards/PEAR/Tests/Classes/ClassDeclarationUnitTest.1.inc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,33 @@ var_dump(new class(10) extends SomeClass implements SomeInterface {
8080

8181
use SomeTrait;
8282
});
83+
84+
class IncorrectClassDeclarationWithCommentAtEnd extends correctClassDeclaration /* Comment */ {
85+
}
86+
87+
class CorrectClassDeclarationWithCommentAtEnd extends correctClassDeclaration
88+
/* Comment */
89+
{
90+
}
91+
92+
// Don't move phpcs:ignore comments.
93+
class PHPCSIgnoreAnnotationAfterOpeningBrace
94+
{ // phpcs:ignore Standard.Cat.Sniff -- for reasons.
95+
}
96+
97+
// Moving any of the other trailing phpcs: comments is ok.
98+
class PHPCSAnnotationAfterOpeningBrace
99+
{ // phpcs:disable Standard.Cat.Sniff -- for reasons.
100+
}
101+
102+
if (!class_exists('ClassOpeningBraceShouldBeIndented')) {
103+
abstract class ClassOpeningBraceShouldBeIndented
104+
{
105+
}
106+
}
107+
108+
if (!class_exists('ClassOpeningBraceTooMuchIndentation')) {
109+
final class ClassOpeningBraceTooMuchIndentation
110+
{
111+
}
112+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
// Correct declarations.
4+
class CorrectClassDeclaration
5+
{
6+
7+
}
8+
9+
abstract class CorrectClassDeclarationWithExtends extends correctClassDeclaration
10+
{
11+
12+
}
13+
14+
final class CorrectClassDeclarationWithImplements implements correctClassDeclaration
15+
{
16+
17+
}
18+
19+
20+
// Incorrect placement of opening braces
21+
class IncorrectBracePlacement
22+
{
23+
}
24+
class IncorrectBracePlacementWithExtends extends correctClassDeclaration
25+
{
26+
}
27+
class IncorrectBracePlacementWithImplements implements correctClassDeclaration
28+
{
29+
}
30+
31+
// Incorrect code placement for opening brace.
32+
class IncorrectCodeAfterOpeningBrace
33+
{
34+
echo phpinfo();
35+
36+
}//end class
37+
38+
class IncorrectClassDeclarationWithExtends extends correctClassDeclaration
39+
{
40+
41+
}
42+
43+
class IncorrectBracePlacement
44+
{
45+
}
46+
47+
abstract class CodeSnifferFail
48+
extends
49+
ArrayObject
50+
implements
51+
Serializable,
52+
Iterator,
53+
Countable,
54+
OuterIterator,
55+
RecursiveIterator
56+
{
57+
}
58+
59+
abstract class CodeSnifferFail
60+
extends
61+
ArrayObject
62+
implements
63+
Serializable,
64+
Iterator,
65+
Countable,
66+
OuterIterator,
67+
RecursiveIterator
68+
{
69+
}
70+
71+
namespace A
72+
{
73+
class B
74+
{
75+
}
76+
}
77+
78+
$util->setLogger(new class {});
79+
80+
var_dump(new class(10) extends SomeClass implements SomeInterface {
81+
private $num;
82+
83+
public function __construct($num)
84+
{
85+
$this->num = $num;
86+
}
87+
88+
use SomeTrait;
89+
});
90+
91+
class IncorrectClassDeclarationWithCommentAtEnd extends correctClassDeclaration /* Comment */
92+
{
93+
}
94+
95+
class CorrectClassDeclarationWithCommentAtEnd extends correctClassDeclaration
96+
/* Comment */
97+
{
98+
}
99+
100+
// Don't move phpcs:ignore comments.
101+
class PHPCSIgnoreAnnotationAfterOpeningBrace
102+
{ // phpcs:ignore Standard.Cat.Sniff -- for reasons.
103+
}
104+
105+
// Moving any of the other trailing phpcs: comments is ok.
106+
class PHPCSAnnotationAfterOpeningBrace
107+
{
108+
// phpcs:disable Standard.Cat.Sniff -- for reasons.
109+
}
110+
111+
if (!class_exists('ClassOpeningBraceShouldBeIndented')) {
112+
abstract class ClassOpeningBraceShouldBeIndented
113+
{
114+
}
115+
}
116+
117+
if (!class_exists('ClassOpeningBraceTooMuchIndentation')) {
118+
final class ClassOpeningBraceTooMuchIndentation
119+
{
120+
}
121+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
if (!class_exists('ClassOpeningBraceTabIndented')) {
4+
abstract class ClassOpeningBraceTabIndented
5+
{
6+
}
7+
}
8+
9+
10+
// Needs to be last test in the file. Intentional parse error.
11+
class MyParseError extends Exception

src/Standards/PEAR/Tests/Classes/ClassDeclarationUnitTest.php

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,57 @@ class ClassDeclarationUnitTest extends AbstractSniffUnitTest
1515
{
1616

1717

18+
/**
19+
* Get a list of CLI values to set before the file is tested.
20+
*
21+
* @param string $testFile The name of the file being tested.
22+
* @param \PHP_CodeSniffer\Config $config The config data for the test run.
23+
*
24+
* @return void
25+
*/
26+
public function setCliValues($testFile, $config)
27+
{
28+
if ($testFile === 'ClassDeclarationUnitTest.1.inc') {
29+
return;
30+
}
31+
32+
$config->tabWidth = 4;
33+
34+
}//end setCliValues()
35+
36+
1837
/**
1938
* Returns the lines where errors should occur.
2039
*
2140
* The key of the array should represent the line number and the value
2241
* should represent the number of errors that should occur on that line.
2342
*
43+
* @param string $testFile The name of the file being tested.
44+
*
2445
* @return array<int, int>
2546
*/
26-
public function getErrorList()
47+
public function getErrorList($testFile='')
2748
{
28-
return [
29-
21 => 1,
30-
22 => 1,
31-
23 => 1,
32-
27 => 1,
33-
33 => 1,
34-
38 => 1,
35-
49 => 1,
36-
];
49+
switch ($testFile) {
50+
case 'ClassDeclarationUnitTest.1.inc':
51+
return [
52+
21 => 1,
53+
22 => 1,
54+
23 => 1,
55+
27 => 1,
56+
33 => 1,
57+
38 => 1,
58+
49 => 1,
59+
84 => 1,
60+
94 => 1,
61+
99 => 1,
62+
104 => 1,
63+
110 => 1,
64+
];
65+
66+
default:
67+
return [];
68+
}//end switch
3769

3870
}//end getErrorList()
3971

@@ -44,11 +76,17 @@ public function getErrorList()
4476
* The key of the array should represent the line number and the value
4577
* should represent the number of warnings that should occur on that line.
4678
*
79+
* @param string $testFile The name of the file being tested.
80+
*
4781
* @return array<int, int>
4882
*/
49-
public function getWarningList()
83+
public function getWarningList($testFile='')
5084
{
51-
return [];
85+
if ($testFile === 'ClassDeclarationUnitTest.2.inc') {
86+
return [11 => 1];
87+
}
88+
89+
return[];
5290

5391
}//end getWarningList()
5492

0 commit comments

Comments
 (0)