Skip to content

Commit 1f63961

Browse files
committed
Merge branch 'php-8.0/3342-pear-functiondeclaration-constructor-prop-prom-indent' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 801ae46 + 596bdf9 commit 1f63961

9 files changed

+272
-7
lines changed

src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,10 @@ public function processArgumentList($phpcsFile, $stackPtr, $indent, $type='funct
456456
}
457457

458458
// We changed lines, so this should be a whitespace indent token.
459-
if ($tokens[$i]['code'] !== T_WHITESPACE) {
460-
$foundIndent = 0;
461-
} else if ($tokens[$i]['line'] !== $tokens[($i + 1)]['line']) {
459+
$foundIndent = 0;
460+
if ($tokens[$i]['code'] === T_WHITESPACE
461+
&& $tokens[$i]['line'] !== $tokens[($i + 1)]['line']
462+
) {
462463
// This is an empty line, so don't check the indent.
463464
$foundIndent = $expectedIndent;
464465

@@ -467,8 +468,11 @@ public function processArgumentList($phpcsFile, $stackPtr, $indent, $type='funct
467468
if ($fix === true) {
468469
$phpcsFile->fixer->replaceToken($i, '');
469470
}
470-
} else {
471+
} else if ($tokens[$i]['code'] === T_WHITESPACE) {
472+
$foundIndent = $tokens[$i]['length'];
473+
} else if ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE) {
471474
$foundIndent = $tokens[$i]['length'];
475+
++$expectedIndent;
472476
}
473477

474478
if ($expectedIndent !== $foundIndent) {

src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,61 @@ if(true) {
314314
;
315315
}
316316
}
317+
318+
class ConstructorPropertyPromotionSingleLineDocblockIndentOK
319+
{
320+
public function __construct(
321+
/** @var string */
322+
public string $public,
323+
/** @var string */
324+
private string $private,
325+
) {
326+
}
327+
}
328+
329+
class ConstructorPropertyPromotionMultiLineDocblockAndAttributeIndentOK
330+
{
331+
public function __construct(
332+
/**
333+
* @var string
334+
* @Assert\NotBlank()
335+
*/
336+
public string $public,
337+
/**
338+
* @var string
339+
* @Assert\NotBlank()
340+
*/
341+
#[NotBlank]
342+
private string $private,
343+
) {
344+
}
345+
}
346+
347+
class ConstructorPropertyPromotionSingleLineDocblockIncorrectIndent
348+
{
349+
public function __construct(
350+
/** @var string */
351+
public string $public,
352+
/** @var string */
353+
private string $private,
354+
) {
355+
}
356+
}
357+
358+
class ConstructorPropertyPromotionMultiLineDocblockAndAttributeIncorrectIndent
359+
{
360+
public function __construct(
361+
/**
362+
* @var string
363+
* @Assert\NotBlank()
364+
*/
365+
public string $public,
366+
/**
367+
* @var string
368+
* @Assert\NotBlank()
369+
*/
370+
#[NotBlank]
371+
private string $private,
372+
) {
373+
}
374+
}

src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,61 @@ if(true) {
312312
abstract function baz();
313313
}
314314
}
315+
316+
class ConstructorPropertyPromotionSingleLineDocblockIndentOK
317+
{
318+
public function __construct(
319+
/** @var string */
320+
public string $public,
321+
/** @var string */
322+
private string $private,
323+
) {
324+
}
325+
}
326+
327+
class ConstructorPropertyPromotionMultiLineDocblockAndAttributeIndentOK
328+
{
329+
public function __construct(
330+
/**
331+
* @var string
332+
* @Assert\NotBlank()
333+
*/
334+
public string $public,
335+
/**
336+
* @var string
337+
* @Assert\NotBlank()
338+
*/
339+
#[NotBlank]
340+
private string $private,
341+
) {
342+
}
343+
}
344+
345+
class ConstructorPropertyPromotionSingleLineDocblockIncorrectIndent
346+
{
347+
public function __construct(
348+
/** @var string */
349+
public string $public,
350+
/** @var string */
351+
private string $private,
352+
) {
353+
}
354+
}
355+
356+
class ConstructorPropertyPromotionMultiLineDocblockAndAttributeIncorrectIndent
357+
{
358+
public function __construct(
359+
/**
360+
* @var string
361+
* @Assert\NotBlank()
362+
*/
363+
public string $public,
364+
/**
365+
* @var string
366+
* @Assert\NotBlank()
367+
*/
368+
#[NotBlank]
369+
private string $private,
370+
) {
371+
}
372+
}

src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ public function getErrorList($testFile='FunctionDeclarationUnitTest.inc')
8282
309 => 1,
8383
313 => 1,
8484
314 => 1,
85+
350 => 1,
86+
351 => 1,
87+
352 => 1,
88+
353 => 1,
89+
361 => 1,
90+
362 => 1,
91+
363 => 1,
92+
364 => 1,
93+
365 => 1,
94+
366 => 1,
95+
367 => 1,
96+
368 => 1,
97+
369 => 1,
98+
370 => 1,
99+
371 => 1,
85100
];
86101
} else {
87102
$errors = [

src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function getErrorList()
3232
32 => 1,
3333
33 => 1,
3434
34 => 1,
35-
35 => 2,
35+
35 => 1,
3636
36 => 1,
3737
37 => 3,
3838
39 => 1,

src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function processBracket($phpcsFile, $openBracket, $tokens, $type='functio
200200
// The open bracket should be the last thing on the line.
201201
if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) {
202202
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
203-
if ($tokens[$next]['line'] !== ($tokens[$openBracket]['line'] + 1)) {
203+
if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) {
204204
$error = 'The first parameter of a multi-line '.$type.' declaration must be on the line after the opening bracket';
205205
$fix = $phpcsFile->addFixableError($error, $next, $errorPrefix.'FirstParamSpacing');
206206
if ($fix === true) {

src/Standards/Squiz/Tests/Functions/MultiLineFunctionDeclarationUnitTest.inc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,61 @@ function foo(
197197
$bar
198198
) {
199199
}
200+
201+
class ConstructorPropertyPromotionSingleLineDocblockIndentOK
202+
{
203+
public function __construct(
204+
/** @var string */
205+
public string $public,
206+
/** @var string */
207+
private string $private,
208+
) {
209+
}
210+
}
211+
212+
class ConstructorPropertyPromotionMultiLineDocblockAndAttributeIndentOK
213+
{
214+
public function __construct(
215+
/**
216+
* @var string
217+
* @Assert\NotBlank()
218+
*/
219+
public string $public,
220+
/**
221+
* @var string
222+
* @Assert\NotBlank()
223+
*/
224+
#[NotBlank]
225+
private string $private,
226+
) {
227+
}
228+
}
229+
230+
class ConstructorPropertyPromotionSingleLineDocblockIncorrectIndent
231+
{
232+
public function __construct(
233+
/** @var string */
234+
public string $public,
235+
/** @var string */
236+
private string $private,
237+
) {
238+
}
239+
}
240+
241+
class ConstructorPropertyPromotionMultiLineDocblockAndAttributeIncorrectIndent
242+
{
243+
public function __construct(
244+
/**
245+
* @var string
246+
* @Assert\NotBlank()
247+
*/
248+
public string $public,
249+
/**
250+
* @var string
251+
* @Assert\NotBlank()
252+
*/
253+
#[NotBlank]
254+
private string $private,
255+
) {
256+
}
257+
}

src/Standards/Squiz/Tests/Functions/MultiLineFunctionDeclarationUnitTest.inc.fixed

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,61 @@ function foo(
209209
$bar
210210
) {
211211
}
212+
213+
class ConstructorPropertyPromotionSingleLineDocblockIndentOK
214+
{
215+
public function __construct(
216+
/** @var string */
217+
public string $public,
218+
/** @var string */
219+
private string $private,
220+
) {
221+
}
222+
}
223+
224+
class ConstructorPropertyPromotionMultiLineDocblockAndAttributeIndentOK
225+
{
226+
public function __construct(
227+
/**
228+
* @var string
229+
* @Assert\NotBlank()
230+
*/
231+
public string $public,
232+
/**
233+
* @var string
234+
* @Assert\NotBlank()
235+
*/
236+
#[NotBlank]
237+
private string $private,
238+
) {
239+
}
240+
}
241+
242+
class ConstructorPropertyPromotionSingleLineDocblockIncorrectIndent
243+
{
244+
public function __construct(
245+
/** @var string */
246+
public string $public,
247+
/** @var string */
248+
private string $private,
249+
) {
250+
}
251+
}
252+
253+
class ConstructorPropertyPromotionMultiLineDocblockAndAttributeIncorrectIndent
254+
{
255+
public function __construct(
256+
/**
257+
* @var string
258+
* @Assert\NotBlank()
259+
*/
260+
public string $public,
261+
/**
262+
* @var string
263+
* @Assert\NotBlank()
264+
*/
265+
#[NotBlank]
266+
private string $private,
267+
) {
268+
}
269+
}

src/Standards/Squiz/Tests/Functions/MultiLineFunctionDeclarationUnitTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,21 @@ public function getErrorList($testFile='MultiLineFunctionDeclarationUnitTest.inc
5555
190 => 2,
5656
194 => 1,
5757
195 => 1,
58-
196 => 1,
58+
233 => 1,
59+
234 => 1,
60+
235 => 1,
61+
236 => 1,
62+
244 => 1,
63+
245 => 1,
64+
246 => 1,
65+
247 => 1,
66+
248 => 1,
67+
249 => 1,
68+
250 => 1,
69+
251 => 1,
70+
252 => 1,
71+
253 => 1,
72+
254 => 1,
5973
];
6074
} else {
6175
$errors = [

0 commit comments

Comments
 (0)