Skip to content

Commit

Permalink
✨ Require multiline constructor parameters and trailing comma (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpi authored Nov 21, 2023
1 parent 5028dd1 commit 8fd73ba
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 0 deletions.
10 changes: 10 additions & 0 deletions PreviousNextDrupal/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,18 @@
</properties>
</rule>

<rule ref="SlevomatCodingStandard.Classes.RequireMultiLineMethodSignature">
<properties>
<property name="minLineLength" value="80"/>
<property name="includedMethodPatterns" type="array">
<element value="/__construct/" />
</property>
</properties>
</rule>

<!-- SlevomatCodingStandard.Functions -->
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInCall" />
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration" />
<rule ref="SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure" />

<!-- SlevomatCodingStandard.Namespaces -->
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ function doesAThing(array $data): int {

#### PSR2.Methods.FunctionClosingBrace.SpacingBeforeClose
#### SlevomatCodingStandard.Classes.ClassStructure
#### SlevomatCodingStandard.Classes.RequireMultiLineMethodSignature
#### SlevomatCodingStandard.Functions.RequireTrailingCommaInCall
#### SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration
#### SlevomatCodingStandard.Functions.StaticClosure
#### SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure
#### SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses
Expand Down
27 changes: 27 additions & 0 deletions tests/Sniffs/RequireMultiLineMethodSignatureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types = 1);

namespace PreviousNext\CodingStandard\Tests\Sniffs;

/**
* @covers \SlevomatCodingStandard\Sniffs\Classes\RequireMultiLineMethodSignatureSniff
*/
final class RequireMultiLineMethodSignatureTest extends Base {

public function testNoError(): void {
$report = self::checkFile(__DIR__ . '/fixtures/RequireMultiLineMethodSignatureNoError.php');
self::assertNoSniffErrorInFile($report);
}

public function testError(): void {
$report = self::checkFile(__DIR__ . '/fixtures/RequireMultiLineMethodSignatureError.php');
self::assertSame(1, $report->getErrorCount());
self::assertSniffError($report, 15, sniffName: 'SlevomatCodingStandard.Classes.RequireMultiLineMethodSignature', code: 'RequiredMultiLineSignature');
}

protected static function getSniffName(): string {
return 'SlevomatCodingStandard.Classes.RequireMultiLineMethodSignature';
}

}
32 changes: 32 additions & 0 deletions tests/Sniffs/RequireTrailingCommaInDeclarationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types = 1);

namespace PreviousNext\CodingStandard\Tests\Sniffs;

/**
* @covers \SlevomatCodingStandard\Sniffs\Classes\RequireMultiLineMethodSignatureSniff
*/
final class RequireTrailingCommaInDeclarationTest extends Base {

public function testNoError(): void {
$report = self::checkFile(__DIR__ . '/fixtures/RequireTrailingCommaInDeclarationNoError.php');
self::assertNoSniffErrorInFile($report);
}

public function testNoErrorSingleLine(): void {
$report = self::checkFile(__DIR__ . '/fixtures/RequireTrailingCommaInDeclarationSingleLineNoError.php');
self::assertNoSniffErrorInFile($report);
}

public function testError(): void {
$report = self::checkFile(__DIR__ . '/fixtures/RequireTrailingCommaInDeclarationError.php');
self::assertSame(1, $report->getErrorCount());
self::assertSniffError($report, 17, sniffName: 'SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration', code: 'MissingTrailingComma');
}

protected static function getSniffName(): string {
return 'SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration';
}

}
18 changes: 18 additions & 0 deletions tests/Sniffs/fixtures/RequireMultiLineMethodSignatureError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types = 1);

namespace Sniffs\fixtures;

/**
* The class.
*/
final class RequireMultiLineMethodSignatureError {

/**
* Constructor.
*/
public function __construct(protected string $fooBar, protected string $helloWorld) {
}

}
21 changes: 21 additions & 0 deletions tests/Sniffs/fixtures/RequireMultiLineMethodSignatureNoError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types = 1);

namespace Sniffs\fixtures;

/**
* The class.
*/
final class RequireMultiLineMethodSignatureNoError {

/**
* Constructor.
*/
public function __construct(
protected string $fooBar,
protected string $helloWorld,
) {
}

}
21 changes: 21 additions & 0 deletions tests/Sniffs/fixtures/RequireTrailingCommaInDeclarationError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types = 1);

namespace Sniffs\fixtures;

/**
* The class.
*/
final class RequireTrailingCommaInDeclarationError {

/**
* Constructor.
*/
public function __construct(
protected string $fooBar,
protected string $helloWorld
) {
}

}
21 changes: 21 additions & 0 deletions tests/Sniffs/fixtures/RequireTrailingCommaInDeclarationNoError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types = 1);

namespace Sniffs\fixtures;

/**
* The class.
*/
final class RequireTrailingCommaInDeclarationNoError {

/**
* Constructor.
*/
public function __construct(
protected string $fooBar,
protected string $helloWorld,
) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types = 1);

namespace PreviousNext\CodingStandard\Tests\Sniffs\fixtures;

/**
* The class.
*/
final class RequireTrailingCommaInDeclarationSingleLineNoError {

/**
* Constructor.
*/
public function __construct(protected string $fooBar) {
}

}

0 comments on commit 8fd73ba

Please sign in to comment.