src/Viserio/Component/Filesystem/Traits/FilesystemExtensionTrait.php
public function changeExtension(string $path, string $extension): string
    {
        $path = $this->getTransformedPath($path);

        $explode    = \explode('.', $path);
        $substrPath = \mb_substr($path, -1);

        // No extension for paths
        if ($substrPath === '/' || \is_dir($path)) {
            return $path;
        }

        $actualExtension = null;
        $extension       = \ltrim($extension, '.');

        if (\count($explode) >= 2 && ! \is_dir($path)) {
            $actualExtension = \mb_strtolower($extension);
        }

        // No actual extension in path
        if ($actualExtension === null) {
            return $path . ($substrPath === '.' ? '' : '.') . $extension;
        }

        return \mb_substr($path, 0, -\mb_strlen($actualExtension)) . $extension;
    }
public function changeExtension(string $path, string $extension): string
    {
        $path = $this->getTransformedPath($path);

        $explode    = \explode('.', $path);
        $substrPath = \mb_substr($path, -1);

        // No extension for paths
        if ($substrPath === '/' || \is_dir($path)) {
            return $path;
        }

        $actualExtension = null;
        $extension       = \ltrim($extension, '.');

        if (\count($explode) >= 2 && ! \is_dir($path)) {
            $actualExtension = \mb_strtolower($extension);
        }

        // No actual extension in path
        if ($actualExtension === null) {
            return $path . ($substrPath === '.' ? '' : '.') . $extension;
        }

        return \mb_substr($path, 0, -\mb_strlen($actualExtension)) . $extension;
    }
Changes the extension of a path string.

@param string $path      The path string with filename.ext to change
@param string $extension New extension (with or without leading dot)

@return string The path string with new file extension