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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# CHANGELOG for PHP CS Fixer: custom fixers

## v3.25.0
- Add NoUselessWriteVisibilityFixer
- ReadonlyPromotedPropertiesFixer - support asymmetric visibility

## v3.24.0
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
![Tests](https://img.shields.io/badge/tests-3607-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-3632-brightgreen.svg)
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)

[![CI status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml/badge.svg)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml)
Expand Down Expand Up @@ -399,6 +399,19 @@ Functions `strlen` and `mb_strlen` must not be compared to 0.
+$isNotEmpty = $string !== '';
```

#### NoUselessWriteVisibilityFixer
There must be no useless write visibility.
```diff
<?php class Foo {
- public public(set) $x;
- public(set) $y;
- protected protected(set) $z;
+ public $x;
+ public $y;
+ protected $z;
}
```

#### NumericLiteralSeparatorFixer
Numeric literals must have configured separators.
DEPRECATED: use `numeric_literal_separator` instead.
Expand Down
104 changes: 104 additions & 0 deletions src/Fixer/NoUselessWriteVisibilityFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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 PhpCsFixerCustomFixers\Fixer;

use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

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],
];
}
}

public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'There must be no useless write visibility.',
[new CodeSample(
<<<'PHP'
<?php class Foo {
public public(set) $x;
public(set) $y;
protected protected(set) $z;
}

PHP,
)],
);
}

public function getPriority(): int
{
return 0;
}

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

public function isRisky(): bool
{
return false;
}

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

private function fixVisibility(Tokens $tokens, int $index, int $kind, bool $makePublicIfNone): void
{
$prevIndex = $tokens->getPrevMeaningfulToken($index);
\assert(\is_int($prevIndex));
if ($tokens[$prevIndex]->isGivenKind(\T_ABSTRACT)) {
$prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
\assert(\is_int($prevIndex));
}

if (!$tokens[$prevIndex]->isGivenKind($this->predecessorKindMap[$kind])) {
if ($makePublicIfNone) {
$prevDeciderIndex = $tokens->getPrevTokenOfKind($index, ['(', ';', '{']);
\assert(\is_int($prevDeciderIndex));
$kind = $tokens[$prevDeciderIndex]->equals('(') ? CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC : \T_PUBLIC;
$tokens[$index] = new Token([$kind, 'public']);
}

return;
}

$tokens->clearAt($index);

if ($tokens[$index + 1]->isWhitespace()) {
$tokens->clearAt($index + 1);
}
}
}
127 changes: 127 additions & 0 deletions tests/Fixer/NoUselessWriteVisibilityFixerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?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 Tests\Fixer;

/**
* @internal
*
* @covers \PhpCsFixerCustomFixers\Fixer\NoUselessWriteVisibilityFixer
*
* @requires PHP >= 8.4
*/
final class NoUselessWriteVisibilityFixerTest extends AbstractFixerTestCase
{
public function testIsRisky(): void
{
self::assertRiskiness(false);
}

/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}

/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'class properties' => [
<<<'PHP'
<?php class Foo {
public int $x;
protected int $y;
private int $z;
}
PHP,
<<<'PHP'
<?php class Foo {
public public(set) int $x;
protected protected(set) int $y;
private private(set) int $z;
}
PHP,
];

yield 'only write visibility' => [
<<<'PHP'
<?php class Foo {
public string $a;
public string $b;
public function __construct(
public string $x,
public string $y,
) {}
public string $c;
public string $d;
}
abstract class Bar {
abstract public function __construct();
public string $a;
}
PHP,
<<<'PHP'
<?php class Foo {
public(set) string $a;
public(set) string $b;
public function __construct(
public(set) string $x,
public(set) string $y,
) {}
public(set) string $c;
public(set) string $d;
}
abstract class Bar {
abstract public function __construct();
public(set) string $a;
}
PHP,
];

yield 'promoted properties' => [
<<<'PHP'
<?php class Foo {
public function __construct(
public string $x,
protected string $y,
private string $z,
) {}
}
PHP,
<<<'PHP'
<?php class Foo {
public function __construct(
public public(set) string $x,
protected protected(set) string $y,
private private(set) string $z,
) {}
}
PHP,
];

yield 'abstract property' => [
<<<'PHP'
<?php abstract class Foo {
public abstract int $x { get; }
}
PHP,
<<<'PHP'
<?php abstract class Foo {
public abstract public(set) int $x { get; }
}
PHP,
];
}
}