Skip to content

Instantly share code, notes, and snippets.

@divinity76
Last active January 16, 2025 12:23
Show Gist options
  • Save divinity76/29e055ded439ea414e0fb9523e966c3e to your computer and use it in GitHub Desktop.
Save divinity76/29e055ded439ea414e0fb9523e966c3e to your computer and use it in GitHub Desktop.
rector.php example
<?php
declare(strict_types=1);
return (static function (\Rector\Config\RectorConfig $rectorConfig): void {
$rectorConfig->phpVersion(\Rector\ValueObject\PhpVersion::PHP_84);
if (1) {
$timeout = 10 * 60;
$maxNumberOfProcess = (int)shell_exec('nproc');
$jobSize = 10;
$rectorConfig->parallel($timeout, $maxNumberOfProcess, $jobSize);
}
// $rectorConfig->configure()->withParallel($timeout, $maxNumberOfProcess, $jobSize);
$rectorConfig->paths([
'/home/hans/projects/easyad',
]);
$rectorConfig->skip([
'*/vendor/*',
'*/tests/*',
'*/external_libs/*',
]);
if (0) {
$ref = new \ReflectionObject($rectorConfig);
$methods = $ref->getMethods();
var_dump($rectorConfig, $methods);
die();
}
$rectorConfig->sets([
//\Rector\Set\ValueObject\LevelSetList::UP_TO_PHP_82,
//\Rector\Set\ValueObject\LevelSetList::UP_TO_PHP_84,
]);
if (1)
$rectorConfig->rules([
\Rector\Php84\Rector\FuncCall\RoundingModeEnumRector::class,
\Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector::class,
]);
$rectorConfig->skip([
\Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector::class,
\Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector::class,
\Rector\Php80\Rector\FunctionLike\MixedTypeRector::class,
\Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector::class,
\Rector\Php73\Rector\FuncCall\ArrayKeyFirstLastRector::class,
\Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector::class,
\Rector\Php80\Rector\Class_\StringableForToStringRector::class,
\Rector\Php70\Rector\FuncCall\RandomFunctionRector::class,
\Rector\Php83\Rector\ClassConst\AddTypeToConstRector::class,
\Rector\Php81\Rector\ClassMethod\NewInInitializerRector::class,
/* \Rector\Php70\Rector\FuncCall\RandomFunctionRector::class, // the code changes may incur a performance penalty (using random_int() over rand() for example)
\Rector\Php80\Rector\FuncCall\ClassOnObjectRector::class, // php<8.0 compatibility
\Rector\Php80\Rector\Class_\StringableForToStringRector::class, // php<8.0 compatibility
\Rector\Php73\Rector\FuncCall\ArrayKeyFirstLastRector::class, // php<7.3 compatibility ... do we really need PHP<7.3 compatibility?
\Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector::class, // php<8.0 compatibility
\Rector\Php80\Rector\FunctionLike\MixedTypeRector::class, // php<8.0 compatibility
\Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector::class, // personal preference.. and PHP<7.4 compatibility
\Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector::class, // probably had our reasons for naming it differently
\Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector::class, // probably had our reasons for naming it differently
\Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector::class, // seems to be a bug in Rector where cannot find parent methods that actually exists
*/
]);
});
<?php
declare(strict_types=1);
require_once('vendor/autoload.php');
$run_rector = 1;
if ($run_rector) {
echo "run rector\n";
$cmd = implode(" ", [
'vendor/bin/rector',
'process',
'--config',
'RectorConfig.php',
//'--dry-run',
//'--debug',
//'--verbose',
//'--ansi',
//'--no-progress-bar',
//'--no-interaction',
'--memory-limit=20G',
//'--autoload-file',
//'vendor/autoload.php',
//'--',
//'src',
//'tests',
//'testfile.php',
]);
echo $cmd . PHP_EOL;
$descriptor_spec = [
//0 => array("pipe", "r"), // stdin is a pipe that the child will read from
// 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
// 2 => array("pipe", "w") // stderr is a pipe that the child will write to
];
$process = proc_open($cmd, $descriptor_spec, $pipes);
$ret = proc_close($process);
var_dump($ret);
if ($ret !== 0) {
echo "Rector failed\n";
die();
}
}
$remove_whitespace_changes = 1;
if ($remove_whitespace_changes) {
$dir = '/home/hans/projects/easyad';
$ret = hhb_exec("git diff --ignore-all-space --patch", "", $stdout, $stderr, true, working_dir: $dir);
$diff = $stdout;
if ($ret !== 0) {
echo "git diff failed\n";
die();
}
$ret = hhb_exec("git checkout -- " . escapeshellarg($dir), "", $stdout, $stderr, true, working_dir: $dir);
if ($ret !== 0) {
echo "git checkout failed\n";
die();
}
$ret = hhb_exec("git apply --ignore-whitespace -", $diff, $stdout, $stderr, true, working_dir: $dir);
if ($ret !== 0) {
echo "git apply failed\n";
die();
}
}
/**
* better version of shell_exec() / exec() / system() / passthru()
* supporting stdin and stdout and stderr and os-level return code
*
* @param string $cmd
* command to execute
* @param string $stdin
* (optional) data to send to stdin, binary data is supported.
* @param string $stdout
* (optional) stdout data generated by cmd
* @param string $stderr
* (optional) stderr data generated by cmd
* @param bool $print_std
* (optional, default false) if you want stdout+stderr to be printed while it's running,
* set this to true. (useful for debugging long-running commands)
* @return int os-level return code
*/
function hhb_exec(string $cmd, string $stdin = "", string &$stdout = null, string &$stderr = null, bool $print_std = false, ?string $working_dir = null): int
{
echo "executing:\n$cmd\n";
$stdouth = tmpfile();
$stderrh = tmpfile();
$descriptorspec = array(
0 => array(
"pipe",
"rb"
), // stdin
1 => array(
"file",
stream_get_meta_data($stdouth)['uri'],
'ab'
),
2 => array(
"file",
stream_get_meta_data($stderrh)['uri'],
'ab'
)
);
$pipes = array();
$proc = proc_open($cmd, $descriptorspec, $pipes, $working_dir);
while (strlen($stdin) > 0) {
$written_now = fwrite($pipes[0], $stdin);
if ($written_now < 1 || $written_now === strlen($stdin)) {
// ... can add more error checking here
break;
}
$stdin = substr($stdin, $written_now);
}
fclose($pipes[0]);
unset($stdin, $pipes[0]);
if (! $print_std) {
$proc_ret = proc_close($proc); // this line will stall until the process has exited.
$stdout = stream_get_contents($stdouth);
$stderr = stream_get_contents($stderrh);
} else {
$stdout = "";
$stderr = "";
stream_set_blocking($stdouth, false);
stream_set_blocking($stderrh, false);
$fetchstd = function () use (&$stdout, &$stderr, &$stdouth, &$stderrh): bool {
$ret = false;
$tmp = stream_get_contents($stdouth); // fread($stdouth, 1); //
if (is_string($tmp) && strlen($tmp) > 0) {
$ret = true;
$stdout .= $tmp;
fwrite(STDOUT, $tmp);
}
$tmp = stream_get_contents($stderrh); // fread($stderrh, 1); //
// var_dump($tmp);
if (is_string($tmp) && strlen($tmp) > 0) {
$ret = true;
$stderr .= $tmp;
fwrite(STDERR, $tmp);
}
return $ret;
};
while (($status = proc_get_status($proc))["running"]) {
if (! $fetchstd()) {
// 100 ms
usleep(100 * 1000);
}
}
$proc_ret = $status["exitcode"];
proc_close($proc);
$fetchstd();
}
fclose($stdouth);
fclose($stderrh);
return $proc_ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment