Skip to content

Commit

Permalink
move collectors to src/Collector namespace for easy discovery, add Mo…
Browse files Browse the repository at this point in the history
…ckedClassCollector (#5055)
  • Loading branch information
TomasVotruba committed Sep 30, 2023
1 parent 0c0e5ee commit 640a49b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 7 deletions.
6 changes: 5 additions & 1 deletion rector.php
Expand Up @@ -4,13 +4,16 @@

use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
use Rector\Config\RectorConfig;

use Rector\Core\Collector\MockedClassCollector;
use Rector\Core\Collector\ParentClassCollector;
use Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector;
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenCollectorRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Collector\ParentClassCollector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;
use Rector\Utils\Rector\MoveAbstractRectorToChildrenRector;
Expand All @@ -33,6 +36,7 @@

// testing collectors
$rectorConfig->collector(ParentClassCollector::class);
$rectorConfig->collector(MockedClassCollector::class);
$rectorConfig->rule(FinalizeClassesWithoutChildrenCollectorRector::class);

$rectorConfig->rules([DeclareStrictTypesRector::class, MoveAbstractRectorToChildrenRector::class]);
Expand Down
Expand Up @@ -3,8 +3,8 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Core\Collector\ParentClassCollector;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenCollectorRector;
use Rector\TypeDeclaration\Collector\ParentClassCollector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(FinalizeClassesWithoutChildrenCollectorRector::class);
Expand Down
Expand Up @@ -9,13 +9,13 @@
use PhpParser\Node\Stmt\Class_;
use PHPStan\Node\CollectedDataNode;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Collector\ParentClassCollector;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\NodeAnalyzer\DoctrineEntityAnalyzer;
use Rector\Core\Rector\AbstractCollectorRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\TypeDeclaration\Collector\ParentClassCollector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
Expand Down
Expand Up @@ -310,12 +310,12 @@ private function resolveDataProviderNodes(ClassMethod $classMethod): DataProvide
/**
* @return array<array-key, Attribute>
*/
private function getPhpDataProviderAttributes(ClassMethod $node): array
private function getPhpDataProviderAttributes(ClassMethod $classMethod): array
{
$attributeName = 'PHPUnit\Framework\Attributes\DataProvider';

/** @var AttributeGroup[] $attrGroups */
$attrGroups = $node->attrGroups;
$attrGroups = $classMethod->attrGroups;

$dataProviders = [];

Expand Down
53 changes: 53 additions & 0 deletions src/Collector/MockedClassCollector.php
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Collector;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Type\Constant\ConstantStringType;

/**
* @implements Collector<MethodCall, string[]|null>
*/
final class MockedClassCollector implements Collector
{
public function getNodeType(): string
{
return MethodCall::class;
}

/**
* @param MethodCall $node
* @return string[]|null
*/
public function processNode(Node $node, Scope $scope): ?array
{
if (! $node->name instanceof Identifier) {
return null;
}

$methodName = $node->name->toString();
if (! in_array($methodName, ['createMock', 'buildMock'], true)) {
return null;
}

$firstArg = $node->getArgs()[0] ?? null;
if (! $firstArg instanceof Arg) {
return null;
}

$mockedClassType = $scope->getType($firstArg->value);

if (! $mockedClassType instanceof ConstantStringType) {
return null;
}

return [$mockedClassType->getValue()];
}
}
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\TypeDeclaration\Collector;
namespace Rector\Core\Collector;

use PhpParser\Node;
use PhpParser\Node\Name;
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/LazyContainerFactory.php
Expand Up @@ -49,6 +49,7 @@
use Rector\Config\RectorConfig;
use Rector\Core\Application\ChangedNodeScopeRefresher;
use Rector\Core\Application\FileProcessor;
use Rector\Core\Collector\ParentClassCollector;
use Rector\Core\Configuration\ConfigInitializer;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Console\Command\ListRulesCommand;
Expand Down Expand Up @@ -179,7 +180,6 @@
use Rector\StaticTypeMapper\PhpParser\StringNodeMapper;
use Rector\StaticTypeMapper\PhpParser\UnionTypeNodeMapper;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\Collector\ParentClassCollector;
use Rector\Utils\Command\MissingInSetCommand;
use Rector\Utils\Command\OutsideAnySetCommand;
use Symfony\Component\Console\Application;
Expand Down

0 comments on commit 640a49b

Please sign in to comment.