Skip to content

Commit

Permalink
SONARPHP-1569 S1448 should not raise on test classes (#1339)
Browse files Browse the repository at this point in the history
  • Loading branch information
rudy-regazzoni-sonarsource authored Dec 9, 2024
1 parent b7ff27d commit a75353b
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@
],
"PHPMailer:src/SMTP.php": [
31
],
"PHPMailer:test/PHPMailer/PHPMailerTest.php": [
24
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,5 @@
],
"PHPWord:src/PhpWord/TemplateProcessor.php": [
33
],
"PHPWord:tests/PhpWordTests/Metadata/SettingsTest.php": [
31
],
"PHPWord:tests/PhpWordTests/Shared/HtmlTest.php": [
36
],
"PHPWord:tests/PhpWordTests/Style/TextBoxTest.php": [
30
],
"PHPWord:tests/PhpWordTests/TemplateProcessorTest.php": [
39
],
"PHPWord:tests/PhpWordTests/Writer/Word2007/ElementTest.php": [
31
],
"PHPWord:tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php": [
33
],
"PHPWord:tests/PhpWordTests/Writer/Word2007/Part/SettingsTest.php": [
33
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,5 @@
"PHP_CodeSniffer:src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.inc": [
3,
63
],
"PHP_CodeSniffer:tests/Core/ErrorSuppressionTest.php": [
17
],
"PHP_CodeSniffer:tests/Core/File/FindEndOfStatementTest.php": [
14
],
"PHP_CodeSniffer:tests/Core/File/FindStartOfStatementTest.php": [
14
],
"PHP_CodeSniffer:tests/Core/File/GetMethodParametersTest.php": [
14
],
"PHP_CodeSniffer:tests/Core/File/GetMethodPropertiesTest.php": [
14
],
"PHP_CodeSniffer:tests/Core/Tokenizer/BackfillFnTokenTest.php": [
14
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,41 +151,5 @@
],
"PhpSpreadsheet:src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php": [
23
],
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Calculation/CalculationTest.php": [
15
],
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Cell/CoordinateTest.php": [
12
],
"PhpSpreadsheet:tests/PhpSpreadsheetTests/IOFactoryTest.php": [
14
],
"PhpSpreadsheet:tests/PhpSpreadsheetTests/ReferenceHelperTest.php": [
18
],
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Shared/DateTest.php": [
14
],
"PhpSpreadsheet:tests/PhpSpreadsheetTests/SpreadsheetTest.php": [
12
],
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Style/ConditionalFormatting/CellMatcherTest.php": [
15
],
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Worksheet/AutoFilter/AutoFilterTest.php": [
12
],
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Worksheet/Table/TableTest.php": [
16
],
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Worksheet/Worksheet2Test.php": [
12
],
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php": [
18
],
"PhpSpreadsheet:tests/PhpSpreadsheetTests/Writer/Xlsx/ConditionalTest.php": [
15
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,5 @@
],
"RubixML:src/Datasets/Unlabeled.php": [
31
],
"RubixML:tests/Datasets/LabeledTest.php": [
22
],
"RubixML:tests/Datasets/UnlabeledTest.php": [
21
]
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
{
"flysystem:src/AdapterTestUtilities/FilesystemAdapterTestCase.php": [
35
],
"flysystem:src/AsyncAwsS3/AsyncAwsS3Adapter.php": [
46
],
"flysystem:src/AsyncAwsS3/AsyncAwsS3AdapterTest.php": [
34
],
"flysystem:src/AwsS3V3/AwsS3V3Adapter.php": [
42
],
"flysystem:src/AwsS3V3/AwsS3V3AdapterTest.php": [
33
],
"flysystem:src/AwsS3V3/S3ClientStub.php": [
22
],
Expand All @@ -23,36 +14,21 @@
"flysystem:src/Filesystem.php": [
18
],
"flysystem:src/FilesystemTest.php": [
25
],
"flysystem:src/Ftp/FtpAdapter.php": [
37
],
"flysystem:src/Ftp/FtpAdapterTestCase.php": [
27
],
"flysystem:src/GoogleCloudStorage/GoogleCloudStorageAdapter.php": [
46
],
"flysystem:src/GridFS/GridFSAdapter.php": [
33
],
"flysystem:src/InMemory/InMemoryFilesystemAdapterTest.php": [
22
],
"flysystem:src/Local/LocalFilesystemAdapter.php": [
50
],
"flysystem:src/Local/LocalFilesystemAdapterTest.php": [
43
],
"flysystem:src/MountManager.php": [
14
],
"flysystem:src/MountManagerTest.php": [
17
],
"flysystem:src/PathPrefixing/PathPrefixedAdapter.php": [
30
],
Expand All @@ -67,8 +43,5 @@
],
"flysystem:src/ZipArchive/ZipArchiveAdapter.php": [
34
],
"flysystem:src/ZipArchive/ZipArchiveAdapterTestCase.php": [
25
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,5 @@
],
"psysh:src/Shell.php": [
51
],
"psysh:test/ConfigurationTest.php": [
25
],
"psysh:test/ShellTest.php": [
23
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ private void checkExpression(@Nullable ExpressionTree expressionTree, MethodDecl
}

private static boolean hasSameParameterList(MethodDeclarationTree method, MethodSymbol other) {
MethodSymbol methodSymbol = ((HasMethodSymbol) method).symbol();

var methodSymbol = ((HasMethodSymbol) method).symbol();
List<Parameter> parameters = methodSymbol.parameters();
List<Parameter> otherParameters = other.parameters();
if (parameters.size() != otherParameters.size()) {
Expand All @@ -142,7 +141,7 @@ private static boolean hasSameParameterList(MethodDeclarationTree method, Method
}

private static boolean hasSameVisibilityAs(MethodDeclarationTree method, MethodSymbol other) {
return ((HasMethodSymbol) method).symbol().visibility().equals(other.visibility());
return ((HasMethodSymbol) method).symbol().visibility() == other.visibility();
}

private static boolean isFunctionCalledWithSameArgumentsAsDeclared(FunctionCallTree functionCallTree, MethodDeclarationTree method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.php.api.PHPKeyword;
import org.sonar.php.tree.symbols.HasMethodSymbol;
import org.sonar.plugins.php.api.tree.Tree.Kind;
import org.sonar.plugins.php.api.tree.declaration.ClassDeclarationTree;
import org.sonar.plugins.php.api.tree.declaration.ClassMemberTree;
Expand Down Expand Up @@ -90,11 +91,14 @@ private int getNumberOfMethods(ClassTree tree) {
}

/**
* Return true if method is private or protected.
* Return true if method is private, protected or is a test method.
*/
private boolean isExcluded(MethodDeclarationTree tree) {
if (!countNonpublicMethods) {
if (((HasMethodSymbol) tree).symbol().isTestMethod().isTrue()) {
return true;
}

if (!countNonpublicMethods) {
for (SyntaxToken modifierToken : tree.modifiers()) {
String modifier = modifierToken.text();
if (PHPKeyword.PROTECTED.getValue().equals(modifier) || PHPKeyword.PRIVATE.getValue().equals(modifier)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ class TooManyMethodsInClassCheckTest {
private static final String FILE_NAME = "TooManyMethodsInClassCheck.php";

@Test
void defaultValue() throws Exception {
void shouldReportNoIssueOnDefaultValue() {
CheckVerifier.verifyNoIssueIgnoringExpected(check, FILE_NAME);
}

@Test
void customMaximumMethodThreshold() throws Exception {
void shouldReportIssuesWithCustomThreshold() {
check.maximumMethodThreshold = 2;
CheckVerifier.verify(check, FILE_NAME);
}

@Test
void customCountNonPublicMethod() throws Exception {
void shouldReportIssuesWithCustomThresholdAndWithoutNonPublicMethods() {
check.maximumMethodThreshold = 2;
check.countNonpublicMethods = false;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ public function f1() {
public function f2();
}

class TestClass { // OK

public function test_f1();

public function f2();

public function f3();
}

$x = new class { // Noncompliant
// ^^^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public void setSymbol(MethodSymbol symbol) {
this.symbol = symbol;
}

@Override
public MethodSymbol symbol() {
return symbol;
}
Expand Down

0 comments on commit a75353b

Please sign in to comment.