Skip to content

Commit

Permalink
Improve config builder + add withAttributesSets() method (#5509)
Browse files Browse the repository at this point in the history
* use single cache directive with details

* use single method to configure imports

* add withAttributesSets() to make upgrading to attributes easy

* tidy
  • Loading branch information
TomasVotruba committed Jan 27, 2024
1 parent 1fe8f3a commit b2d76bd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 42 deletions.
1 change: 0 additions & 1 deletion composer-unused.php
Expand Up @@ -9,7 +9,6 @@

return static function (Configuration $configuration): Configuration {
// rector dependencies
$configuration->addPatternFilter(PatternFilter::fromString('#rector/.#'));
$configuration->addPatternFilter(PatternFilter::fromString('#phpstan/phpstan#'));

return $configuration;
Expand Down
3 changes: 1 addition & 2 deletions rector.php
Expand Up @@ -36,8 +36,7 @@
__DIR__ . '/config',
])
->withRootFiles()
->withImportNames()
->withRemoveUnusedImports()
->withImportNames(removeUnusedImports: true)
->withSkip([
StringClassNameToClassConstantRector::class,
__DIR__ . '/bin/validate-phpstan-version.php',
Expand Down
106 changes: 68 additions & 38 deletions src/Configuration/RectorConfigBuilder.php
Expand Up @@ -7,7 +7,13 @@
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
use Rector\Config\RectorConfig;
use Rector\Contract\Rector\RectorInterface;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Symfony\Set\FOSRestSetList;
use Rector\Symfony\Set\JMSSetList;
use Rector\Symfony\Set\SensiolabsSetList;
use Rector\Symfony\Set\SymfonySetList;
use Rector\ValueObject\PhpVersion;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -92,8 +98,6 @@ final class RectorConfigBuilder

private int $indentSize = 4;

private ?string $phpstanConfig = null;

/**
* @var string[]
*/
Expand Down Expand Up @@ -160,10 +164,6 @@ public function __invoke(RectorConfig $rectorConfig): void
$rectorConfig->indent($this->indentChar, $this->indentSize);
}

if ($this->phpstanConfig !== null) {
$rectorConfig->phpstanConfig($this->phpstanConfig);
}

if ($this->phpstanConfigs !== []) {
$rectorConfig->phpstanConfigs($this->phpstanConfigs);
}
Expand Down Expand Up @@ -231,6 +231,54 @@ public function withSets(array $sets): self
return $this;
}

/**
* Upgrade your annotations to attributes
*/
public function withAttributesSets(
bool $symfony = false,
bool $doctrine = false,
bool $mongoDb = false,
bool $gedmo = false,
bool $phpunit = false,
bool $fosRest = false,
bool $jms = false,
bool $sensiolabs = false,
): self {
if ($symfony) {
$this->sets[] = SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES;
}

if ($doctrine) {
$this->sets[] = DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES;
}

if ($mongoDb) {
$this->sets[] = DoctrineSetList::MONGODB__ANNOTATIONS_TO_ATTRIBUTES;
}

if ($gedmo) {
$this->sets[] = DoctrineSetList::GEDMO_ANNOTATIONS_TO_ATTRIBUTES;
}

if ($phpunit) {
$this->sets[] = PHPUnitSetList::ANNOTATIONS_TO_ATTRIBUTES;
}

if ($fosRest) {
$this->sets[] = FOSRestSetList::ANNOTATIONS_TO_ATTRIBUTES;
}

if ($jms) {
$this->sets[] = JMSSetList::ANNOTATIONS_TO_ATTRIBUTES;
}

if ($sensiolabs) {
$this->sets[] = SensiolabsSetList::ANNOTATIONS_TO_ATTRIBUTES;
}

return $this;
}

public function withPreparedSets(
bool $deadCode = false,
bool $codeQuality = false,
Expand Down Expand Up @@ -301,20 +349,17 @@ public function withFileExtensions(array $fileExtensions): self
return $this;
}

public function withCacheDirectory(string $cacheDirectory, ?string $containerCacheDirectory = null): self
{
$this->cacheDirectory = $cacheDirectory;
$this->containerCacheDirectory = $containerCacheDirectory;

return $this;
}

/**
* @param class-string<CacheStorageInterface> $cacheClass
* @param class-string<CacheStorageInterface>|null $cacheClass
*/
public function withClassCache(string $cacheClass): self
{
public function withCache(
?string $cacheDirectory = null,
?string $cacheClass = null,
?string $containerCacheDirectory = null
): self {
$this->cacheDirectory = $cacheDirectory;
$this->cacheClass = $cacheClass;
$this->containerCacheDirectory = $containerCacheDirectory;

return $this;
}
Expand Down Expand Up @@ -359,25 +404,16 @@ public function withoutParallel(): self
return $this;
}

public function withImportNames(bool $importNames = true, bool $importDocBlockNames = true): self
{
public function withImportNames(
bool $importNames = true,
bool $importDocBlockNames = true,
bool $importShortClasses = true,
bool $removeUnusedImports = false
): self {
$this->importNames = $importNames;
$this->importDocBlockNames = $importDocBlockNames;

return $this;
}

public function withImporShortClasses(bool $importShortClasses = true): self
{
$this->importShortClasses = $importShortClasses;

return $this;
}

public function withRemoveUnusedImports(bool $removeUnusedImports = false): self
{
$this->removeUnusedImports = $removeUnusedImports;

return $this;
}

Expand Down Expand Up @@ -419,12 +455,6 @@ public function withBootstrapFiles(array $bootstrapFiles): self
return $this;
}

public function withPHPStanConfig(string $phpstanConfig): self
{
$this->phpstanConfig = $phpstanConfig;
return $this;
}

/**
* @param string[] $phpstanConfigs
*/
Expand Down
2 changes: 1 addition & 1 deletion templates/custom-rule/utils/rector/src/Rector/__Name__.php
Expand Up @@ -10,7 +10,7 @@
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \rector\tests\Rector\__Name__\__Name__Test
* @see \Utils\Rector\Tests\Rector\__Name__\__Name__Test
*/
final class __Name__ extends AbstractRector
{
Expand Down

0 comments on commit b2d76bd

Please sign in to comment.