Skip to content

Commit 31dba61

Browse files
committed
[MultilineFunctionDeclaration] Make multiline in the middle
If a function is having multiple rows in the parameters, then nothing can be on the first row.
1 parent e9c1e96 commit 31dba61

File tree

11 files changed

+111
-6
lines changed

11 files changed

+111
-6
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ class Example
2929
}
3030
```
3131

32+
Enforce multi row parameters to not be on the first row.
33+
Example:
34+
35+
```php
36+
class Example
37+
{
38+
public function test(
39+
#[Attribute]
40+
int $param
41+
)
42+
}
43+
3244
### DocCommentSniff
3345

3446
Allows some doc blocks to work as single line:

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@
2020
},
2121
"require": {
2222
"php": "^8.4",
23-
"squizlabs/php_codesniffer": "^4.0"
23+
"squizlabs/php_codesniffer": "^4.0",
24+
"slevomat/coding-standard": "^8.25"
2425
},
2526
"bin": [
2627
"bin/stefna-hook-install"
2728
],
2829
"require-dev": {
2930
"phpunit/phpunit": "^12.5"
31+
},
32+
"config": {
33+
"allow-plugins": {
34+
"dealerdirect/phpcodesniffer-composer-installer": true
35+
}
3036
}
3137
}

library.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,7 @@
8080
<rule ref="Squiz.Strings.DoubleQuoteUsage">
8181
<exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar"/>
8282
</rule>
83+
84+
<config name="installed_paths" value="vendor/slevomat/coding-standard"/>
85+
<rule ref="SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing"/>
8386
</ruleset>

src/Stefna/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ public function processSingleLineDeclaration(File $phpcsFile, int $stackPtr, arr
4848
return;
4949
}
5050
}
51+
elseif (
52+
!$tokensCollection->sameLine(
53+
$tokensCollection->parenthesisOpener($stackPtr),
54+
$tokensCollection->parenthesisCloser($stackPtr)
55+
)
56+
) {
57+
$parenthesisStart = $tokensCollection->parenthesisOpener($stackPtr);
58+
$nextPtr = $phpcsFile->findNext(T_WHITESPACE, $parenthesisStart + 1, exclude: true);
59+
60+
if ($tokensCollection->sameLine($parenthesisStart, $nextPtr)) {
61+
$error = 'Multiline function declarations can not start on the first line';
62+
$fix = $phpcsFile->addFixableError($error, $nextPtr, 'MultilineNotPure');
63+
if ($fix) {
64+
$phpcsFile->fixer->addNewline($parenthesisStart);
65+
}
66+
}
67+
}
5168

5269
parent::processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens);
5370
}

tests/Sniffs/Functions/MultiLineFunctionDeclarationSniffTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,13 @@ public function testSingleLineWhitespace(): void
2222

2323
self::assertAllFixedInFile('OK');
2424
}
25+
26+
public function testMultilineDeclaration(): void
27+
{
28+
$this->checkFile('MultilineDeclaration');
29+
30+
self::assertSniffError('MultilineNotPure', line: 7);
31+
32+
self::assertAllFixedInFile();
33+
}
2534
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace StefnaTest\Sniffs\Functions\data\MultiLineFunctionDeclarationSniff;
4+
5+
class MultilineDeclaration
6+
{
7+
public function setFoo(
8+
#[Beep]
9+
Foo $new
10+
): void {
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace StefnaTest\Sniffs\Functions\data\MultiLineFunctionDeclarationSniff;
4+
5+
class MultilineDeclaration
6+
{
7+
public function setFoo(#[Beep]
8+
Foo $new): void
9+
{}
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace StefnaTest\Sniffs\GeneralCodeStyle;
4+
5+
use StefnaTest\Sniffs\TestCase;
6+
7+
class FormattingTest extends TestCase
8+
{
9+
public function testFormatting(): void
10+
{
11+
$this->checkFile('AttributeMethodDeclaration');
12+
13+
self::assertAllFixedInFile(skipErrorCheck: true);
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace StefnaTest\Sniffs\GeneralCodeStyle\data\Formatting;
4+
5+
class X
6+
{
7+
public function setFoo(
8+
#[Beep]
9+
Foo $new
10+
): void {
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace StefnaTest\Sniffs\GeneralCodeStyle\data\Formatting;
4+
5+
class X
6+
{
7+
public function setFoo(#[Beep] Foo $new): void
8+
{
9+
}
10+
}

0 commit comments

Comments
 (0)