<commit_before><?php

namespace OpenDominion\Services\Dominion\Actions;

use OpenDominion\Contracts\Services\Dominion\Actions\ReleaseActionService as ReleaseActionServiceContract;
use OpenDominion\Helpers\UnitHelper;
use OpenDominion\Models\Dominion;
use OpenDominion\Traits\DominionGuardsTrait;
use RuntimeException;

class ReleaseActionService implements ReleaseActionServiceContract
{
    use DominionGuardsTrait;

    /** @var UnitHelper */
    protected $unitHelper;

    /**
     * ReleaseActionService constructor.
     *
     * @param UnitHelper $unitHelper
     */
    public function __construct(UnitHelper $unitHelper)
    {
        $this->unitHelper = $unitHelper;
    }

    /**
     * {@inheritdoc}
     */
    public function release(Dominion $dominion, array $data)
    {
        $this->guardLockedDominion($dominion);

        $data = array_map('intval', $data);

        $troopsReleased = [];

        $totalTroopsToRelease = array_sum($data);

        if ($totalTroopsToRelease === 0) {
            throw new RuntimeException('Military release aborted due to bad input.');
        }

        foreach ($data as $unitType => $amount) {
            if ($amount === 0) { // todo: collect()->except(amount == 0)
                continue;
            }

            if ($amount > $dominion->{'military_' . $unitType}) {
                throw new RuntimeException('Military release was not completed due to bad input.');
            }
        }

        foreach ($data as $unitType => $amount) {
            if ($amount === 0) {
                continue;
            }

            $dominion->{'military_' . $unitType} -= $amount;

            if ($unitType === 'draftees') {
                $dominion->peasants += $amount;
            } else {
                $dominion->military_draftees += $amount;
            }

            $troopsReleased[$unitType] = $amount;
        }

        $dominion->save();

        // todo: refactor
        $troopsReleasedStringParts = ['You successfully released'];
        $troopsReleasedStringPartsParts = []; // todo: refactooooooor

        foreach ($troopsReleased as $unitType => $amount) {
            if ($amount === 0) {
                continue;
            }

            if ($unitType === 'draftees') {
                $troopsReleasedStringParts[] = sprintf('%s %s into the peasantry', number_format($amount), str_plural('draftee', $amount));
            } else {
                $troopsReleasedStringPartsParts[] = (number_format($amount) . ' ' . str_plural(str_singular(strtolower($this->unitHelper->getUnitName($unitType, $dominion->race))), $amount));
            }
        }


        if (!empty($troopsReleasedStringPartsParts)) {
            $tmp = implode(', ', $troopsReleasedStringPartsParts);
            $tmp = strrev(implode(strrev(' and '), explode(strrev(', '), strrev($tmp), 2)));

            if (isset($troopsReleased['draftees'])) {
                $troopsReleasedStringParts[] = 'and';
            }

            $troopsReleasedStringParts[] = $tmp;
            $troopsReleasedStringParts[] = 'into draftees';
        }

        $troopsReleasedString = (implode(' ', $troopsReleasedStringParts) . '.');
        // todo: /refactor

        return [
            'message' => $troopsReleasedString,
            'totalTroopsReleased' => $totalTroopsToRelease,
        ];
    }
}<commit_msg>StyleCI<commit_after><?php

namespace OpenDominion\Services\Dominion\Actions;

use OpenDominion\Contracts\Services\Dominion\Actions\ReleaseActionService as ReleaseActionServiceContract;
use OpenDominion\Helpers\UnitHelper;
use OpenDominion\Models\Dominion;
use OpenDominion\Traits\DominionGuardsTrait;
use RuntimeException;

class ReleaseActionService implements ReleaseActionServiceContract
{
    use DominionGuardsTrait;

    /** @var UnitHelper */
    protected $unitHelper;

    /**
     * ReleaseActionService constructor.
     *
     * @param UnitHelper $unitHelper
     */
    public function __construct(UnitHelper $unitHelper)
    {
        $this->unitHelper = $unitHelper;
    }

    /**
     * {@inheritdoc}
     */
    public function release(Dominion $dominion, array $data)
    {
        $this->guardLockedDominion($dominion);

        $data = array_map('intval', $data);

        $troopsReleased = [];

        $totalTroopsToRelease = array_sum($data);

        if ($totalTroopsToRelease === 0) {
            throw new RuntimeException('Military release aborted due to bad input.');
        }

        foreach ($data as $unitType => $amount) {
            if ($amount === 0) { // todo: collect()->except(amount == 0)
                continue;
            }

            if ($amount > $dominion->{'military_' . $unitType}) {
                throw new RuntimeException('Military release was not completed due to bad input.');
            }
        }

        foreach ($data as $unitType => $amount) {
            if ($amount === 0) {
                continue;
            }

            $dominion->{'military_' . $unitType} -= $amount;

            if ($unitType === 'draftees') {
                $dominion->peasants += $amount;
            } else {
                $dominion->military_draftees += $amount;
            }

            $troopsReleased[$unitType] = $amount;
        }

        $dominion->save();

        // todo: refactor
        $troopsReleasedStringParts = ['You successfully released'];
        $troopsReleasedStringPartsParts = []; // todo: refactooooooor

        foreach ($troopsReleased as $unitType => $amount) {
            if ($amount === 0) {
                continue;
            }

            if ($unitType === 'draftees') {
                $troopsReleasedStringParts[] = sprintf('%s %s into the peasantry', number_format($amount), str_plural('draftee', $amount));
            } else {
                $troopsReleasedStringPartsParts[] = (number_format($amount) . ' ' . str_plural(str_singular(strtolower($this->unitHelper->getUnitName($unitType, $dominion->race))), $amount));
            }
        }

        if (!empty($troopsReleasedStringPartsParts)) {
            $tmp = implode(', ', $troopsReleasedStringPartsParts);
            $tmp = strrev(implode(strrev(' and '), explode(strrev(', '), strrev($tmp), 2)));

            if (isset($troopsReleased['draftees'])) {
                $troopsReleasedStringParts[] = 'and';
            }

            $troopsReleasedStringParts[] = $tmp;
            $troopsReleasedStringParts[] = 'into draftees';
        }

        $troopsReleasedString = (implode(' ', $troopsReleasedStringParts) . '.');
        // todo: /refactor

        return [
            'message' => $troopsReleasedString,
            'totalTroopsReleased' => $totalTroopsToRelease,
        ];
    }
}
{'fraction_non_alphanumeric': 0.13271834548430292, 'fraction_numerical': 0.0017854485939592323, 'mean_word_length': 2.2009523809523808, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1688545', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1878, 'n_words': 356}