Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ class Example
}
```

Enforce multi row parameters to not be on the first row.
Example:

```php
class Example
{
public function test(
#[Attribute]
int $param,
)
}
```

### DocCommentSniff

Allows some doc blocks to work as single line:
Expand Down
1 change: 1 addition & 0 deletions library.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,5 @@
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration"/>
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInCall"/>
<rule ref="SlevomatCodingStandard.Arrays.DisallowImplicitArrayCreation"/>
<rule ref="SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing"/>
</ruleset>
17 changes: 17 additions & 0 deletions src/Stefna/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ public function processSingleLineDeclaration(File $phpcsFile, int $stackPtr, arr
return;
}
}
elseif (
!$tokensCollection->sameLine(
$tokensCollection->parenthesisOpener($stackPtr),
$tokensCollection->parenthesisCloser($stackPtr),
)
) {
$parenthesisStart = $tokensCollection->parenthesisOpener($stackPtr);
$nextPtr = $phpcsFile->findNext(T_WHITESPACE, $parenthesisStart + 1, exclude: true);

if ($tokensCollection->sameLine($parenthesisStart, $nextPtr)) {
$error = 'Multiline function declarations can not start on the first line';
$fix = $phpcsFile->addFixableError($error, $nextPtr, 'MultilineNotPure');
if ($fix) {
$phpcsFile->fixer->addNewline($parenthesisStart);
}
}
}

parent::processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ public function testSingleLineWhitespace(): void

self::assertAllFixedInFile('OK');
}

public function testMultilineDeclaration(): void
{
$this->checkFile('MultilineDeclaration');

self::assertSniffError('MultilineNotPure', line: 7);

self::assertAllFixedInFile();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace StefnaTest\Sniffs\Functions\data\MultiLineFunctionDeclarationSniff;

class MultilineDeclaration
{
public function setFoo(
#[Beep]
Foo $new,
): void {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace StefnaTest\Sniffs\Functions\data\MultiLineFunctionDeclarationSniff;

class MultilineDeclaration
{
public function setFoo(#[Beep]
Foo $new,): void
{}
}
7 changes: 7 additions & 0 deletions tests/Sniffs/GeneralCodeStyle/FormattingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ public function testFormatting(): void

self::assertAllFixedInFile(skipErrorCheck: true);
}

public function testAttributeMethodDeclaration(): void
{
$this->checkFile('AttributeMethodDeclaration');

self::assertAllFixedInFile(skipErrorCheck: true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace StefnaTest\Sniffs\GeneralCodeStyle\data\Formatting;

class X
{
public function setFoo(
#[Beep]
Foo $new,
): void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this wrong?
Perhaps the fixed version should only fix the single problem the test is testing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it is wrong.

The tests use all sniffs available, and even for the formatting, but since I don't have #35 merged yet I apparently missed this. I apparently just fixed it in the other test, not this one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or wait, you weren't referring to the missing ,?
@bix0r what part is wrong with this one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually referring to the empty method body. Isn't supposed to be: ): void {}. But yeah, I did miss the trailing comma :o

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace StefnaTest\Sniffs\GeneralCodeStyle\data\Formatting;

class X
{
public function setFoo(#[Beep] Foo $new): void
{
}
}