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 .dev-tools/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"shipmonk/phpstan-rules": "^4.1.4",
"squizlabs/php_codesniffer": "^3.13.2",
"tomasvotruba/type-coverage": "^2.0.2",
"vimeo/psalm": "^6.12.1"
"vimeo/psalm": "^6.13.0"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 5 additions & 5 deletions .dev-tools/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .dev-tools/psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<directory name='../src' />
</projectFiles>

<stubs>
<file name='./psalm_stub.php' preloadClasses='true' />
</stubs>

<issueHandlers>
<DeprecatedClass errorLevel='suppress' />
<InaccessibleMethod errorLevel='suppress' />
Expand Down
22 changes: 22 additions & 0 deletions .dev-tools/psalm_stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

/*
* This file is part of PHP CS Fixer: custom fixers.
*
* (c) 2018 Kuba Werłos
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace PhpCsFixer\Tokenizer {
final class FCT
{
public const int T_ATTRIBUTE = \T_ATTRIBUTE;
public const int T_PRIVATE_SET = \T_PRIVATE_SET;
public const int T_PROTECTED_SET = \T_PROTECTED_SET;
public const int T_PUBLIC_SET = \T_PUBLIC_SET;
public const int T_READONLY = \T_READONLY;
}
final class OtherClassSoTheNameOfClassAboveIsNotChanged {}
}
29 changes: 11 additions & 18 deletions src/Fixer/NoUselessWriteVisibilityFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\FCT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand All @@ -23,19 +24,11 @@
*/
final class NoUselessWriteVisibilityFixer extends AbstractFixer
{
/** @var non-empty-array<int, list<int>> */
private array $predecessorKindMap;

public function __construct()
{
if (\defined('T_PUBLIC_SET')) {
$this->predecessorKindMap = [
\T_PUBLIC_SET => [\T_PUBLIC, CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC],
\T_PROTECTED_SET => [\T_PROTECTED, CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED],
\T_PRIVATE_SET => [\T_PRIVATE, CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE],
];
}
}
private const PREDECESSOR_KIND_MAP = [
FCT::T_PUBLIC_SET => [\T_PUBLIC, CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC],
FCT::T_PROTECTED_SET => [\T_PROTECTED, CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED],
FCT::T_PRIVATE_SET => [\T_PRIVATE, CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE],
];

public function getDefinition(): FixerDefinitionInterface
{
Expand All @@ -61,7 +54,7 @@ public function getPriority(): int

public function isCandidate(Tokens $tokens): bool
{
return \defined('T_PUBLIC_SET') && $tokens->isAnyTokenKindsFound(\array_keys($this->predecessorKindMap));
return $tokens->isAnyTokenKindsFound([FCT::T_PUBLIC_SET, FCT::T_PROTECTED_SET, FCT::T_PRIVATE_SET]);
}

public function isRisky(): bool
Expand All @@ -71,14 +64,14 @@ public function isRisky(): bool

public function fix(\SplFileInfo $file, Tokens $tokens): void
{
foreach ($tokens->findGivenKind(\array_keys($this->predecessorKindMap)) as $kind => $elements) {
foreach ($tokens->findGivenKind([FCT::T_PUBLIC_SET, FCT::T_PROTECTED_SET, FCT::T_PRIVATE_SET]) as $kind => $elements) {
foreach (\array_keys($elements) as $index) {
$this->fixVisibility($tokens, $index, $kind, $kind === \T_PUBLIC_SET);
self::fixVisibility($tokens, $index, $kind, $kind === \T_PUBLIC_SET);
}
}
}

private function fixVisibility(Tokens $tokens, int $index, int $kind, bool $makePublicIfNone): void
private static function fixVisibility(Tokens $tokens, int $index, int $kind, bool $makePublicIfNone): void
{
$prevIndex = $tokens->getPrevMeaningfulToken($index);
\assert(\is_int($prevIndex));
Expand All @@ -87,7 +80,7 @@ private function fixVisibility(Tokens $tokens, int $index, int $kind, bool $make
\assert(\is_int($prevIndex));
}

if (!$tokens[$prevIndex]->isGivenKind($this->predecessorKindMap[$kind])) {
if (!$tokens[$prevIndex]->isGivenKind(self::PREDECESSOR_KIND_MAP[$kind])) {
if ($makePublicIfNone) {
$prevDeciderIndex = $tokens->getPrevTokenOfKind($index, ['(', ';', '{']);
\assert(\is_int($prevDeciderIndex));
Expand Down
12 changes: 3 additions & 9 deletions src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\FCT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixerCustomFixers\TokenRemover;
Expand Down Expand Up @@ -96,7 +97,7 @@ private static function getIndexAfterPhpDoc(Tokens $tokens, int $index): ?int
{
$nextIndex = $tokens->getNextMeaningfulToken($index);

while ($nextIndex !== null && \defined('T_ATTRIBUTE') && $tokens[$nextIndex]->isGivenKind(\T_ATTRIBUTE)) {
while ($nextIndex !== null && $tokens[$nextIndex]->isGivenKind(FCT::T_ATTRIBUTE)) {
$nextIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ATTRIBUTE, $nextIndex);
$nextIndex = $tokens->getNextMeaningfulToken($nextIndex);
}
Expand All @@ -106,14 +107,7 @@ private static function getIndexAfterPhpDoc(Tokens $tokens, int $index): ?int

private static function removeForClassElement(Tokens $tokens, int $index, int $propertyStartIndex): void
{
$tokenKinds = [\T_NS_SEPARATOR, \T_STATIC, \T_STRING, \T_WHITESPACE, CT::T_ARRAY_TYPEHINT, CT::T_NULLABLE_TYPE, CT::T_TYPE_ALTERNATION];

if (\defined('T_READONLY')) {
$tokenKinds[] = CT::T_TYPE_INTERSECTION;
$tokenKinds[] = \T_READONLY;
}

$variableIndex = $tokens->getTokenNotOfKindsSibling($propertyStartIndex, 1, $tokenKinds);
$variableIndex = $tokens->getTokenNotOfKindsSibling($propertyStartIndex, 1, [\T_NS_SEPARATOR, \T_STATIC, \T_STRING, \T_WHITESPACE, CT::T_ARRAY_TYPEHINT, CT::T_NULLABLE_TYPE, CT::T_TYPE_ALTERNATION, CT::T_TYPE_INTERSECTION, FCT::T_READONLY]);
\assert(\is_int($variableIndex));

if (!$tokens[$variableIndex]->isGivenKind(\T_VARIABLE)) {
Expand Down
38 changes: 15 additions & 23 deletions src/Fixer/ReadonlyPromotedPropertiesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpCsFixer\FixerDefinition\VersionSpecification;
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\FCT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixerCustomFixers\Analyzer\ConstructorAnalyzer;
Expand Down Expand Up @@ -43,23 +44,14 @@ final class ReadonlyPromotedPropertiesFixer extends AbstractFixer
[\T_COALESCE_EQUAL, '??='],
[\T_CONCAT_EQUAL, '.='],
];

/** @var list<int> */
private array $promotedPropertyVisibilityKinds;

public function __construct()
{
$this->promotedPropertyVisibilityKinds = [
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED,
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
];
if (\defined('T_PUBLIC_SET')) {
$this->promotedPropertyVisibilityKinds[] = \T_PUBLIC_SET;
$this->promotedPropertyVisibilityKinds[] = \T_PROTECTED_SET;
$this->promotedPropertyVisibilityKinds[] = \T_PRIVATE_SET;
}
}
private const PROMOTED_PROPERTY_VISIBILITY_KINDS = [
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED,
CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
FCT::T_PUBLIC_SET,
FCT::T_PROTECTED_SET,
FCT::T_PRIVATE_SET,
];

public function getDefinition(): FixerDefinitionInterface
{
Expand Down Expand Up @@ -92,7 +84,7 @@ public function getPriority(): int

public function isCandidate(Tokens $tokens): bool
{
return \defined('T_READONLY') && $tokens->isAnyTokenKindsFound($this->promotedPropertyVisibilityKinds);
return $tokens->isAnyTokenKindsFound(self::PROMOTED_PROPERTY_VISIBILITY_KINDS);
}

public function isRisky(): bool
Expand Down Expand Up @@ -126,7 +118,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
\assert(\is_int($constructorOpenParenthesisIndex));
$constructorCloseParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $constructorOpenParenthesisIndex);

$this->fixParameters(
self::fixParameters(
$tokens,
$classOpenBraceIndex,
$classCloseBraceIndex,
Expand All @@ -146,7 +138,7 @@ private static function isClassReadonly(Tokens $tokens, int $index): bool
return $tokens[$index]->isGivenKind(\T_READONLY);
}

private function fixParameters(
private static function fixParameters(
Tokens $tokens,
int $classOpenBraceIndex,
int $classCloseBraceIndex,
Expand All @@ -158,7 +150,7 @@ private function fixParameters(
continue;
}

$insertIndex = $this->getInsertIndex($tokens, $index);
$insertIndex = self::getInsertIndex($tokens, $index);
if ($insertIndex === null) {
continue;
}
Expand All @@ -177,7 +169,7 @@ private function fixParameters(
}
}

private function getInsertIndex(Tokens $tokens, int $index): ?int
private static function getInsertIndex(Tokens $tokens, int $index): ?int
{
$insertIndex = null;

Expand All @@ -189,7 +181,7 @@ private function getInsertIndex(Tokens $tokens, int $index): ?int
if ($tokens[$index]->isGivenKind(\T_READONLY)) {
return null;
}
if ($insertIndex === null && $tokens[$index]->isGivenKind($this->promotedPropertyVisibilityKinds)) {
if ($insertIndex === null && $tokens[$index]->isGivenKind(self::PROMOTED_PROPERTY_VISIBILITY_KINDS)) {
$insertIndex = $index;
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/AutoReview/SrcCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ public function testFixerSupportsAllFilesByDefault(): void
/**
* @param class-string $className
*
* @dataProvider provideThereIsNoPregFunctionUsedDirectlyCases
* @dataProvider provideThereIsNoDisallowedFunctionUsedDirectlyCases
*/
public function testThereIsNoPregFunctionUsedDirectly(string $className): void
public function testThereIsNoDisallowedFunctionUsedDirectly(string $className): void
{
$reflectionClass = new \ReflectionClass($className);

Expand All @@ -113,27 +113,27 @@ public function testThereIsNoPregFunctionUsedDirectly(string $className): void
$stringTokens,
);
$strings = \array_unique($strings);
$message = \sprintf('Class %s must not use preg_*, it shall use Preg::* instead.', $className);

$message = \sprintf('Class %s must not use preg_*, it shall use Preg::* instead.', $className);
self::assertNotContains('preg_filter', $strings, $message);
self::assertNotContains('preg_grep', $strings, $message);
self::assertNotContains('preg_match', $strings, $message);
self::assertNotContains('preg_match_all', $strings, $message);
self::assertNotContains('preg_replace', $strings, $message);
self::assertNotContains('preg_replace_callback', $strings, $message);
self::assertNotContains('preg_split', $strings, $message);

self::assertNotContains('defined', $strings);
}

/**
* @return iterable<array{string}>
*/
public static function provideThereIsNoPregFunctionUsedDirectlyCases(): iterable
public static function provideThereIsNoDisallowedFunctionUsedDirectlyCases(): iterable
{
$finder = Finder::create()
->files()
->in(__DIR__ . '/../../src')
->notName('php-cs-fixer.config.*.php')
->notName('run')
->sortByName();

/** @var SplFileInfo $file */
Expand Down
Loading