Skip to content

Commit

Permalink
apply constant decouple
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 6, 2020
1 parent b86f4af commit b0bd524
Show file tree
Hide file tree
Showing 29 changed files with 409 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function resolvePackage(Configuration $configuration): Package

private function isPackageAlreadyLoaded(array $composerJson, Package $package): bool
{
return isset($composerJson['autoload'][self::PSR - 4][$package->getSrcNamespace()]);
return isset($composerJson['autoload'][self::PSR_4][$package->getSrcNamespace()]);
}

private function rebuildAutoload(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
*/
final class ConstructorInjectionToActionInjectionRector extends AbstractRector
{
/**
* @var string
*/
private const __CONSTRUCT = '__construct';

/**
* @var Param[]
*/
Expand Down Expand Up @@ -118,12 +123,12 @@ public function refactor(Node $node): ?Node

// traverse constructor dependencies and names of their properties
/** @var ClassMethod $constructMethod */
$constructMethod = $node->getMethod('__construct');
$constructMethod = $node->getMethod(self::__CONSTRUCT);
$this->collectPropertyFetchToParams($constructMethod);

// replace them in property fetches with particular class methods and use variable instead
foreach ($node->getMethods() as $classMethod) {
if ($this->isName($classMethod, '__construct')) {
if ($this->isName($classMethod, self::__CONSTRUCT)) {
continue;
}

Expand Down Expand Up @@ -177,7 +182,7 @@ private function shouldSkip(Class_ $class): bool
return true;
}

$constructMethod = $class->getMethod('__construct');
$constructMethod = $class->getMethod(self::__CONSTRUCT);
// no constructor, nothing to do

return $constructMethod === null;
Expand Down
33 changes: 24 additions & 9 deletions rules/cakephp/src/Rector/MethodCall/ModalToGetSetRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@
*/
final class ModalToGetSetRector extends AbstractRector
{
/**
* @var string
*/
private const SET = 'set';

/**
* @var string
*/
private const GET = 'get';

/**
* @var string
*/
private const MINIMAL_ARGUMENT_COUNT = 'minimal_argument_count';

/**
* @var mixed[]
*/
Expand Down Expand Up @@ -110,11 +125,11 @@ private function matchTypeAndMethodName(MethodCall $methodCall): ?array
$config = $methodNamesToGetAndSetNames[$currentMethodName];

// default
$config['set'] = $config['set'] ?? 'set' . ucfirst($currentMethodName);
$config['get'] = $config['get'] ?? 'get' . ucfirst($currentMethodName);
$config[self::SET] = $config[self::SET] ?? self::SET . ucfirst($currentMethodName);
$config[self::GET] = $config[self::GET] ?? self::GET . ucfirst($currentMethodName);

// default minimal argument count for setter
$config['minimal_argument_count'] = $config['minimal_argument_count'] ?? 1;
$config[self::MINIMAL_ARGUMENT_COUNT] = $config[self::MINIMAL_ARGUMENT_COUNT] ?? 1;

return $config;
}
Expand All @@ -127,26 +142,26 @@ private function matchTypeAndMethodName(MethodCall $methodCall): ?array
*/
private function resolveNewMethodNameByCondition(MethodCall $methodCall, array $config): string
{
if (count($methodCall->args) >= $config['minimal_argument_count']) {
return $config['set'];
if (count($methodCall->args) >= $config[self::MINIMAL_ARGUMENT_COUNT]) {
return $config[self::SET];
}

if (! isset($methodCall->args[0])) {
return $config['get'];
return $config[self::GET];
}

// first argument type that is considered setter
if (! isset($config['first_argument_type_to_set'])) {
return $config['get'];
return $config[self::GET];
}

$argumentType = $config['first_argument_type_to_set'];
$argumentValue = $methodCall->args[0]->value;

if ($argumentType === 'array' && $argumentValue instanceof Array_) {
return $config['set'];
return $config[self::SET];
}

return $config['get'];
return $config[self::GET];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
*/
final class RenameMethodCallBasedOnParameterRector extends AbstractRector
{
/**
* @var string
*/
private const MATCH_PARAMETER = 'match_parameter';

/**
* @var string
*/
private const REPLACE_WITH = 'replace_with';

/**
* @var mixed[]
*/
Expand Down Expand Up @@ -55,12 +65,12 @@ public function getDefinition(): RectorDefinition
[
'$methodNamesByTypes' => [
'getParam' => [
'match_parameter' => 'paging',
'replace_with' => 'getAttribute',
self::MATCH_PARAMETER => 'paging',
self::REPLACE_WITH => 'getAttribute',
],
'withParam' => [
'match_parameter' => 'paging',
'replace_with' => 'withAttribute',
self::MATCH_PARAMETER => 'paging',
self::REPLACE_WITH => 'withAttribute',
],
],
]
Expand All @@ -87,7 +97,7 @@ public function refactor(Node $node): ?Node
return null;
}

$node->name = new Identifier($config['replace_with']);
$node->name = new Identifier($config[self::REPLACE_WITH]);

return $node;
}
Expand All @@ -113,7 +123,7 @@ private function matchTypeAndMethodName(MethodCall $methodCall): ?array
continue;
}
$config = $methodMapping[$currentMethodName];
if (empty($config['match_parameter'])) {
if (empty($config[self::MATCH_PARAMETER])) {
continue;
}
if (count($methodCall->args) < 1) {
Expand All @@ -123,7 +133,7 @@ private function matchTypeAndMethodName(MethodCall $methodCall): ?array
if (! ($arg->value instanceof String_)) {
continue;
}
if ($arg->value->value !== $config['match_parameter']) {
if ($arg->value->value !== $config[self::MATCH_PARAMETER]) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@

final class DefaultDoctrineEntityAndRepositoryMapper implements DoctrineEntityAndRepositoryMapperInterface
{
/**
* @var string
*/
private const REPOSITORY = 'Repository';

public function mapRepositoryToEntity(string $repository): ?string
{
// "SomeRepository" => "Some"
$withoutSuffix = Strings::substring($repository, 0, - strlen('Repository'));
$withoutSuffix = Strings::substring($repository, 0, - strlen(self::REPOSITORY));

// "App\Repository\Some" => "App\Entity\Some"
return Strings::replace($withoutSuffix, '#Repository#', 'Entity');
Expand All @@ -21,9 +26,9 @@ public function mapRepositoryToEntity(string $repository): ?string
public function mapEntityToRepository(string $entity): ?string
{
// "Some" => "SomeRepository"
$withSuffix = $entity . 'Repository';
$withSuffix = $entity . self::REPOSITORY;

// "App\Entity\SomeRepository" => "App\Repository\SomeRepository"
return Strings::replace($withSuffix, '#Entity#', 'Repository');
return Strings::replace($withSuffix, '#Entity#', self::REPOSITORY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

final class PhpDocTagNodeFactory
{
/**
* @var string
*/
private const UUID = 'uuid';

/**
* @var JoinTableNameResolver
*/
Expand Down Expand Up @@ -63,8 +68,8 @@ public function createJoinTableTagNode(Property $property): PhpDocTagNode
$joinTableTagValueNode = new JoinTableTagValueNode(
$uuidJoinTable,
null,
[new JoinColumnTagValueNode(['referencedColumnName' => 'uuid'])],
[new JoinColumnTagValueNode(['referencedColumnName' => 'uuid'])]
[new JoinColumnTagValueNode(['referencedColumnName' => self::UUID])],
[new JoinColumnTagValueNode(['referencedColumnName' => self::UUID])]
);

return new SpacelessPhpDocTagNode($joinTableTagValueNode->getShortName(), $joinTableTagValueNode);
Expand All @@ -73,7 +78,7 @@ public function createJoinTableTagNode(Property $property): PhpDocTagNode
public function createJoinColumnTagNode(bool $isNullable): JoinColumnTagValueNode
{
return new JoinColumnTagValueNode([
'referencedColumn' => 'uuid',
'referencedColumn' => self::UUID,
'nullable' => $isNullable,
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
*/
final class ManagerRegistryGetManagerToEntityManagerRector extends AbstractRector
{
/**
* @var string
*/
private const GET_MANAGER = 'getManager';

/**
* @var string
*/
private const ENTITY_MANAGER = 'entityManager';

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('', [
Expand Down Expand Up @@ -102,7 +112,7 @@ public function refactor(Node $node): ?Node

// collect on registry method calls, so we know if the manager registry is needed
$registryCalledMethods = $this->resolveManagerRegistryCalledMethodNames($node);
if (! in_array('getManager', $registryCalledMethods, true)) {
if (! in_array(self::GET_MANAGER, $registryCalledMethods, true)) {
return null;
}

Expand All @@ -113,7 +123,7 @@ public function refactor(Node $node): ?Node
return null;
}

if ($registryCalledMethods === ['getManager']) {
if ($registryCalledMethods === [self::GET_MANAGER]) {
// the manager registry is needed only get entity manager → we don't need it now
$this->removeManagerRegistryDependency($node, $constructMethodNode, $managerRegistryParam);
}
Expand All @@ -125,7 +135,7 @@ public function refactor(Node $node): ?Node
$this->addConstructorDependencyWithProperty(
$node,
$constructMethodNode,
'entityManager',
self::ENTITY_MANAGER,
new FullyQualifiedObjectType(EntityManagerInterface::class)
);

Expand Down Expand Up @@ -216,7 +226,7 @@ private function replaceEntityRegistryVariableWithEntityManagerProperty(Class_ $
return null;
}

return new PropertyFetch(new Variable('this'), 'entityManager');
return new PropertyFetch(new Variable('this'), self::ENTITY_MANAGER);
});
}

Expand Down Expand Up @@ -249,7 +259,7 @@ private function addConstructorDependencyWithProperty(

private function createEntityManagerParam(): Param
{
return new Param(new Variable('entityManager'), null, new FullyQualified(EntityManagerInterface::class));
return new Param(new Variable(self::ENTITY_MANAGER), null, new FullyQualified(EntityManagerInterface::class));
}

private function removeRegistryDependencyAssign(Class_ $class, ClassMethod $classMethod, Param $registryParam): void
Expand Down Expand Up @@ -283,7 +293,7 @@ private function isRegistryGetManagerMethodCall(Assign $assign): bool
if (! $this->isObjectType($assign->expr->var, ManagerRegistry::class)) {
return false;
}
return $this->isName($assign->expr->name, 'getManager');
return $this->isName($assign->expr->name, self::GET_MANAGER);
}

/**
Expand Down
Loading

0 comments on commit b0bd524

Please sign in to comment.