Skip to content

Commit

Permalink
remove 3rd party support, as will be handled in rector core
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 24, 2024
1 parent 9231a51 commit a98fff3
Show file tree
Hide file tree
Showing 10 changed files with 3 additions and 235 deletions.
19 changes: 0 additions & 19 deletions src/FileSystem/TemplateFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ final class TemplateFileSystem
*/
private const FIXTURE_SHORT_REGEX = '#/Fixture/#';

/**
* @var string
* @see https://regex101.com/r/HBcfXd/1
*/
private const PACKAGE_RULES_PATH_REGEX = '#(rules)\/__Package__#i';

/**
* @var string
* @see https://regex101.com/r/HBcfXd/1
*/
private const PACKAGE_RULES_TESTS_PATH_REGEX = '#(rules-tests)\/__Package__#i';

public function __construct(
private readonly TemplateFactory $templateFactory,
private readonly Filesystem $filesystem,
Expand All @@ -49,13 +37,6 @@ public function resolveDestination(
$destination = $this->resolveRelativeFilepath($filePath);
$destination = $this->changeRootPathForRootPackage($rectorRecipe, $destination);

// normalize core package
if (! $rectorRecipe->isRectorRepository()) {
// special keyword for 3rd party Rectors, not for core Github contribution
$destination = Strings::replace($destination, self::PACKAGE_RULES_PATH_REGEX, 'utils/rector/src');
$destination = Strings::replace($destination, self::PACKAGE_RULES_TESTS_PATH_REGEX, 'utils/rector/tests');
}

$destination = $this->templateFactory->create($destination, $templateVariables);

// remove ".inc" protection from PHPUnit if not a test case
Expand Down
18 changes: 0 additions & 18 deletions src/Generator/FileGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@

final class FileGenerator
{
/**
* @var string
* @see https://regex101.com/r/RVbPEX/1
*/
private const RECTOR_UTILS_REGEX = '#Rector\\\\Utils#';

/**
* @var string
* @see https://regex101.com/r/RVbPEX/1
*/
private const RECTOR_UTILS_TESTS_REGEX = '#Rector\\\\Tests\\\\Utils#';

public function __construct(
private readonly Filesystem $filesystem,
private readonly TemplateFactory $templateFactory,
Expand Down Expand Up @@ -77,12 +65,6 @@ private function generateFileInfoWithTemplateVariables(

$content = $this->templateFactory->create($templateFileContents, $templateVariables);

// replace "Rector\Utils\" with "Utils\Rector\" for 3rd party packages
if (! $rectorRecipe->isRectorRepository()) {
$content = Strings::replace($content, self::RECTOR_UTILS_REGEX, 'Utils\Rector');
$content = Strings::replace($content, self::RECTOR_UTILS_TESTS_REGEX, 'Utils\Rector\Tests');
}

// correct tests PSR-4 namespace for core rector packages
if (in_array($rectorRecipe->getPackage(), Packages::RECTOR_CORE, true)) {
$content = Strings::replace(
Expand Down
50 changes: 0 additions & 50 deletions src/ValueObject/RectorRecipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\RectorGenerator\ValueObject;

use Nette\Utils\Json;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
Expand All @@ -25,8 +24,6 @@ final class RectorRecipe

private ?string $codeAfter = null;

private bool $isRectorRepository;

private ?string $category = null;

/**
Expand All @@ -50,8 +47,6 @@ public function __construct(
string $codeBefore,
string $codeAfter
) {
$this->isRectorRepository = $this->detectRectorRepository();

$this->setPackage($package);
$this->setName($name);
$this->setNodeTypes($nodeTypes);
Expand Down Expand Up @@ -99,26 +94,11 @@ public function getCodeAfter(): string
return $this->codeAfter;
}

public function isRectorRepository(): bool
{
return $this->isRectorRepository;
}

public function getCategory(): string
{
return $this->category;
}

/**
* For testing purposes
*
* @api
*/
public function setIsRectorRepository(bool $isRectorRepository): void
{
$this->isRectorRepository = $isRectorRepository;
}

/**
* For tests
*
Expand Down Expand Up @@ -204,34 +184,4 @@ private function normalizeCode(string $code): string

return trim($code);
}

private function detectRectorRepository(): bool
{
$possibleComposerJsonFilePaths = [
__DIR__ . '/../../../../../composer.json',
__DIR__ . '/../../composer.json',
];

foreach ($possibleComposerJsonFilePaths as $possibleComposerJsonFilePath) {
if (! file_exists($possibleComposerJsonFilePath)) {
continue;
}

$composerJsonContent = file_get_contents($possibleComposerJsonFilePath);
if ($composerJsonContent === false) {
continue;
}

$composerJson = Json::decode($composerJsonContent, Json::FORCE_ARRAY);
if (! isset($composerJson['name'])) {
continue;
}

if (\str_starts_with((string) $composerJson['name'], 'rector/')) {
return true;
}
}

return false;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

25 changes: 1 addition & 24 deletions tests/RectorGenerator/RectorGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Nette\Utils\FileSystem;
use Rector\RectorGenerator\Generator\RectorGenerator;
use Rector\RectorGenerator\Tests\RectorGenerator\Source\StaticRectorRecipeFactory;
use Rector\RectorGenerator\ValueObject\RectorRecipe;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
Expand Down Expand Up @@ -37,7 +36,7 @@ protected function tearDown(): void

public function test(): void
{
$rectorRecipe = $this->createConfiguration(__DIR__ . '/Source/config/some_set.php', true);
$rectorRecipe = StaticRectorRecipeFactory::createRectorRecipe();
$this->rectorGenerator->generate($rectorRecipe, self::DESTINATION_DIRECTORY);

$fileInfos = $this->findFileInfos(__DIR__ . '/Fixture/expected/');
Expand All @@ -52,28 +51,6 @@ public function test(): void
}
}

public function test3rdParty(): void
{
$rectorRecipe = $this->createConfiguration(__DIR__ . '/Source/config/some_set.php', false);
$this->rectorGenerator->generate($rectorRecipe, self::DESTINATION_DIRECTORY);

$fileInfos = $this->findFileInfos(__DIR__ . '/Fixture/expected_3rd_party/');

foreach ($fileInfos as $fileInfo) {
$this->assertFileExists(self::DESTINATION_DIRECTORY . '/' . $fileInfo->getRelativePathname());

$this->assertFileEquals(
$fileInfo->getRealPath(),
self::DESTINATION_DIRECTORY . '/' . $fileInfo->getRelativePathname()
);
}
}

private function createConfiguration(string $setFilePath, bool $isRectorRepository): RectorRecipe
{
return StaticRectorRecipeFactory::createRectorRecipe($setFilePath, $isRectorRepository);
}

/**
* @return SplFileInfo[]
*/
Expand Down
12 changes: 2 additions & 10 deletions tests/RectorGenerator/Source/StaticRectorRecipeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@
*/
final class StaticRectorRecipeFactory
{
public static function createRectorRecipe(string $setFilePath, bool $isRectorRepository): RectorRecipe
public static function createRectorRecipe(): RectorRecipe
{
if (! file_exists($setFilePath)) {
$message = sprintf('Set file path "%s" was not found', $setFilePath);
throw new ShouldNotHappenException($message);
}

$rectorRecipe = new RectorRecipe(
'Utils',
'WhateverRector',
Expand All @@ -40,10 +35,7 @@ public static function createRectorRecipe(string $setFilePath, bool $isRectorRep
CODE_SAMPLE
);

$rectorRecipe->setIsRectorRepository($isRectorRepository);
if ($isRectorRepository) {
$rectorRecipe->setPackage('ModeratePackage');
}
$rectorRecipe->setPackage('ModeratePackage');

return $rectorRecipe;
}
Expand Down
9 changes: 0 additions & 9 deletions tests/RectorGenerator/Source/config/some_set.php

This file was deleted.

0 comments on commit a98fff3

Please sign in to comment.