<commit_before><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace Database\Factories;

use App\Exceptions\ModelNotSavedException;
use Illuminate\Database\Eloquent\Factories\Factory as BaseFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

abstract class Factory extends BaseFactory
{
    private bool $requireSaved = true;

    /**
     * Return the created model even if it fails to save.
     *
     * @return $this
     */
    public function allowUnsaved(): static
    {
        $this->requireSaved = false;

        return $this;
    }

    protected function callAfterCreating(Collection $instances, ?Model $parent = null): void
    {
        $instances->each(function ($model) {
            if (!$model->exists && $this->requireSaved) {
                throw new ModelNotSavedException(
                    method_exists($model, 'validationErrors')
                        ? $model->validationErrors()->toSentence()
                        : 'Failed to save model',
                );
            }
        });

        parent::callAfterCreating($instances, $parent);
    }

    protected function newInstance(array $arguments = []): static
    {
        $factory = parent::newInstance($arguments);
        $factory->requireSaved = $this->requireSaved;

        return $factory;
    }

    // TODO: remove following line after removing legacy-factories
    // fooling legacy-factories' "isLegacyFactory" check: class Hello extends Factory
}<commit_msg>Move requireSaved check outside of loop<commit_after><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace Database\Factories;

use App\Exceptions\ModelNotSavedException;
use Illuminate\Database\Eloquent\Factories\Factory as BaseFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

abstract class Factory extends BaseFactory
{
    private bool $requireSaved = true;

    /**
     * Return the created model even if it fails to save.
     *
     * @return $this
     */
    public function allowUnsaved(): static
    {
        $this->requireSaved = false;

        return $this;
    }

    protected function callAfterCreating(Collection $instances, ?Model $parent = null): void
    {
        if ($this->requireSaved) {
            $instances->each(function ($model) {
                if (!$model->exists) {
                    throw new ModelNotSavedException(
                        method_exists($model, 'validationErrors')
                            ? $model->validationErrors()->toSentence()
                            : 'Failed to save model',
                    );
                }
            });
        }

        parent::callAfterCreating($instances, $parent);
    }

    protected function newInstance(array $arguments = []): static
    {
        $factory = parent::newInstance($arguments);
        $factory->requireSaved = $this->requireSaved;

        return $factory;
    }

    // TODO: remove following line after removing legacy-factories
    // fooling legacy-factories' "isLegacyFactory" check: class Hello extends Factory
}
{'fraction_non_alphanumeric': 0.10135533294048321, 'fraction_numerical': 0.0017678255745433118, 'mean_word_length': 2.4572301425661913, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '3602943', 'n_tokens_mistral': 903, 'n_tokens_neox': 885, 'n_words': 248}