Skip to content

Commit

Permalink
Remove collector interface and fix SpatieEnumClassToEnumRector alread…
Browse files Browse the repository at this point in the history
…y has underscore to double underscore (#5473)

* remove CollectorInterface tagging

* regenerate docs

* [ci-review] Rector Rectify

* remove Collector interface

* fix hard to read assignment

* [ci-review] Rector Rectify

* update test

* update test

* fix multi underscore on Enum

* move value to constant

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jan 16, 2024
1 parent ee89fb7 commit 07df7dc
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 97 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/code_analysis.yaml
Expand Up @@ -44,8 +44,7 @@ jobs:
--skip-type="Rector\\NodeTypeResolver\\PHPStan\\Scope\\Contract\\NodeVisitor\\ScopeResolverNodeVisitorInterface" \
--skip-type="Rector\\BetterPhpDocParser\\Contract\\BasePhpDocNodeVisitorInterface" \
--skip-type="Rector\\BetterPhpDocParser\\Contract\\PhpDocParser\\PhpDocNodeDecoratorInterface" \
--skip-type="Rector\\BetterPhpDocParser\\ValueObject\\Type\\FullyQualifiedIdentifierTypeNode" \
--skip-type="Rector\\Contract\\Rector\\CollectorRectorInterface"
--skip-type="Rector\\BetterPhpDocParser\\ValueObject\\Type\\FullyQualifiedIdentifierTypeNode"
-
name: 'Compatible PHPStan versions'
Expand Down
25 changes: 4 additions & 21 deletions build/target-repository/docs/rector_rules_overview.md
@@ -1,4 +1,4 @@
# 355 Rules Overview
# 354 Rules Overview

<br>

Expand Down Expand Up @@ -46,7 +46,7 @@

- [Php83](#php83) (3)

- [Privatization](#privatization) (5)
- [Privatization](#privatization) (4)

- [Removing](#removing) (5)

Expand Down Expand Up @@ -5136,6 +5136,8 @@ Decorate read-only property with `readonly` attribute

Refactor Spatie enum class to native Enum

:wrench: **configure it!**

- class: [`Rector\Php81\Rector\Class_\SpatieEnumClassToEnumRector`](../rules/Php81/Rector/Class_/SpatieEnumClassToEnumRector.php)

```diff
Expand Down Expand Up @@ -5304,25 +5306,6 @@ Combine separated host and port on `ldap_connect()` args

## Privatization

### FinalizeClassesWithoutChildrenCollectorRector

Finalize classes without children using collectors

- class: [`Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenCollectorRector`](../rules/Privatization/Rector/Class_/FinalizeClassesWithoutChildrenCollectorRector.php)

```diff
-class FirstClass extends SecondClass
+final class FirstClass extends SecondClass
{
}

class SecondClass
{
}
```

<br>

### FinalizeClassesWithoutChildrenRector

Finalize every class that has no children
Expand Down
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\Php81\Rector\Class_\FixtureUpperCaseSnakeCase\FixtureUpperCaseSnakeCase;

use Spatie\Enum\Enum;

/**
* @method static self isOK()
* @method static self needsReview()
* @method static self isV2()
* @method static self IS_OBSOLETE()
*/
class StatusEnum extends Enum
{
}

?>
-----
<?php

namespace Rector\Tests\Php81\Rector\Class_\FixtureUpperCaseSnakeCase\FixtureUpperCaseSnakeCase;

use Spatie\Enum\Enum;

enum StatusEnum : string
{
case IS_OK = 'isOK';
case NEEDS_REVIEW = 'needsReview';
case IS_V_2 = 'isV2';
case IS_OBSOLETE = 'IS_OBSOLETE';
}

?>
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php81\Rector\Class_\SpatieEnumClassToEnumRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

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

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

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule_uppercase_snake_case.php';
}
}
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php81\Rector\Class_\SpatieEnumClassToEnumRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(SpatieEnumClassToEnumRector::class, [
SpatieEnumClassToEnumRector::TO_UPPER_SNAKE_CASE => true,
]);
};
7 changes: 7 additions & 0 deletions rules/Php81/NodeFactory/EnumFactory.php
Expand Up @@ -36,6 +36,12 @@
*/
private const PASCAL_CASE_TO_UNDERSCORE_REGEX = '/(?<=[A-Z])(?=[A-Z][a-z])|(?<=[^A-Z])(?=[A-Z])|(?<=[A-Za-z])(?=[^A-Za-z])/';

/**
* @var string
* @see https://regex101.com/r/FneU33/1
*/
private const MULTI_UNDERSCORES_REGEX = '#_{2,}#';

public function __construct(
private NodeNameResolver $nodeNameResolver,
private PhpDocInfoFactory $phpDocInfoFactory,
Expand Down Expand Up @@ -142,6 +148,7 @@ private function createEnumCaseFromDocComment(
$enumName = strtoupper(
Strings::replace($nodeValue->methodName, self::PASCAL_CASE_TO_UNDERSCORE_REGEX, '_$0')
);
$enumName = Strings::replace($enumName, self::MULTI_UNDERSCORES_REGEX, '_');
} else {
$enumName = strtoupper($nodeValue->methodName);
}
Expand Down
16 changes: 12 additions & 4 deletions rules/Php81/Rector/Class_/SpatieEnumClassToEnumRector.php
Expand Up @@ -13,7 +13,7 @@
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
Expand All @@ -26,6 +26,11 @@ final class SpatieEnumClassToEnumRector extends AbstractRector implements MinPhp
{
private bool $toUpperSnakeCase = false;

/**
* @var string
*/
public const TO_UPPER_SNAKE_CASE = 'toUpperSnakeCase';

public function __construct(
private readonly EnumFactory $enumFactory
) {
Expand All @@ -39,7 +44,7 @@ public function provideMinPhpVersion(): int
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Refactor Spatie enum class to native Enum', [
new CodeSample(
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
use \Spatie\Enum\Enum;
Expand All @@ -61,7 +66,10 @@ enum StatusEnum : string
case PUBLISHED = 'published';
case ARCHIVED = 'archived';
}
CODE_SAMPLE
CODE_SAMPLE,
[
SpatieEnumClassToEnumRector::TO_UPPER_SNAKE_CASE => false,
]
),
]);
}
Expand Down Expand Up @@ -91,6 +99,6 @@ public function refactor(Node $node): ?Enum_
*/
public function configure(array $configuration): void
{
$this->toUpperSnakeCase = true === ($configuration['toUpperSnakeCase'] ?? false);
$this->toUpperSnakeCase = $configuration[self::TO_UPPER_SNAKE_CASE] ?? false;
}
}
5 changes: 0 additions & 5 deletions src/Config/RectorConfig.php
Expand Up @@ -9,7 +9,6 @@
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Contract\Rector\CollectorRectorInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Contract\Rector\RectorInterface;
use Rector\DependencyInjection\Laravel\ContainerMemento;
Expand Down Expand Up @@ -187,10 +186,6 @@ public function rule(string $rectorClass): void
$this->singleton($rectorClass);
$this->tag($rectorClass, RectorInterface::class);

if (is_a($rectorClass, CollectorRectorInterface::class, true)) {
$this->tag($rectorClass, CollectorRectorInterface::class);
}

// for cache invalidation in case of change
SimpleParameterProvider::addParameter(Option::REGISTERED_RECTOR_RULES, $rectorClass);
}
Expand Down
5 changes: 0 additions & 5 deletions src/DependencyInjection/LazyContainerFactory.php
Expand Up @@ -58,7 +58,6 @@
use Rector\Console\Style\RectorStyle;
use Rector\Console\Style\SymfonyStyleFactory;
use Rector\Contract\DependencyInjection\ResetableInterface;
use Rector\Contract\Rector\CollectorRectorInterface;
use Rector\Contract\Rector\RectorInterface;
use Rector\NodeDecorator\CreatedByRuleDecorator;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
Expand Down Expand Up @@ -435,10 +434,6 @@ public function create(): RectorConfig
->needs('$rectors')
->giveTagged(RectorInterface::class);

$rectorConfig->when(RectorNodeTraverser::class)
->needs('$collectorRectors')
->giveTagged(CollectorRectorInterface::class);

$rectorConfig->when(ConfigInitializer::class)
->needs('$rectors')
->giveTagged(RectorInterface::class);
Expand Down
11 changes: 1 addition & 10 deletions src/PhpParser/NodeTraverser/RectorNodeTraverser.php
Expand Up @@ -6,7 +6,6 @@

use PhpParser\Node;
use PhpParser\NodeTraverser;
use Rector\Contract\Rector\CollectorRectorInterface;
use Rector\Contract\Rector\RectorInterface;
use Rector\VersionBonding\PhpVersionedFilter;

Expand Down Expand Up @@ -60,15 +59,7 @@ private function prepareNodeVisitors(): void
}

// filer out by version
$activeRectors = $this->phpVersionedFilter->filter($this->rectors);

$nonCollectorActiveRectors = array_filter(
$activeRectors,
static fn (RectorInterface $rector): bool => ! $rector instanceof CollectorRectorInterface
);

$this->visitors = [...$this->visitors, ...$nonCollectorActiveRectors];

$this->visitors = $this->phpVersionedFilter->filter($this->rectors);
$this->areNodeVisitorsPrepared = true;
}
}
6 changes: 0 additions & 6 deletions src/Testing/PHPUnit/AbstractRectorTestCase.php
Expand Up @@ -8,7 +8,6 @@
use Iterator;
use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use PHPStan\Collectors\Collector;
use PHPUnit\Framework\ExpectationFailedException;
use Rector\Application\ApplicationFileProcessor;
use Rector\Autoloading\AdditionalAutoloader;
Expand All @@ -17,7 +16,6 @@
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Contract\DependencyInjection\ResetableInterface;
use Rector\Contract\Rector\CollectorRectorInterface;
use Rector\Contract\Rector\RectorInterface;
use Rector\DependencyInjection\Laravel\ContainerMemento;
use Rector\Exception\ShouldNotHappenException;
Expand Down Expand Up @@ -89,8 +87,6 @@ protected function setUp(): void

// this has to be always empty, so we can add new rules with their configuration
$this->assertEmpty($rectorConfig->tagged(RectorInterface::class));
$this->assertEmpty($rectorConfig->tagged(CollectorRectorInterface::class));
$this->assertEmpty($rectorConfig->tagged(Collector::class));

$this->bootFromConfigFiles([$configFile]);

Expand Down Expand Up @@ -174,8 +170,6 @@ protected function forgetRectorsRulesAndCollectors(): void

// 1. forget tagged services
ContainerMemento::forgetTag($rectorConfig, RectorInterface::class);
ContainerMemento::forgetTag($rectorConfig, Collector::class);
ContainerMemento::forgetTag($rectorConfig, CollectorRectorInterface::class);

// 2. remove after binding too, to avoid setting configuration over and over again
$privatesAccessor = new PrivatesAccessor();
Expand Down
44 changes: 0 additions & 44 deletions stubs/PHPStan/Collectors/Collector.php

This file was deleted.

0 comments on commit 07df7dc

Please sign in to comment.