Skip to content

Commit

Permalink
[TASK] Add ExtensionConfigResolver for rector/rector-installer (#6150)
Browse files Browse the repository at this point in the history
Relates: 6129
  • Loading branch information
sabbelasichon authored Apr 16, 2021
1 parent 2b22e73 commit 95b31d0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
11 changes: 11 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ parameters:
message: '#Function "class_exists\(\)" cannot be used/left in the code#'
paths:
- bin/rector.php
- src/Bootstrap/ExtensionConfigResolver.php

-
message: '#Do not use static property#'
Expand Down Expand Up @@ -528,3 +529,13 @@ parameters:
path: rules/TypeDeclaration/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector.php

- '#Method Rector\\Core\\PhpParser\\Node\\BetterNodeFinder\:\:findParentTypes\(\) should return T of PhpParser\\Node\|null but returns class\-string<T of PhpParser\\Node\>\|T of PhpParser\\Node#'

-
message: '#Parameter \#1\ $argument of class ReflectionClass constructor expects class-string<Rector\\RectorInstaller\\GeneratedConfig>|Rector\\RectorInstaller\\GeneratedConfig, string given.#'
paths:
- src/Bootstrap/ExtensionConfigResolver.php

-
message: '#Cognitive complexity for "Rector\\Core\\Bootstrap\\ExtensionConfigResolver\:\:appendExtensionsConfig\(\)" is 13, keep it under 9#'
paths:
- src/Bootstrap/ExtensionConfigResolver.php
51 changes: 51 additions & 0 deletions src/Bootstrap/ExtensionConfigResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);

namespace Rector\Core\Bootstrap;

use Symplify\SmartFileSystem\SmartFileInfo;

final class ExtensionConfigResolver
{
/**
* @param SmartFileInfo[] $configFileInfos
* @return SmartFileInfo[]
*/
public function appendExtensionsConfig(array $configFileInfos): array
{
if (! class_exists('Rector\RectorInstaller\GeneratedConfig')) {
return $configFileInfos;
}

$generatedConfigReflection = new \ReflectionClass('Rector\RectorInstaller\GeneratedConfig');

if ($generatedConfigReflection->getFileName() === false) {
return $configFileInfos;
}

$generatedConfigDirectory = dirname($generatedConfigReflection->getFileName());
foreach (\Rector\RectorInstaller\GeneratedConfig::EXTENSIONS as $name => $extensionConfig) {
foreach ($extensionConfig['extra']['includes'] ?? [] as $includedFile) {
$includedFilePath = null;
if (isset($extensionConfig['relative_install_path'])) {
$includedFilePath = sprintf(
'%s/%s/%s',
$generatedConfigDirectory,
$extensionConfig['relative_install_path'],
$includedFile
);
if (! file_exists($includedFilePath) || ! is_readable($includedFilePath)) {
$includedFilePath = null;
}
}

if ($includedFilePath === null) {
$includedFilePath = sprintf('%s/%s', $extensionConfig['install_path'], $includedFile);
}
$configFileInfos[] = new SmartFileInfo($includedFilePath);
}
}

return $configFileInfos;
}
}
7 changes: 7 additions & 0 deletions src/Bootstrap/RectorConfigsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ final class RectorConfigsResolver
*/
private $setConfigResolver;

/**
* @var ExtensionConfigResolver
*/
private $extensionConfigResolver;

public function __construct()
{
$this->setConfigResolver = new SetConfigResolver();
$this->configResolver = new ConfigResolver();
$this->extensionConfigResolver = new ExtensionConfigResolver();
}

/**
Expand Down Expand Up @@ -64,6 +70,7 @@ public function provide(): BootstrapConfigs
) : [];

$configFileInfos = $this->appendRectorRecipeConfig($argvInput, $configFileInfos);
$configFileInfos = $this->extensionConfigResolver->appendExtensionsConfig($configFileInfos);

return new BootstrapConfigs($mainConfigFileInfo, $configFileInfos);
}
Expand Down

0 comments on commit 95b31d0

Please sign in to comment.