<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 App\Models;

use App\Libraries\MorphMap;
use App\Models\Score\Best as ScoreBest;
use Ds\Set;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
 * @property \Carbon\Carbon|null $created_at
 * @property \App\Models\Score\Best\Model $score
 * @property \Carbon\Carbon|null $updated_at
 */
class ScorePin extends Model
{
    // TODO: make it an interface and check it in isValidType
    const SCORES = [
        MorphMap::MAP[ScoreBest\Fruits::class],
        MorphMap::MAP[ScoreBest\Mania::class],
        MorphMap::MAP[ScoreBest\Osu::class],
        MorphMap::MAP[ScoreBest\Taiko::class],
    ];

    public static function isValidType(string|null $type): bool
    {
        static $lookup;

        $lookup ??= new Set(static::SCORES);

        return $lookup->contains($type);
    }

    public function scopeForMode($query, string|Score\Best\Model $modeOrScore): Builder
    {
        if (is_string($modeOrScore)) {
            $class = Score\Best\Model::getClassByString($modeOrScore);
            $instance = new $class();
        } else {
            $instance = $modeOrScore;
        }

        return $query->where('score_type', $instance->getMorphClass());
    }

    public function scopeWithVisibleScore($query): Builder
    {
        return $query->whereHas('score', fn ($q) => $q->visibleUsers());
    }

    public function score(): MorphTo
    {
        return $this->morphTo();
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}<commit_msg>Specify polymorphic target for relation query<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 App\Models;

use App\Libraries\MorphMap;
use App\Models\Score\Best as ScoreBest;
use Ds\Set;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
 * @property \Carbon\Carbon|null $created_at
 * @property \App\Models\Score\Best\Model $score
 * @property \Carbon\Carbon|null $updated_at
 */
class ScorePin extends Model
{
    const SCORES = [
        MorphMap::MAP[ScoreBest\Fruits::class],
        MorphMap::MAP[ScoreBest\Mania::class],
        MorphMap::MAP[ScoreBest\Osu::class],
        MorphMap::MAP[ScoreBest\Taiko::class],
    ];

    public static function isValidType(string|null $type): bool
    {
        static $lookup;

        $lookup ??= new Set(static::SCORES);

        return $lookup->contains($type);
    }

    public function scopeForMode($query, string|Score\Best\Model $modeOrScore): Builder
    {
        if (is_string($modeOrScore)) {
            $class = Score\Best\Model::getClassByString($modeOrScore);
            $instance = new $class();
        } else {
            $instance = $modeOrScore;
        }

        return $query->where('score_type', $instance->getMorphClass());
    }

    public function scopeWithVisibleScore($query): Builder
    {
        return $query->whereHasMorph('score', static::SCORES, fn ($q) => $q->visibleUsers());
    }

    public function score(): MorphTo
    {
        return $this->morphTo();
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}
{'fraction_non_alphanumeric': 0.13275535085342727, 'fraction_numerical': 0.001625575724735844, 'mean_word_length': 3.148314606741573, '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': '7184668', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1147, 'n_words': 252}