• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

conedevelopment / root / 18281319793

06 Oct 2025 12:46PM UTC coverage: 76.331% (-0.02%) from 76.348%
18281319793

push

github

iamgergo
fix n+1

4 of 6 new or added lines in 2 files covered. (66.67%)

3354 of 4394 relevant lines covered (76.33%)

34.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.73
/src/Fields/Meta.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Cone\Root\Fields;
6

7
use Closure;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Eloquent\Relations\MorphOne as EloquentRelation;
11
use Illuminate\Http\Request;
12

13
/**
14
 * @extends \Cone\Root\Fields\MorphOne<\Illuminate\Database\Eloquent\Relations\MorphOne>
15
 */
16
class Meta extends MorphOne
17
{
18
    /**
19
     * The field instance.
20
     */
21
    protected Field $field;
22

23
    /**
24
     * Create a new relation field instance.
25
     */
26
    public function __construct(string $label, Closure|string|null $modelAttribute = null, Closure|string|null $relation = null)
6✔
27
    {
28
        $relation ??= function (Model $model): EloquentRelation {
6✔
29
            /** @phpstan-var \Cone\Root\Tests\phpstan\MetaDataModel $model */
30
            $related = $model->metaData()->make();
3✔
31

32
            return $model->metaData()
3✔
33
                ->one()
3✔
34
                ->ofMany(
3✔
35
                    [$related->getCreatedAtColumn() => 'MAX'],
3✔
36
                    function (Builder $query) use ($related): Builder {
3✔
37
                        return $query->where($related->qualifyColumn('key'), $this->getModelAttribute());
3✔
38
                    },
3✔
39
                    'metaData'
3✔
40
                )
3✔
41
                ->withDefault(['key' => $this->getModelAttribute()]);
3✔
42
        };
6✔
43

44
        parent::__construct($label, $modelAttribute, $relation);
6✔
45

46
        $this->as(Text::class);
6✔
47
    }
48

49
    /**
50
     * Get the relation name.
51
     */
52
    public function getRelationName(): string
4✔
53
    {
54
        return $this->relation instanceof Closure
4✔
55
            ? sprintf('__root_%s', $this->getModelAttribute())
4✔
56
            : $this->relation;
4✔
57
    }
58

59
    /**
60
     * Get the field instance.
61
     */
62
    public function getField(): Field
3✔
63
    {
64
        return $this->field;
3✔
65
    }
66

67
    /**
68
     * Set the field class.
69
     */
70
    public function as(string $field, ?Closure $callback = null): static
6✔
71
    {
72
        $this->field = new $field($this->label, $this->getModelAttribute());
6✔
73

74
        $this->field->value(fn (Request $request, Model $model): mixed => $this->resolveValue($request, $model));
6✔
75

76
        if (! is_null($callback)) {
6✔
77
            call_user_func_array($callback, [$this->field]);
×
78
        }
79

80
        return $this;
6✔
81
    }
82

83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function resolveOptions(Request $request, Model $model): array
1✔
87
    {
88
        return [];
1✔
89
    }
90

91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getValue(Model $model): mixed
3✔
95
    {
96
        $name = $this->getRelationName();
3✔
97

98
        if ($this->relation instanceof Closure
3✔
99
            && $model->relationLoaded('metaData')
3✔
100
            && ! $model->relationLoaded($name)
3✔
101
        ) {
NEW
102
            $model->setRelation($name, $model->metaValue($this->getModelAttribute()));
×
103
        }
104

105
        return parent::getValue($model);
3✔
106
    }
107

108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function resolveValue(Request $request, Model $model): mixed
2✔
112
    {
113
        if (is_null($this->valueResolver)) {
2✔
114
            $this->valueResolver = static fn (Request $request, Model $model, mixed $value): mixed => $value?->value;
2✔
115
        }
116

117
        return parent::resolveValue($request, $model);
2✔
118
    }
119

120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function resolveHydrate(Request $request, Model $model, mixed $value): void
1✔
124
    {
125
        if (is_null($this->hydrateResolver)) {
1✔
126
            $this->hydrateResolver = function (Request $request, Model $model, mixed $value): void {
1✔
127
                $related = $this->getValue($model);
1✔
128

129
                $related->setAttribute('value', $value);
1✔
130

131
                $model->setRelation($this->getRelationName(), $related);
1✔
132
            };
1✔
133
        }
134

135
        parent::resolveHydrate($request, $model, $value);
1✔
136
    }
137

138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function toDisplay(Request $request, Model $model): array
1✔
142
    {
143
        return $this->field->toDisplay($request, $model);
1✔
144
    }
145

146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function toInput(Request $request, Model $model): array
1✔
150
    {
151
        return $this->field->toInput($request, $model);
1✔
152
    }
153

154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function toValidate(Request $request, Model $model): array
×
158
    {
159
        return $this->field->toValidate($request, $model);
×
160
    }
161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc