Skip to content

Commit

Permalink
CR fix
Browse files Browse the repository at this point in the history
  • Loading branch information
David Kurka committed Nov 30, 2023
1 parent 8087faa commit e0c911f
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

declare(strict_types=1);

namespace Cdn77\Sniffs\OrderingConventions;
namespace Cdn77\Sniffs\Ordering;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use SlevomatCodingStandard\Helpers\FixerHelper;

use function array_key_first;
use function array_map;
use function assert;
use function implode;
use function in_array;
use function is_array;
use function reset;
use function sort;
use function sprintf;
use function ucfirst;
Expand Down Expand Up @@ -75,9 +73,7 @@ public function process(File $phpcsFile, mixed $stackPtr): void
continue;
}

$firstNameWithValue = reset($namesWithValues);
assert(is_array($firstNameWithValue));

$firstNameWithValue = $namesWithValues[array_key_first($namesWithValues)];
$fix = $phpcsFile->addFixableError(
sprintf('%s constant names are not alphabetically ordered.', ucfirst($visibility)),
$firstNameWithValue['name']['ptr'],
Expand Down
41 changes: 41 additions & 0 deletions tests/Sniffs/Ordering/AlphabeticallyOrderedConstantsSniffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Cdn77\Sniffs\Ordering;

use Cdn77\TestCase;

use function array_keys;
use function json_encode;

use const JSON_THROW_ON_ERROR;

final class AlphabeticallyOrderedConstantsSniffTest extends TestCase
{
public function testErrors(): void
{
$file = self::checkFile(
__DIR__ . '/data/AlphabeticallyOrderedConstantsSniffTest.php',
);

$expectedErrors = [
9 => AlphabeticallyOrderedConstantsSniff::CodeIncorrectConstantOrder,
19 => AlphabeticallyOrderedConstantsSniff::CodeIncorrectConstantOrder,
24 => AlphabeticallyOrderedConstantsSniff::CodeIncorrectConstantOrder,
];
$possibleLines = array_keys($expectedErrors);
$errors = $file->getErrors();

foreach ($errors as $line => $error) {
self::assertContains($line, $possibleLines, json_encode($error, JSON_THROW_ON_ERROR));

$expectedError = $expectedErrors[$line];
self::assertSniffError($file, $line, $expectedError);
}

self::assertSame(3, $file->getErrorCount());

self::assertAllFixedInFile($file);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Cdn77\Sniffs\Ordering\data;

final class AlphabeticallyOrderedConstantsSniffTest
{
public const A = 'a';
public const B = 'b';

public const
IgnoredMultiline1 = 1,
IgnoredMultiline2 = 2;

public const C = 'c';
public const D = 'd';

protected const E = 'e';
protected const F = 'f';
protected const G = 'g';
protected const H = 'h';

private const I = 'i';
private const J = 'j';
private const K = 'k';
private const L = 'l';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Cdn77\Sniffs\Ordering\data;

final class AlphabeticallyOrderedConstantsSniffTest
{
public const C = 'c';
public const A = 'a';

public const
IgnoredMultiline1 = 1,
IgnoredMultiline2 = 2;

public const D = 'd';
public const B = 'b';

protected const H = 'h';
protected const E = 'e';
protected const G = 'g';
protected const F = 'f';

private const K = 'k';
private const J = 'j';
private const I = 'i';
private const L = 'l';
}

0 comments on commit e0c911f

Please sign in to comment.