Skip to content

Commit a75353b

Browse files
SONARPHP-1569 S1448 should not raise on test classes (#1339)
1 parent b7ff27d commit a75353b

File tree

12 files changed

+22
-126
lines changed

12 files changed

+22
-126
lines changed

its/ruling/src/integrationTest/resources/expected/PHPMailer/php-S1448.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@
44
],
55
"PHPMailer:src/SMTP.php": [
66
31
7-
],
8-
"PHPMailer:test/PHPMailer/PHPMailerTest.php": [
9-
24
107
]
118
}

its/ruling/src/integrationTest/resources/expected/PHPWord/php-S1448.json

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,5 @@
5555
],
5656
"PHPWord:src/PhpWord/TemplateProcessor.php": [
5757
33
58-
],
59-
"PHPWord:tests/PhpWordTests/Metadata/SettingsTest.php": [
60-
31
61-
],
62-
"PHPWord:tests/PhpWordTests/Shared/HtmlTest.php": [
63-
36
64-
],
65-
"PHPWord:tests/PhpWordTests/Style/TextBoxTest.php": [
66-
30
67-
],
68-
"PHPWord:tests/PhpWordTests/TemplateProcessorTest.php": [
69-
39
70-
],
71-
"PHPWord:tests/PhpWordTests/Writer/Word2007/ElementTest.php": [
72-
31
73-
],
74-
"PHPWord:tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php": [
75-
33
76-
],
77-
"PHPWord:tests/PhpWordTests/Writer/Word2007/Part/SettingsTest.php": [
78-
33
7958
]
8059
}

its/ruling/src/integrationTest/resources/expected/PHP_CodeSniffer/php-S1448.json

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,5 @@
88
"PHP_CodeSniffer:src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.inc": [
99
3,
1010
63
11-
],
12-
"PHP_CodeSniffer:tests/Core/ErrorSuppressionTest.php": [
13-
17
14-
],
15-
"PHP_CodeSniffer:tests/Core/File/FindEndOfStatementTest.php": [
16-
14
17-
],
18-
"PHP_CodeSniffer:tests/Core/File/FindStartOfStatementTest.php": [
19-
14
20-
],
21-
"PHP_CodeSniffer:tests/Core/File/GetMethodParametersTest.php": [
22-
14
23-
],
24-
"PHP_CodeSniffer:tests/Core/File/GetMethodPropertiesTest.php": [
25-
14
26-
],
27-
"PHP_CodeSniffer:tests/Core/Tokenizer/BackfillFnTokenTest.php": [
28-
14
2911
]
3012
}

its/ruling/src/integrationTest/resources/expected/PhpSpreadsheet/php-S1448.json

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -151,41 +151,5 @@
151151
],
152152
"PhpSpreadsheet:src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php": [
153153
23
154-
],
155-
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Calculation/CalculationTest.php": [
156-
15
157-
],
158-
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Cell/CoordinateTest.php": [
159-
12
160-
],
161-
"PhpSpreadsheet:tests/PhpSpreadsheetTests/IOFactoryTest.php": [
162-
14
163-
],
164-
"PhpSpreadsheet:tests/PhpSpreadsheetTests/ReferenceHelperTest.php": [
165-
18
166-
],
167-
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Shared/DateTest.php": [
168-
14
169-
],
170-
"PhpSpreadsheet:tests/PhpSpreadsheetTests/SpreadsheetTest.php": [
171-
12
172-
],
173-
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Style/ConditionalFormatting/CellMatcherTest.php": [
174-
15
175-
],
176-
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterTest.php": [
177-
12
178-
],
179-
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Worksheet/Table/TableTest.php": [
180-
16
181-
],
182-
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Worksheet/Worksheet2Test.php": [
183-
12
184-
],
185-
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php": [
186-
18
187-
],
188-
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Writer/Xlsx/ConditionalTest.php": [
189-
15
190154
]
191155
}

its/ruling/src/integrationTest/resources/expected/RubixML/php-S1448.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,5 @@
77
],
88
"RubixML:src/Datasets/Unlabeled.php": [
99
31
10-
],
11-
"RubixML:tests/Datasets/LabeledTest.php": [
12-
22
13-
],
14-
"RubixML:tests/Datasets/UnlabeledTest.php": [
15-
21
1610
]
1711
}
Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
{
2-
"flysystem:src/AdapterTestUtilities/FilesystemAdapterTestCase.php": [
3-
35
4-
],
52
"flysystem:src/AsyncAwsS3/AsyncAwsS3Adapter.php": [
63
46
74
],
8-
"flysystem:src/AsyncAwsS3/AsyncAwsS3AdapterTest.php": [
9-
34
10-
],
115
"flysystem:src/AwsS3V3/AwsS3V3Adapter.php": [
126
42
137
],
14-
"flysystem:src/AwsS3V3/AwsS3V3AdapterTest.php": [
15-
33
16-
],
178
"flysystem:src/AwsS3V3/S3ClientStub.php": [
189
22
1910
],
@@ -23,36 +14,21 @@
2314
"flysystem:src/Filesystem.php": [
2415
18
2516
],
26-
"flysystem:src/FilesystemTest.php": [
27-
25
28-
],
2917
"flysystem:src/Ftp/FtpAdapter.php": [
3018
37
3119
],
32-
"flysystem:src/Ftp/FtpAdapterTestCase.php": [
33-
27
34-
],
3520
"flysystem:src/GoogleCloudStorage/GoogleCloudStorageAdapter.php": [
3621
46
3722
],
3823
"flysystem:src/GridFS/GridFSAdapter.php": [
3924
33
4025
],
41-
"flysystem:src/InMemory/InMemoryFilesystemAdapterTest.php": [
42-
22
43-
],
4426
"flysystem:src/Local/LocalFilesystemAdapter.php": [
4527
50
4628
],
47-
"flysystem:src/Local/LocalFilesystemAdapterTest.php": [
48-
43
49-
],
5029
"flysystem:src/MountManager.php": [
5130
14
5231
],
53-
"flysystem:src/MountManagerTest.php": [
54-
17
55-
],
5632
"flysystem:src/PathPrefixing/PathPrefixedAdapter.php": [
5733
30
5834
],
@@ -67,8 +43,5 @@
6743
],
6844
"flysystem:src/ZipArchive/ZipArchiveAdapter.php": [
6945
34
70-
],
71-
"flysystem:src/ZipArchive/ZipArchiveAdapterTestCase.php": [
72-
25
7346
]
7447
}

its/ruling/src/integrationTest/resources/expected/psysh/php-S1448.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,5 @@
2828
],
2929
"psysh:src/Shell.php": [
3030
51
31-
],
32-
"psysh:test/ConfigurationTest.php": [
33-
25
34-
],
35-
"psysh:test/ShellTest.php": [
36-
23
3731
]
3832
}

php-checks/src/main/java/org/sonar/php/checks/OverridingMethodSimplyCallParentCheck.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ private void checkExpression(@Nullable ExpressionTree expressionTree, MethodDecl
126126
}
127127

128128
private static boolean hasSameParameterList(MethodDeclarationTree method, MethodSymbol other) {
129-
MethodSymbol methodSymbol = ((HasMethodSymbol) method).symbol();
130-
129+
var methodSymbol = ((HasMethodSymbol) method).symbol();
131130
List<Parameter> parameters = methodSymbol.parameters();
132131
List<Parameter> otherParameters = other.parameters();
133132
if (parameters.size() != otherParameters.size()) {
@@ -142,7 +141,7 @@ private static boolean hasSameParameterList(MethodDeclarationTree method, Method
142141
}
143142

144143
private static boolean hasSameVisibilityAs(MethodDeclarationTree method, MethodSymbol other) {
145-
return ((HasMethodSymbol) method).symbol().visibility().equals(other.visibility());
144+
return ((HasMethodSymbol) method).symbol().visibility() == other.visibility();
146145
}
147146

148147
private static boolean isFunctionCalledWithSameArgumentsAsDeclared(FunctionCallTree functionCallTree, MethodDeclarationTree method) {

php-checks/src/main/java/org/sonar/php/checks/TooManyMethodsInClassCheck.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.sonar.check.Rule;
2020
import org.sonar.check.RuleProperty;
2121
import org.sonar.php.api.PHPKeyword;
22+
import org.sonar.php.tree.symbols.HasMethodSymbol;
2223
import org.sonar.plugins.php.api.tree.Tree.Kind;
2324
import org.sonar.plugins.php.api.tree.declaration.ClassDeclarationTree;
2425
import org.sonar.plugins.php.api.tree.declaration.ClassMemberTree;
@@ -90,11 +91,14 @@ private int getNumberOfMethods(ClassTree tree) {
9091
}
9192

9293
/**
93-
* Return true if method is private or protected.
94+
* Return true if method is private, protected or is a test method.
9495
*/
9596
private boolean isExcluded(MethodDeclarationTree tree) {
96-
if (!countNonpublicMethods) {
97+
if (((HasMethodSymbol) tree).symbol().isTestMethod().isTrue()) {
98+
return true;
99+
}
97100

101+
if (!countNonpublicMethods) {
98102
for (SyntaxToken modifierToken : tree.modifiers()) {
99103
String modifier = modifierToken.text();
100104
if (PHPKeyword.PROTECTED.getValue().equals(modifier) || PHPKeyword.PRIVATE.getValue().equals(modifier)) {

php-checks/src/test/java/org/sonar/php/checks/TooManyMethodsInClassCheckTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@ class TooManyMethodsInClassCheckTest {
3131
private static final String FILE_NAME = "TooManyMethodsInClassCheck.php";
3232

3333
@Test
34-
void defaultValue() throws Exception {
34+
void shouldReportNoIssueOnDefaultValue() {
3535
CheckVerifier.verifyNoIssueIgnoringExpected(check, FILE_NAME);
3636
}
3737

3838
@Test
39-
void customMaximumMethodThreshold() throws Exception {
39+
void shouldReportIssuesWithCustomThreshold() {
4040
check.maximumMethodThreshold = 2;
4141
CheckVerifier.verify(check, FILE_NAME);
4242
}
4343

4444
@Test
45-
void customCountNonPublicMethod() throws Exception {
45+
void shouldReportIssuesWithCustomThresholdAndWithoutNonPublicMethods() {
4646
check.maximumMethodThreshold = 2;
4747
check.countNonpublicMethods = false;
4848

4949
List<PhpIssue> issues = Arrays.asList(
5050
new LineIssue(check, 3, "Class \"I\" has 3 methods, which is greater than 2 authorized. Split it into smaller classes."),
51-
new LineIssue(check, 35, "This anonymous class has 3 methods, which is greater than 2 authorized. Split it into smaller classes."));
51+
new LineIssue(check, 44, "This anonymous class has 3 methods, which is greater than 2 authorized. Split it into smaller classes."));
5252
PHPCheckTest.check(check, TestUtils.getCheckFile(FILE_NAME), issues);
5353
}
5454

0 commit comments

Comments
 (0)