<commit_before><?php declare(strict_types=1);

namespace Lmc\Steward\Selenium;

use Assert\Assert;

/**
 * Download Selenium standalone server
 */
class Downloader
{
    /** @var string Selenium storage URL */
    public static $storageUrl = 'https://selenium-release.storage.googleapis.com';
    /** @var string Target directory where should be the file saved */
    private $targetDir;
    /** @var string Version to download */
    private $version;

    /**
     * @param string $targetDir Target directory where should be the file saved
     */
    public function __construct(string $targetDir)
    {
        $this->targetDir = rtrim($targetDir, '/');
    }

    /**
     * Get latest released version of Selenium server. If not found, null is returned.
     */
    public static function getLatestVersion(): ?string
    {
        $availableVersions = self::getAvailableVersions();

        if (!$availableVersions) {
            return null;
        }

        return end($availableVersions);
    }

    public static function getAvailableVersions(): array
    {
        $data = @file_get_contents(self::$storageUrl);
        if (!$data) {
            return [];
        }

        libxml_use_internal_errors(true); // disable errors from being thrown
        $xml = simplexml_load_string($data);
        if (!$xml) {
            return [];
        }

        $releases = $xml->xpath('//*[text()[contains(.,"selenium-server-standalone")]]');
        $availableVersions = [];
        foreach ($releases as $release) {
            $parsedVersion = preg_replace('/.*standalone-(.+)\.jar/', '$1', $release);
            if ((string) $release === $parsedVersion) { // regexp did not match
                continue;
            }

            $availableVersions[] = $parsedVersion;
        }

        self::sortVersions($availableVersions);

        return $availableVersions;
    }

    private static function sortVersions(array &$versions): void
    {
        // Sort naturally, but versions like 3.0.0 must be after 3.0.0-beta, so we must take care of them explicitly
        usort($versions, function (string $a, string $b): int {
            $aParts = explode('-', $a);
            $bParts = explode('-', $b);

            // First part is the same (3.0.0), but one string does have second part (-beta) while the other one does not
            if ($aParts[0] === $bParts[0] && (count($aParts) !== count($bParts))) {
                // The one with less parts should be ordered after the longer one
                return count($bParts) <=> count($aParts);
            }

            return strnatcmp(mb_strtolower($a), mb_strtolower($b));
        });
    }<commit_msg>Fix incompatible argument type<commit_after><?php declare(strict_types=1);

namespace Lmc\Steward\Selenium;

use Assert\Assert;

/**
 * Download Selenium standalone server
 */
class Downloader
{
    /** @var string Selenium storage URL */
    public static $storageUrl = 'https://selenium-release.storage.googleapis.com';
    /** @var string Target directory where should be the file saved */
    private $targetDir;
    /** @var string Version to download */
    private $version;

    /**
     * @param string $targetDir Target directory where should be the file saved
     */
    public function __construct(string $targetDir)
    {
        $this->targetDir = rtrim($targetDir, '/');
    }

    /**
     * Get latest released version of Selenium server. If not found, null is returned.
     */
    public static function getLatestVersion(): ?string
    {
        $availableVersions = self::getAvailableVersions();

        if (!$availableVersions) {
            return null;
        }

        return end($availableVersions);
    }

    public static function getAvailableVersions(): array
    {
        $data = @file_get_contents(self::$storageUrl);
        if (!$data) {
            return [];
        }

        libxml_use_internal_errors(true); // disable errors from being thrown
        $xml = simplexml_load_string($data);
        if (!$xml) {
            return [];
        }

        $releases = $xml->xpath('//*[text()[contains(.,"selenium-server-standalone")]]');
        $availableVersions = [];
        foreach ($releases as $release) {
            $release = (string) $release;
            $parsedVersion = preg_replace('/.*standalone-(.+)\.jar/', '$1', $release);
            if ($release === $parsedVersion) { // regexp did not match
                continue;
            }

            $availableVersions[] = $parsedVersion;
        }

        self::sortVersions($availableVersions);

        return $availableVersions;
    }

    private static function sortVersions(array &$versions): void
    {
        // Sort naturally, but versions like 3.0.0 must be after 3.0.0-beta, so we must take care of them explicitly
        usort($versions, function (string $a, string $b): int {
            $aParts = explode('-', $a);
            $bParts = explode('-', $b);

            // First part is the same (3.0.0), but one string does have second part (-beta) while the other one does not
            if ($aParts[0] === $bParts[0] && (count($aParts) !== count($bParts))) {
                // The one with less parts should be ordered after the longer one
                return count($bParts) <=> count($aParts);
            }

            return strnatcmp(mb_strtolower($a), mb_strtolower($b));
        });
    }
{'fraction_non_alphanumeric': 0.13148148148148148, 'fraction_numerical': 0.004814814814814815, 'mean_word_length': 2.259505129752565, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6409378', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1458, 'n_words': 423}