mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 16:30:51 +00:00
[DX] Remove MoveInterfacesToContractNamespaceDirectoryRector as breaky, use PHPStan rule instead (#1835)
This commit is contained in:
parent
a4eff1e0ad
commit
8027c7fd8c
2
.github/workflows/php_linter.yaml
vendored
2
.github/workflows/php_linter.yaml
vendored
|
@ -23,4 +23,4 @@ jobs:
|
|||
|
||||
- run: composer create-project php-parallel-lint/php-parallel-lint php-parallel-lint
|
||||
|
||||
- run: php-parallel-lint/parallel-lint src bin/rector config tests packages rules --colors --exclude rules/psr4/tests/Rector/Namespace_/MultipleClassFileToPsr4ClassesRector/Source --exclude rules/autodiscovery/tests/Rector/FileNode/MoveInterfacesToContractNamespaceDirectoryRector/Expected --exclude packages/node-type-resolver/tests/PerNodeTypeResolver/PropertyFetchTypeResolver/Source --exclude rules/nette-kdyby/tests/Rector/MethodCall/ReplaceEventManagerWithEventSubscriberRector/Source/ExpectedSomeClassCopyEvent.php --exclude rules/nette-kdyby/tests/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector/Source --exclude rules/type-declaration/tests/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector/Source
|
||||
- run: php-parallel-lint/parallel-lint src bin/rector config tests packages rules --colors --exclude rules/psr4/tests/Rector/Namespace_/MultipleClassFileToPsr4ClassesRector/Source --exclude packages/node-type-resolver/tests/PerNodeTypeResolver/PropertyFetchTypeResolver/Source --exclude rules/nette-kdyby/tests/Rector/MethodCall/ReplaceEventManagerWithEventSubscriberRector/Source/ExpectedSomeClassCopyEvent.php --exclude rules/nette-kdyby/tests/Rector/MethodCall/ReplaceMagicPropertyEventWithEventClassRector/Source --exclude rules/type-declaration/tests/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector/Source
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -114,7 +114,6 @@
|
|||
},
|
||||
"classmap": [
|
||||
"stubs",
|
||||
"rules-tests/Autodiscovery/Rector/Interface_/MoveInterfacesToContractNamespaceDirectoryRector/Expected",
|
||||
"rules-tests/CodingStyle/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source",
|
||||
"rules-tests/Renaming/Rector/Name/RenameClassRector/Source",
|
||||
"rules-tests/Renaming/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector/Source"
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity;
|
||||
|
||||
final class SameClassImplementEntity implements \Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
{
|
||||
public function __construct(\Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface $random)
|
||||
{
|
||||
}
|
||||
|
||||
public function returnAnother(): \Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity;
|
||||
|
||||
class RandomInterfaceUseCaseInTheSameNamespace
|
||||
{
|
||||
/**
|
||||
* @var \Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
*/
|
||||
public $random;
|
||||
|
||||
public function create(): \Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract;
|
||||
|
||||
interface RandomInterface
|
||||
{
|
||||
public function returnAnother(): \Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface;
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source;
|
||||
|
||||
use Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface;
|
||||
|
||||
class RandomInterfaceUseCase
|
||||
{
|
||||
public \Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface $random;
|
||||
|
||||
public function create(): \Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,74 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
use Symplify\SmartFileSystem\SmartFileSystem;
|
||||
|
||||
/**
|
||||
* @requires PHP 7.4
|
||||
*/
|
||||
final class MoveInterfacesToContractNamespaceDirectoryRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $originalFileInfo, AddedFileWithContent $expectedAddedFileWithContent): void
|
||||
{
|
||||
$this->doTestFileInfo($originalFileInfo);
|
||||
$this->assertFileWasAdded($expectedAddedFileWithContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo|AddedFileWithContent[]>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
$smartFileSystem = new SmartFileSystem();
|
||||
|
||||
yield [
|
||||
new SmartFileInfo(__DIR__ . '/Source/Entity/RandomInterface.php'),
|
||||
new AddedFileWithContent(
|
||||
$this->getFixtureTempDirectory() . '/Contract/RandomInterface.php',
|
||||
$smartFileSystem->readFile(__DIR__ . '/Expected/ExpectedRandomInterface.php')
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideDataSkip()
|
||||
*/
|
||||
public function testSkip(SmartFileInfo $originalFileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($originalFileInfo);
|
||||
$this->assertFileWasNotChanged($this->originalTempFileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideDataSkip(): Iterator
|
||||
{
|
||||
// skip already in correct location
|
||||
yield [new SmartFileInfo(__DIR__ . '/Source/Contract/KeepThisSomeInterface.php')];
|
||||
|
||||
// skip already in correct location
|
||||
yield [new SmartFileInfo(__DIR__ . '/Source/Contract/Foo/KeepThisSomeInterface.php')];
|
||||
|
||||
// skip nette control factory
|
||||
yield [new SmartFileInfo(__DIR__ . '/Source/Control/ControlFactory.php')];
|
||||
|
||||
// skip form control factory, even in docblock
|
||||
yield [new SmartFileInfo(__DIR__ . '/Source/Control/FormFactory.php')];
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/some_config.php';
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract\Foo;
|
||||
|
||||
interface KeepThisSomeInterface
|
||||
{
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Contract;
|
||||
|
||||
interface KeepThisSomeInterface
|
||||
{
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Control;
|
||||
|
||||
interface ControlFactory
|
||||
{
|
||||
public function create(): SomeControl;
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Control;
|
||||
|
||||
interface FormFactory
|
||||
{
|
||||
/**
|
||||
* @return SomeForm
|
||||
*/
|
||||
public function create();
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Control;
|
||||
|
||||
use Nette\Application\UI\Control;
|
||||
|
||||
final class SomeControl extends Control
|
||||
{
|
||||
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Control;
|
||||
|
||||
use Nette\Application\UI\Form;
|
||||
|
||||
final class SomeForm extends Form
|
||||
{
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity;
|
||||
|
||||
interface RandomInterface
|
||||
{
|
||||
public function returnAnother(): RandomInterface;
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity;
|
||||
|
||||
class RandomInterfaceUseCaseInTheSameNamespace
|
||||
{
|
||||
/**
|
||||
* @var RandomInterface
|
||||
*/
|
||||
public $random;
|
||||
|
||||
public function create(): RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source;
|
||||
|
||||
use Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity\RandomInterface;
|
||||
|
||||
class RandomInterfaceUseCase
|
||||
{
|
||||
/**
|
||||
* @var RandomInterface
|
||||
*/
|
||||
public $random;
|
||||
|
||||
public function create(): RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\ValueObject;
|
||||
|
||||
final class SameClassImplementEntity implements \Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity\RandomInterface
|
||||
{
|
||||
public function __construct(\Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity\RandomInterface $random)
|
||||
{
|
||||
}
|
||||
public function returnAnother(): \Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\Source\Entity\RandomInterface
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector;
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersionFeature::TYPED_PROPERTIES - 1);
|
||||
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(MoveInterfacesToContractNamespaceDirectoryRector::class);
|
||||
};
|
|
@ -1,48 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\TypeWithClassName;
|
||||
use PHPStan\Type\UnionType;
|
||||
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;
|
||||
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
|
||||
|
||||
final class NetteComponentFactoryInterfaceAnalyzer
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ReturnTypeInferer $returnTypeInferer
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://doc.nette.org/en/3.0/components#toc-components-with-dependencies
|
||||
*/
|
||||
public function isComponentFactoryInterface(Interface_ $interface): bool
|
||||
{
|
||||
foreach ($interface->getMethods() as $classMethod) {
|
||||
$returnType = $this->returnTypeInferer->inferFunctionLike($classMethod);
|
||||
if (! $returnType instanceof TypeWithClassName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$controlOrForm = new UnionType([
|
||||
new ObjectType('Nette\Application\UI\Control'),
|
||||
new ObjectType('Nette\Application\UI\Form'),
|
||||
]);
|
||||
|
||||
if ($returnType instanceof ShortenedObjectType) {
|
||||
$returnType = new ObjectType($returnType->getFullyQualifiedName());
|
||||
}
|
||||
|
||||
if ($controlOrForm->isSuperTypeOf($returnType)->yes()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,88 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Autodiscovery\Rector\Interface_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
use Rector\Autodiscovery\NodeAnalyzer\NetteComponentFactoryInterfaceAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\FileSystemRector\ValueObject\AddedFileWithNodes;
|
||||
use Rector\FileSystemRector\ValueObjectFactory\AddedFileWithNodesFactory;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* Inspiration @see https://github.com/rectorphp/rector/pull/1865/files#diff-0d18e660cdb626958662641b491623f8
|
||||
*
|
||||
* @see \Rector\Tests\Autodiscovery\Rector\Interface_\MoveInterfacesToContractNamespaceDirectoryRector\MoveInterfacesToContractNamespaceDirectoryRectorTest
|
||||
*/
|
||||
final class MoveInterfacesToContractNamespaceDirectoryRector extends AbstractRector
|
||||
{
|
||||
public function __construct(
|
||||
private readonly NetteComponentFactoryInterfaceAnalyzer $netteComponentFactoryInterfaceAnalyzer,
|
||||
private readonly AddedFileWithNodesFactory $addedFileWithNodesFactory
|
||||
) {
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Move interface to "Contract" namespace', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
// file: app/Exception/Rule.php
|
||||
|
||||
namespace App\Exception;
|
||||
|
||||
interface Rule
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
// file: app/Contract/Rule.php
|
||||
|
||||
namespace App\Contract;
|
||||
|
||||
interface Rule
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Interface_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Interface_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($this->netteComponentFactoryInterfaceAnalyzer->isComponentFactoryInterface($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$addedFileWithNodes = $this->addedFileWithNodesFactory->createWithDesiredGroup(
|
||||
$this->file->getSmartFileInfo(),
|
||||
$this->file,
|
||||
'Contract'
|
||||
);
|
||||
|
||||
if (! $addedFileWithNodes instanceof AddedFileWithNodes) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->removedAndAddedFilesCollector->removeFile($this->file->getSmartFileInfo());
|
||||
$this->removedAndAddedFilesCollector->addAddedFile($addedFileWithNodes);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user