Skip to content
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"illuminate/container": "^12.35",
"nette/utils": "^4.0.7",
"sebastian/diff": "^6.0.2",
"squizlabs/php_codesniffer": "^3.13.4",
"squizlabs/php_codesniffer": "^4.0",
"symfony/console": "^6.4.24",
"symfony/finder": "^7.3.0",
"symplify/coding-standard": "^12.5",
Expand Down
2 changes: 1 addition & 1 deletion config/set/common/spaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
use PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff;
use PhpCsFixer\Fixer\CastNotation\CastSpacesFixer;
use PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes\LowercaseClassKeywordsSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions\FunctionDeclarationArgumentSpacingSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\Strings\DoubleQuoteUsageSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
use PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff;
use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use PhpCsFixer\Fixer\Basic\BracesFixer;
Expand Down
31 changes: 13 additions & 18 deletions src/SniffRunner/ValueObject/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
*/
final class File extends BaseFile
{
/**
* @var string
*/
public $tokenizerType = 'PHP';

private string|null $activeSniffClass = null;

private string|null $previousActiveSniffClass = null;
Expand Down Expand Up @@ -122,7 +117,7 @@ public function process(): void
*
* @param mixed[] $data
*/
public function addFixableError($error, $stackPtr, $code, $data = [], $severity = 0): bool
public function addFixableError(string $error, int $stackPtr, string $code, array $data = [], int $severity = 0): bool
{
$fullyQualifiedCode = $this->resolveFullyQualifiedCode($code);
$this->sniffMetadataCollector->addAppliedSniff($fullyQualifiedCode);
Expand All @@ -133,7 +128,7 @@ public function addFixableError($error, $stackPtr, $code, $data = [], $severity
/**
* @param mixed[] $data
*/
public function addError($error, $stackPtr, $code, $data = [], $severity = 0, $fixable = false): bool
public function addError(string $error, ?int $stackPtr, string $code, array $data = [], int $severity = 0, bool $fixable = false): bool
{
if ($this->shouldSkipError($error, $code, $data)) {
return false;
Expand All @@ -143,10 +138,10 @@ public function addError($error, $stackPtr, $code, $data = [], $severity = 0, $f
}

/**
* @param mixed $data
* @param mixed[] $data
* Allow only specific classes
*/
public function addWarning($warning, $stackPtr, $code, $data = [], $severity = 0, $fixable = false): bool
public function addWarning(string $warning, ?int $stackPtr, string $code, array $data = [], int $severity = 0, bool $fixable = false): bool
{
if ($this->activeSniffClass === null) {
throw new ShouldNotHappenException();
Expand Down Expand Up @@ -175,18 +170,18 @@ public function processWithTokenListenersAndFilePath(
}

/**
* @param mixed $data
* @param mixed[] $data
* Delegated from addError().
*/
protected function addMessage(
$isError,
$message,
$line,
$column,
$sniffClassOrCode,
$data,
$severity,
$isFixable = false
bool $isError,
string $message,
int $line,
int $column,
string $sniffClassOrCode,
array $data,
int $severity,
bool $isFixable = false
): bool {
// skip warnings
if (! $isError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

use PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\DisallowTabIndentSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
use PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
use PhpCsFixer\Fixer\ControlStructure\IncludeFixer;
use PhpCsFixer\Fixer\Whitespace\IndentationTypeFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
Expand Down
46 changes: 24 additions & 22 deletions tests/SniffRunner/Application/FixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public function testTokenContent(): void
$this->file->parse();
$this->fixer->startFile($this->file);

// Tokenization of identifier names changed in PHPCS from 3.x to 4.0
// https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Version-4.0-Developer-Upgrade-Guide#namespaced-names
$token = $this->fixer->getTokenContent(14);
$this->assertSame('\\', $token);
$this->assertSame('Symplify\EasyCodingStandard\Tests\SniffRunner\Application\FixerSource', $token);
Comment thread
bytehead marked this conversation as resolved.

$this->fixer->replaceToken(14, '_');
$token = $this->fixer->getTokenContent(14);
Expand All @@ -59,11 +61,11 @@ public function testAddContent(): void
$this->fixer->addContentBefore(14, 'A');

$token = $this->fixer->getTokenContent(14);
$this->assertSame('A\\', $token);
$this->assertSame('ASymplify\EasyCodingStandard\Tests\SniffRunner\Application\FixerSource', $token);

$this->fixer->addContent(14, 'B');
$token = $this->fixer->getTokenContent(14);
$this->assertSame('A\\B', $token);
$this->assertSame('ASymplify\EasyCodingStandard\Tests\SniffRunner\Application\FixerSourceB', $token);
}

public function testChangesets(): void
Expand All @@ -73,32 +75,32 @@ public function testChangesets(): void
$this->fixer->beginChangeSet();

$tokenContent = $this->fixer->getTokenContent(14);
$this->assertSame('\\', $tokenContent);
$this->assertSame('Symplify\EasyCodingStandard\Tests\SniffRunner\Application\FixerSource', $tokenContent);

$this->fixer->addContentBefore(14, 'A');
$tokenContent = $this->fixer->getTokenContent(14);
$this->assertSame('A\\', $tokenContent);
$this->assertSame('ASymplify\EasyCodingStandard\Tests\SniffRunner\Application\FixerSource', $tokenContent);

// during the changeset, you are free to modify current token as you wish...
$this->fixer->addContent(14, 'B');
$tokenContent = $this->fixer->getTokenContent(14);
$this->assertSame('A\\B', $tokenContent);
$this->assertSame('ASymplify\EasyCodingStandard\Tests\SniffRunner\Application\FixerSourceB', $tokenContent);

// you can also rollback the changes...
$this->fixer->rollbackChangeset();
$tokenContent = $this->fixer->getTokenContent(14);
$this->assertSame('\\', $tokenContent);
$this->assertSame('Symplify\EasyCodingStandard\Tests\SniffRunner\Application\FixerSource', $tokenContent);

$this->fixer->addContent(14, 'B');
$this->fixer->endChangeSet();

$tokenContent = $this->fixer->getTokenContent(14);
$this->assertSame('\\B', $tokenContent);
$this->assertSame('Symplify\EasyCodingStandard\Tests\SniffRunner\Application\FixerSourceB', $tokenContent);

// ...that stops being the case after changeset is committed
$this->fixer->addContent(14, 'C');
$tokenContent = $this->fixer->getTokenContent(14);
$this->assertSame('\\B', $tokenContent);
$this->assertSame('Symplify\EasyCodingStandard\Tests\SniffRunner\Application\FixerSourceB', $tokenContent);
}

public function testAddNewline(): void
Expand All @@ -108,15 +110,15 @@ public function testAddNewline(): void
$this->fixer->beginChangeSet();

$token = $this->fixer->getTokenContent(14);
$this->assertSame('\\', $token);
$this->assertSame('Symplify\EasyCodingStandard\Tests\SniffRunner\Application\FixerSource', $token);

$this->fixer->addNewline(14);
$token = $this->fixer->getTokenContent(14);
$this->assertSame('\\' . PHP_EOL, $token);
$this->assertSame('Symplify\EasyCodingStandard\Tests\SniffRunner\Application\FixerSource' . PHP_EOL, $token);

$this->fixer->addNewlineBefore(14);
$token = $this->fixer->getTokenContent(14);
$this->assertSame(PHP_EOL . '\\' . PHP_EOL, $token);
$this->assertSame(PHP_EOL . 'Symplify\EasyCodingStandard\Tests\SniffRunner\Application\FixerSource' . PHP_EOL, $token);
}

public function testSubstrToken(): void
Expand All @@ -125,19 +127,19 @@ public function testSubstrToken(): void
$this->fixer->startFile($this->file);
$this->fixer->beginChangeSet();

$token = $this->fixer->getTokenContent(15);
$this->assertSame('EasyCodingStandard', $token);
$token = $this->fixer->getTokenContent(14);
$this->assertSame('Symplify\EasyCodingStandard\Tests\SniffRunner\Application\FixerSource', $token);

$this->fixer->substrToken(15, 0, 4);
$token = $this->fixer->getTokenContent(15);
$this->assertSame('Easy', $token);
$this->fixer->substrToken(14, 0, 4);
$token = $this->fixer->getTokenContent(14);
$this->assertSame('Symp', $token);

$this->fixer->substrToken(15, 3);
$token = $this->fixer->getTokenContent(15);
$this->assertSame('y', $token);
$this->fixer->substrToken(14, 3);
$token = $this->fixer->getTokenContent(14);
$this->assertSame('p', $token);

$this->fixer->substrToken(17, 3, 0);
$token = $this->fixer->getTokenContent(17);
$this->fixer->substrToken(14, 3, 0);
$token = $this->fixer->getTokenContent(14);
$this->assertSame('', $token);
}
}