rename( // paths [__DIR__ . '/../utils/node-documentation-generator/snippet'], '*.php.inc', '#(\.php\.inc)$#', '.php' ); // CODE ↓ final class FileRenamer { /** * @var SmartFinder */ private $smartFinder; /** * @var SmartFileSystem */ private $smartFileSystem; public function __construct() { $this->smartFinder = new SmartFinder(new FinderSanitizer(), new FileSystemFilter()); ; $this->smartFileSystem = new SmartFileSystem(); } /** * @param string[] $sources */ public function rename(array $sources, string $suffix, string $matchingRegex, string $replacement) { Assert::allString($sources); Assert::allFileExists($sources); $fileInfos = $this->smartFinder->find($sources, $suffix); $this->renameFileInfos($fileInfos, $matchingRegex, $replacement); } /** * @param SmartFileInfo[] $fileInfos */ private function renameFileInfos(array $fileInfos, string $matchingRegex, string $replacement): void { foreach ($fileInfos as $fileInfo) { // do the rename $oldRealPath = $fileInfo->getRealPath(); $newRealPath = Strings::replace($oldRealPath, $matchingRegex, $replacement); if ($oldRealPath === $newRealPath) { continue; } $this->smartFileSystem->rename($oldRealPath, $newRealPath); } } }