<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\Forum;

use App\Models\Forum\Forum;
use App\Models\Forum\PollOption;
use App\Models\Forum\Post;
use App\Models\Forum\Topic;
use Database\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\Sequence;

class TopicFactory extends Factory
{
    protected $model = Topic::class;

    public function definition(): array
    {
        return [
            'forum_id' => Forum::factory(),
            'topic_time' => fn () => $this->faker->dateTimeBetween('-5 years'),
            'topic_title' => fn () => $this->faker->catchPhrase(),
        ];
    }

    public function poll(bool $createPollOptions = true): static
    {
        $factory = $this->state([
            'poll_hide_results' => fn () => $this->faker->boolean(),
            'poll_length' => fn () => $this->faker->boolean()
                ? $this->faker->numberBetween(86400, 604800)
                : 0, // between 1 and 7 days, or infinite length
            'poll_max_options' => 1,
            'poll_start' => fn (array $attr) => $attr['topic_time'],
            'poll_title' => fn () => $this->faker->sentence(),
            'poll_vote_change' => fn () => $this->faker->boolean(),
        ]);

        if (!$createPollOptions) {
            return $factory;
        }

        $optionCount = $this->faker->numberBetween(2, 10);

        return $factory
            ->state(['poll_max_options' => fn () => $this->faker->numberBetween(1, $optionCount)])
            ->has(
                PollOption::factory()
                    ->count($optionCount)
                    ->sequence(fn (Sequence $sequence) => [
                        'poll_option_id' => $sequence->index,
                    ]),
            );
    }

    public function withPost(): static
    {
        return $this
            ->has(
                Post::factory()
                    ->state(function (array $_attr, Topic $topic) {
                        $attributes = ['post_time' => $topic->topic_time];

                        if ($topic->topic_poster !== null) {
                            $attributes['poster_id'] = $topic->topic_poster;
                        }

                        return $attributes;
                    }),
            )
            ->afterCreating(fn (Topic $topic) => $topic->refreshCache());
    }
}
<commit_msg>randomElement instead of ternary<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\Forum;

use App\Models\Forum\Forum;
use App\Models\Forum\PollOption;
use App\Models\Forum\Post;
use App\Models\Forum\Topic;
use Database\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\Sequence;

class TopicFactory extends Factory
{
    protected $model = Topic::class;

    public function definition(): array
    {
        return [
            'forum_id' => Forum::factory(),
            'topic_time' => fn () => $this->faker->dateTimeBetween('-5 years'),
            'topic_title' => fn () => $this->faker->catchPhrase(),
        ];
    }

    public function poll(bool $createPollOptions = true): static
    {
        $factory = $this->state([
            'poll_hide_results' => fn () => $this->faker->boolean(),
            'poll_length' => fn () => $this->faker->randomElement([
                // between 1 and 7 days, or infinite length
                $this->faker->numberBetween(86400, 604800),
                0,
            ]),
            'poll_max_options' => 1,
            'poll_start' => fn (array $attr) => $attr['topic_time'],
            'poll_title' => fn () => $this->faker->sentence(),
            'poll_vote_change' => fn () => $this->faker->boolean(),
        ]);

        if (!$createPollOptions) {
            return $factory;
        }

        $optionCount = $this->faker->numberBetween(2, 10);

        return $factory
            ->state(['poll_max_options' => fn () => $this->faker->numberBetween(1, $optionCount)])
            ->has(
                PollOption::factory()
                    ->count($optionCount)
                    ->sequence(fn (Sequence $sequence) => [
                        'poll_option_id' => $sequence->index,
                    ]),
            );
    }

    public function withPost(): static
    {
        return $this
            ->has(
                Post::factory()
                    ->state(function (array $_attr, Topic $topic) {
                        $attributes = ['post_time' => $topic->topic_time];

                        if ($topic->topic_poster !== null) {
                            $attributes['poster_id'] = $topic->topic_poster;
                        }

                        return $attributes;
                    }),
            )
            ->afterCreating(fn (Topic $topic) => $topic->refreshCache());
    }
}

{'fraction_non_alphanumeric': 0.1463035019455253, 'fraction_numerical': 0.00894941634241245, 'mean_word_length': 1.79858464888405, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 113, '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': '2877004', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1411, 'n_words': 303}