Search code examples
phppermissionsrector

PHP Rector does not work at all because of "Cannot rename"


I use Rector to automatically refactor a codebase. I try to replace the outdated functions like utf8_decode() and utf8_encode() with custom user-defined functions. The rector.php looks like this:

<?php
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Utils\Rector\ReplaceUTF8Functions;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__ . \DIRECTORY_SEPARATOR . 'service.php'
    ]);
    $rectorConfig->rule(ReplaceUTF8Functions::class);;
};

And the ReplaceUTF8Functions looks like this:

<?php
namespace Utils\Rector;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Expr\FuncCall;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use function array_key_exists;

final class ReplaceUTF8Functions extends AbstractRector {

    private const REPLACEMENT = [
        'utf8_decode' => 'utf8ToLatin1',
        'utf8_encode' => 'latin1ToUtf8'
    ];

    public function getNodeTypes(): array {
        return [
            FuncCall::class
        ];
    }

    /**
     * @param FuncCall $node
     * @return null|Node
     */
    public function refactor(Node $node): ?Node {
        $name = $this->getName($node->name);
        if (!array_key_exists($name, self::REPLACEMENT))
            return null;
        $node->name = new Name(self::REPLACEMENT[$name]);
        return $node;
    }

    public function getRuleDefinition(): RuleDefinition {
        return new RuleDefinition('Replace the utf8_* functions with custom counterparts', [
                new CodeSample(
                    'utf8_decode(\'string\');',
                    'utf8ToLatin1(\'string\');'
                )
            ]
        );
    }
}

But once I try to run it:

vendor\bin\rector process

It gives an error:

                                    cts\trunk\service.php" file, due to:
         "System error: "Cannot rename "C:\Projects\trunk\ser8FA1.tmp" to
         "C:\Projects\trunk\service.php":
         rename(C:\Projects\trunk\ser8FA1.tmp,C:\Projects\trunk\service.php): Zugriff verweigert (code:

         Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new". On line: 286

Script rector handling the __exec_command event returned with error code 1

Firstly, the error message is not complete at all. It's trimmed. I ran it in the Windows Command Prompt, in the ConEmu - it does not get better. Moreover - I tried to give all the permissions to the current user on the file I want to process - nothing. I've tried to run the command as follows - vendor\bin\rector process. I tried to run it as administrator. Have you encountered the same error?


Solution

  • I've asked the same on the GitHub page and the developers fixed the issue