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: 2 additions & 0 deletions config/set/common/namespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
use PhpCsFixer\Fixer\Import\OrderedImportsFixer;
use PhpCsFixer\Fixer\NamespaceNotation\SingleBlankLineBeforeNamespaceFixer;
use Symplify\CodingStandard\Fixer\ConstantNotation\RemoveSlashFromNativeConstantFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
Expand All @@ -14,4 +15,5 @@
OrderedImportsFixer::class,
SingleBlankLineBeforeNamespaceFixer::class,
NoUnneededImportAliasFixer::class,
RemoveSlashFromNativeConstantFixer::class,
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Symplify\CodingStandard\Fixer\ConstantNotation;

use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;

/**
* @see \Symplify\CodingStandard\Tests\Fixer\ConstantNotation\RemoveSlashFromNativeConstantFixer\RemoveSlashFromNativeConstantFixerTest
*/
final class RemoveSlashFromNativeConstantFixer extends AbstractSymplifyFixer
{
private const string ERROR_MESSAGE = 'Remove leading backslash from "true", "false" and "null".';

/**
* @var string[]
*/
private const array KEYWORDS = ['true', 'false', 'null'];

public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(self::ERROR_MESSAGE, [new CodeSample("<?php\nreturn \\true;\n")]);
}

/**
* @param Tokens<Token> $tokens
*/
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_NS_SEPARATOR);
}

/**
* @param Tokens<Token> $tokens
*/
public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
{
foreach ($tokens as $index => $token) {
if (! $token->isGivenKind(T_NS_SEPARATOR)) {
continue;
}

$nextIndex = $tokens->getNextMeaningfulToken($index);
if ($nextIndex === null) {
continue;
}

if (! $tokens[$nextIndex]->isGivenKind(T_STRING)) {
continue;
}

if (! in_array(strtolower($tokens[$nextIndex]->getContent()), self::KEYWORDS, true)) {
continue;
}

// the keyword must be a standalone name, not a namespace segment like "Foo\true" or "\true\Bar"
$prevIndex = $tokens->getPrevMeaningfulToken($index);
if ($prevIndex !== null && $tokens[$prevIndex]->isGivenKind([T_STRING, T_NS_SEPARATOR])) {
continue;
}

$afterKeywordIndex = $tokens->getNextMeaningfulToken($nextIndex);
if ($afterKeywordIndex !== null && $tokens[$afterKeywordIndex]->isGivenKind(T_NS_SEPARATOR)) {
continue;
}

$tokens->clearAt($index);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Symplify\CodingStandard\Tests\Fixer\ConstantNotation\RemoveSlashFromNativeConstantFixer\Fixture;

function fixBoolAndNull()
{
if (\true) {
return \false;
}

return \null;
}

?>
-----
<?php

namespace Symplify\CodingStandard\Tests\Fixer\ConstantNotation\RemoveSlashFromNativeConstantFixer\Fixture;

function fixBoolAndNull()
{
if (true) {
return false;
}

return null;
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Symplify\CodingStandard\Tests\Fixer\ConstantNotation\RemoveSlashFromNativeConstantFixer\Fixture;

$value = \TRUE;
$other = \Null;

?>
-----
<?php

namespace Symplify\CodingStandard\Tests\Fixer\ConstantNotation\RemoveSlashFromNativeConstantFixer\Fixture;

$value = TRUE;
$other = Null;

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symplify\CodingStandard\Tests\Fixer\ConstantNotation\RemoveSlashFromNativeConstantFixer\Fixture;

use Some\True\Value;

function skipNamespaceSegment()
{
return Some\True\Value::NAME;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Symplify\CodingStandard\Tests\Fixer\ConstantNotation\RemoveSlashFromNativeConstantFixer;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Symplify\EasyCodingStandard\Testing\PHPUnit\AbstractCheckerTestCase;

final class RemoveSlashFromNativeConstantFixerTest extends AbstractCheckerTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFiles(__DIR__ . '/Fixture');
}

public function provideConfig(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Symplify\CodingStandard\Fixer\ConstantNotation\RemoveSlashFromNativeConstantFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function (ECSConfig $ecsConfig): void {
$ecsConfig->rule(RemoveSlashFromNativeConstantFixer::class);
};
Loading