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
31 changes: 28 additions & 3 deletions src/Configuration/RectorConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ final class RectorConfigBuilder
'withCodingStyleLevel' => [SetList::CODING_STYLE, 'coding style'],
];

/**
* The composer-based set of the extensions that rector-src does not require, so their set list class cannot be
* imported here. Resolved at run-time; an extension that ships no such set falls back to its set group.
*
* @var array<SetGroup::*, string>
*/
private const array EXTENSION_COMPOSER_BASED_SET_LISTS = [
SetGroup::LARAVEL => 'RectorLaravel\\Set\\LaravelSetList::COMPOSER_BASED',
SetGroup::DRUPAL => 'DrupalRector\\Set\\DrupalSetList::COMPOSER_BASED',
];

/**
* @var string[]
*/
Expand Down Expand Up @@ -741,10 +752,24 @@ public function withComposerBased(
SetGroup::DRUPAL => $drupal,
];

foreach ($setMap as $setPath => $isEnabled) {
if ($isEnabled) {
$this->setGroups[] = $setPath;
foreach ($setMap as $setGroup => $isEnabled) {
if (! $isEnabled) {
continue;
}

$setListConstant = self::EXTENSION_COMPOSER_BASED_SET_LISTS[$setGroup];
if (defined($setListConstant)) {
$setFilePath = constant($setListConstant);
Assert::string($setFilePath);

// single set, as every rule inside is bound to the installed package version on its own
$this->sets[] = $setFilePath;
continue;
}

// @deprecated fallback for extensions that still describe their sets as objects,
// instead of bonding the rules themselves
$this->setGroups[] = $setGroup;
}

if ($phpunit) {
Expand Down
73 changes: 73 additions & 0 deletions tests/Configuration/ExtensionComposerBasedSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Configuration;

use PHPUnit\Framework\TestCase;
use Rector\Configuration\RectorConfigBuilder;
use Rector\Set\Enum\SetGroup;
use ReflectionClass;

final class ExtensionComposerBasedSetTest extends TestCase
{
/**
* Both toggles of withComposerBased() that point at an extension package must have a set list mapped,
* otherwise the lookup hits an undefined array key.
*/
public function testEverySetGroupToggleHasASetListMapped(): void
{
$extensionSetLists = $this->provideExtensionComposerBasedSetLists();

self::assertArrayHasKey(SetGroup::LARAVEL, $extensionSetLists);
self::assertArrayHasKey(SetGroup::DRUPAL, $extensionSetLists);

foreach ($extensionSetLists as $setListConstant) {
self::assertMatchesRegularExpression('#^\w+(\\\\\w+)+::\w+$#', $setListConstant);
}
}

/**
* The extension packages are not required by rector-src, so their constant is undefined here and the
* deprecated set group has to keep working.
*/
public function testFallsBackToTheSetGroupWhenTheExtensionIsNotInstalled(): void
{
foreach ($this->provideExtensionComposerBasedSetLists() as $setListConstant) {
self::assertFalse(defined($setListConstant), $setListConstant);
}

$rectorConfigBuilder = new RectorConfigBuilder()
->withComposerBased(laravel: true, drupal: true);

self::assertSame([SetGroup::LARAVEL, SetGroup::DRUPAL], $this->readPrivateArray($rectorConfigBuilder, 'setGroups'));
self::assertSame([], $this->readPrivateArray($rectorConfigBuilder, 'sets'));
}

/**
* @return array<string, string>
*/
private function provideExtensionComposerBasedSetLists(): array
{
$extensionSetLists = new ReflectionClass(RectorConfigBuilder::class)
->getConstant('EXTENSION_COMPOSER_BASED_SET_LISTS');

self::assertIsArray($extensionSetLists);

return $extensionSetLists;
}

/**
* @return mixed[]
*/
private function readPrivateArray(RectorConfigBuilder $rectorConfigBuilder, string $propertyName): array
{
$value = new ReflectionClass($rectorConfigBuilder)
->getProperty($propertyName)
->getValue($rectorConfigBuilder);

self::assertIsArray($value);

return $value;
}
}
Loading