public static function writeFile(string $file, string $contents, int $permissions = 0777): string
    {
        self::createDirectory(\dirname($file));
        
        $tempFile = \tempnam(\sys_get_temp_dir(), self::TEMP_FILE_PREFIX);
        
        \file_put_contents($tempFile, $contents);
        
        if (\DIRECTORY_SEPARATOR == '\\') {
            if (\is_file($file)) {
                @\unlink($file);
            }
        }
        
        if (!@\rename($tempFile, $file)) {
            throw new \RuntimeException(\sprintf('Could not move temp file to "%s"%s', $file, self::getErrorInfo()));
        }
        
        if (\DIRECTORY_SEPARATOR != '\\') {
            self::changePermissions($file, $permissions);
        }
        
        \clearstatcache(true, $file);
        
        return $file;
    }