Last active
September 30, 2021 11:17
-
-
Save popovserhii/45cc8f2e70a2cc6aa6af9beb4bbdbda7 to your computer and use it in GitHub Desktop.
Convert PHP-CS-Fixer rules to ECS rules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'vendor/autoload.php'; | |
function varExport($expression, $return = false) | |
{ | |
$export = var_export($expression, true); | |
$export = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $export); | |
$array = preg_split("/\r\n|\n|\r/", $export); | |
$array = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [null, ']$1', ' => ['], $array); | |
$export = join(PHP_EOL, array_filter(["["] + $array)); | |
if ((bool) $return) { | |
return $export; | |
} else { | |
echo $export; | |
} | |
} | |
/** @var PhpCsFixer\Config $fixer */ | |
$fixer = require_once '.php_cs'; | |
$rules = $fixer->getRules(); | |
$newRules = []; | |
$skippedRules = []; | |
foreach ($rules as $rule => $option) { | |
$ucRule = str_replace('_', '', ucwords($rule, '_')) . 'Fixer::class'; | |
if (is_bool($option) && $option) { | |
// standard rule | |
$ucRule = '$services->set(' . $ucRule . ');'; | |
$newRules[] = $ucRule; | |
} elseif (is_bool($option) && !$option) { | |
// mark rule as skipped | |
$skippedRules[] = $ucRule . ' => null'; | |
continue; | |
} elseif (is_array($option)) { | |
// rule with options | |
$ucRule = '$services->set(' . $ucRule . ')'; | |
$varExport = varExport($option, true); | |
$ucRule = <<<HTML | |
$ucRule | |
->call('configure', [$varExport]); | |
HTML; | |
$newRules[] = $ucRule; | |
} | |
} | |
if ($skippedRules) { | |
$skipped = "\n" . implode($skippedRules, ",\n") . "\n"; | |
$newRules[] = '$parameters->set(Option::SKIP, [' . $skipped . '])'; | |
} | |
echo implode($newRules, "\n"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment