Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TraitUseSpacingSniff: Add linesCountBeforeFirstUseWhenFirstInClass #866 #870

Merged
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ Enforces configurable number of lines before first `use`, after last `use` and b
Sniff provides the following settings:

* `linesCountBeforeFirstUse`: allows to configure the number of lines before first `use`.
* `linesCountBeforeFirstUseWhenFirstInClass`: allows to configure the number of lines before first `use` when the `use` is the first statement in the class.
* `linesCountBetweenUses`: allows to configure the number of lines between two `use` statements.
* `linesCountAfterLastUse`: allows to configure the number of lines after last `use`.
* `linesCountAfterLastUseWhenLastInClass`: allows to configure the number of lines after last `use` when the `use` is the last statement in the class.
Expand Down
12 changes: 12 additions & 0 deletions SlevomatCodingStandard/Sniffs/Classes/TraitUseSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class TraitUseSpacingSniff implements Sniff
/** @var int */
public $linesCountBeforeFirstUse = 1;

/** @var int */
public $linesCountBeforeFirstUseWhenFirstInClass = null;

/** @var int */
public $linesCountBetweenUses = 0;

Expand Down Expand Up @@ -69,8 +72,11 @@ public function process(File $phpcsFile, $classPointer): void

private function checkLinesBeforeFirstUse(File $phpcsFile, int $firstUsePointer): void
{
$tokens = $phpcsFile->getTokens();

/** @var int $pointerBeforeFirstUse */
$pointerBeforeFirstUse = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $firstUsePointer - 1);
$isAtTheStartOfClass = $tokens[$pointerBeforeFirstUse]['code'] === T_OPEN_CURLY_BRACKET;
kukulich marked this conversation as resolved.
Show resolved Hide resolved

$whitespaceBeforeFirstUse = '';

Expand All @@ -79,6 +85,12 @@ private function checkLinesBeforeFirstUse(File $phpcsFile, int $firstUsePointer)
}

$requiredLinesCountBeforeFirstUse = SniffSettingsHelper::normalizeInteger($this->linesCountBeforeFirstUse);
if (
$isAtTheStartOfClass
&& $this->linesCountBeforeFirstUseWhenFirstInClass !== null
) {
$requiredLinesCountBeforeFirstUse = SniffSettingsHelper::normalizeInteger($this->linesCountBeforeFirstUseWhenFirstInClass);
}
$actualLinesCountBeforeFirstUse = substr_count($whitespaceBeforeFirstUse, $phpcsFile->eolChar) - 1;

if ($actualLinesCountBeforeFirstUse === $requiredLinesCountBeforeFirstUse) {
Expand Down
38 changes: 38 additions & 0 deletions tests/Sniffs/Classes/TraitUseSpacingSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function testModifiedSettingNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/traitUseSpacingModifiedSettingsNoErrors.php', [
'linesCountBeforeFirstUse' => 0,
'linesCountBeforeFirstUseWhenFirstInClass' => 0,
'linesCountBetweenUses' => 1,
'linesCountAfterLastUse' => 2,
'linesCountAfterLastUseWhenLastInClass' => 2,
Expand All @@ -73,6 +74,7 @@ public function testModifiedSettingErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/traitUseSpacingModifiedSettingsErrors.php', [
'linesCountBeforeFirstUse' => 0,
'linesCountBeforeFirstUseWhenFirstInClass' => 0,
'linesCountBetweenUses' => 1,
'linesCountAfterLastUse' => 2,
'linesCountAfterLastUseWhenLastInClass' => 2,
Expand All @@ -89,10 +91,44 @@ public function testModifiedSettingErrors(): void
self::assertAllFixedInFile($report);
}

public function testModifiedSettingWhenUseIsFirstInClassNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/traitUseSpacingModifiedSettingsWhenUseIsLastInClassNoErrors.php', [
'linesCountBeforeFirstUse' => 2,
'linesCountBeforeFirstUseWhenFirstInClass' => 0,
'linesCountBetweenUses' => 1,
'linesCountAfterLastUse' => 0,
'linesCountAfterLastUseWhenLastInClass' => 0,
]);
self::assertNoSniffErrorInFile($report);
}

public function testModifiedSettingWhenUseIsFirstInClassErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/traitUseSpacingModifiedSettingsWhenUseIsLastInClassErrors.php', [
'linesCountBeforeFirstUse' => 2,
'linesCountBeforeFirstUseWhenFirstInClass' => 0,
'linesCountBetweenUses' => 1,
'linesCountAfterLastUse' => 0,
'linesCountAfterLastUseWhenLastInClass' => 0,
]);

self::assertSame(5, $report->getErrorCount());

self::assertSniffError($report, 6, TraitUseSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_USE);
self::assertSniffError($report, 7, TraitUseSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_USES);
self::assertSniffError($report, 10, TraitUseSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_USES);
self::assertSniffError($report, 11, TraitUseSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_USES);
self::assertSniffError($report, 11, TraitUseSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_LAST_USE);

self::assertAllFixedInFile($report);
}

public function testModifiedSettingWhenUseIsLastInClassNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/traitUseSpacingModifiedSettingsWhenUseIsLastInClassNoErrors.php', [
'linesCountBeforeFirstUse' => 0,
'linesCountBeforeFirstUseWhenFirstInClass' => 0,
'linesCountBetweenUses' => 1,
'linesCountAfterLastUse' => 2,
'linesCountAfterLastUseWhenLastInClass' => 0,
Expand All @@ -104,6 +140,7 @@ public function testModifiedSettingWhenUseIsLastInClassErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/traitUseSpacingModifiedSettingsWhenUseIsLastInClassErrors.php', [
'linesCountBeforeFirstUse' => 0,
'linesCountBeforeFirstUseWhenFirstInClass' => 0,
'linesCountBetweenUses' => 1,
'linesCountAfterLastUse' => 2,
'linesCountAfterLastUseWhenLastInClass' => 0,
Expand All @@ -124,6 +161,7 @@ public function testModifiedSettingWhenUseIsLastInClass2Errors(): void
{
$report = self::checkFile(__DIR__ . '/data/traitUseSpacingModifiedSettingsWhenUseIsLastInClass2Errors.php', [
'linesCountBeforeFirstUse' => 0,
'linesCountBeforeFirstUseWhenFirstInClass' => 1,
'linesCountBetweenUses' => 0,
'linesCountAfterLastUse' => 1,
'linesCountAfterLastUseWhenLastInClass' => 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

class Whatever
{



use A;
use B;


use C;
use D {
D::d as dd;
}

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

class Whatever
{
use A;

use B;

use C;

use D {
D::d as dd;
}
}