Skip to content

Commit

Permalink
Add DISABLE_AUTO_IMPORT_DOC_BLOCK_NAMES option (#2526)
Browse files Browse the repository at this point in the history
* -add DISABLE_AUTO_IMPORT_DOC_BLOCK_NAMES option

* -refactoring

* -refactoring

* -refactoring
  • Loading branch information
alexndlm committed Jun 18, 2022
1 parent 1a3288b commit 3e6c9cf
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 7 deletions.
15 changes: 11 additions & 4 deletions packages/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,31 @@ public function skip(array $criteria): void
$parameters->set(Option::SKIP, $criteria);
}

public function importNames(): void
public function importNames(bool $importNames = true, bool $importDocBlockNames = true): void
{
$parameters = $this->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
$parameters->set(Option::AUTO_IMPORT_NAMES, $importNames);
$parameters->set(Option::AUTO_IMPORT_DOC_BLOCK_NAMES, $importDocBlockNames);
}

public function importShortClasses(): void
public function importShortClasses(bool $importShortClasses = true): void
{
$parameters = $this->parameters();
$parameters->set(Option::IMPORT_SHORT_CLASSES, true);
$parameters->set(Option::IMPORT_SHORT_CLASSES, $importShortClasses);
}

/**
* @deprecated Use @see \Rector\Config\RectorConfig::importShortClasses(false) instead
*/
public function disableImportShortClasses(): void
{
$parameters = $this->parameters();
$parameters->set(Option::IMPORT_SHORT_CLASSES, false);
}

/**
* @deprecated Use @see \Rector\Config\RectorConfig::importNames(false) instead
*/
public function disableImportNames(): void
{
$parameters = $this->parameters();
Expand Down
6 changes: 4 additions & 2 deletions packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public function enterNode(Node $node): ?Node
return $this->processNodeName($node, $file);
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$this->docBlockNameImporter->importNames($phpDocInfo->getPhpDocNode(), $node);
if ($this->parameterProvider->provideBoolParameter(Option::AUTO_IMPORT_DOC_BLOCK_NAMES)) {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$this->docBlockNameImporter->importNames($phpDocInfo->getPhpDocNode(), $node);
}

return $node;
}
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<directory>tests</directory>
<directory>rules-tests</directory>
<directory>packages-tests</directory>
<directory>utils/*/tests</directory>
</testsuite>
</testsuites>

Expand Down
6 changes: 6 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ final class Option
*/
public const AUTO_IMPORT_NAMES = 'auto_import_names';

/**
* @deprecated Use @see \Rector\Config\RectorConfig::importNames() instead
* @var string
*/
public const AUTO_IMPORT_DOC_BLOCK_NAMES = 'auto_import_doc_block_names';

/**
* @deprecated Use @see \Rector\Config\RectorConfig::importShortClasses() instead
* @var string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\DisableAutoImportDocBlock;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class DisableAutoImportDocBlockTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configure_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
* @Route("/someUrl")
*/
class SomeController extends Controller
{
}

?>
-----
<?php

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
* @\Symfony\Component\Routing\Annotation\Route("/someUrl")
*/
class SomeController extends Controller
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Core\Tests\Issues\DisableAutoImportDocBlock\Fixture;

final class SomeClass
{
/**
* @throws \PHPUnit\Framework\InvalidArgumentException
* @throws \InvalidArgumentException
*/
public function run()
{
}
}

?>

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Core\Tests\Issues\DisableAutoImportDocBlock\Fixture;

use ScrumWorks\OpenApiSchema\Annotation as OA;

final class WalletStaticDataView
{
/**
* @OA\Property(description="Map transaction type to label")
*/
public array $transactionLabelMap;
}
11 changes: 11 additions & 0 deletions tests/Issues/DisableAutoImportDocBlock/config/configure_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Rector\ClassMethod\ReplaceSensioRouteAnnotationWithSymfonyRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->importNames(true, false);
$rectorConfig->rule(ReplaceSensioRouteAnnotationWithSymfonyRector::class);
};

0 comments on commit 3e6c9cf

Please sign in to comment.