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

ICanBoogie / ActiveRecord / 4437457574

pending completion
4437457574

push

github

Olivier Laviale
Case of a nullable belong_to

7 of 7 new or added lines in 1 file covered. (100.0%)

1356 of 1686 relevant lines covered (80.43%)

34.68 hits per line

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

84.0
/lib/ActiveRecord/RelationCollection.php
1
<?php
2

3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace ICanBoogie\ActiveRecord;
13

14
use ArrayAccess;
15
use Closure;
16
use ICanBoogie\ActiveRecord;
17
use ICanBoogie\OffsetNotWritable;
18

19
/**
20
 * Relation collection of a model.
21
 *
22
 * @implements ArrayAccess<string, Relation>
23
 */
24
class RelationCollection implements ArrayAccess
25
{
26
    /**
27
     * Relations.
28
     *
29
     * @var array<string, Relation>
30
     */
31
    private array $relations;
32

33
    /**
34
     * @param Model<int|string|string[], ActiveRecord> $model The parent model.
35
     */
36
    public function __construct(
37
        public readonly Model $model
38
    ) {
39
    }
97✔
40

41
    /**
42
     * Checks if a relation exists.
43
     *
44
     * @param string $offset Relation name.
45
     */
46
    public function offsetExists(mixed $offset): bool
47
    {
48
        return isset($this->relations[$offset]);
×
49
    }
50

51
    /**
52
     * Returns a relation.
53
     *
54
     * @param string $offset Relation name.
55
     *
56
     * @throws RelationNotDefined if the relation is not defined.
57
     */
58
    public function offsetGet(mixed $offset): Relation
59
    {
60
        return $this->relations[$offset]
3✔
61
            ?? throw new RelationNotDefined($offset, $this);
3✔
62
    }
63

64
    /**
65
     * @throws OffsetNotWritable because relations cannot be set.
66
     */
67
    public function offsetSet(mixed $offset, mixed $value): void
68
    {
69
        throw new OffsetNotWritable([ $offset, $this ]);
×
70
    }
71

72
    /**
73
     * @throws OffsetNotWritable because relations cannot be unset.
74
     */
75
    public function offsetUnset(mixed $offset): void
76
    {
77
        throw new OffsetNotWritable([ $offset, $this ]);
×
78
    }
79

80
    /**
81
     * Adds a {@link BelongsToRelation} relation.
82
     */
83
    public function belongs_to(
84
        string $related,
85
        string $local_key,
86
        string $foreign_key,
87
        string $as,
88
    ): void
89
    {
90
        $this->relations[$as] = new BelongsToRelation(
70✔
91
            owner: $this->model,
70✔
92
            related: $related,
70✔
93
            local_key: $local_key,
70✔
94
            foreign_key: $foreign_key,
70✔
95
            as: $as,
70✔
96
        );
70✔
97
    }
98

99
    /**
100
     * Adds a {@link HasManyRelation} relation.
101
     */
102
    public function has_many(
103
        string $related,
104
        string $local_key,
105
        string $foreign_key,
106
        string $as,
107
        ?string $through = null,
108
    ): void
109
    {
110
        $this->relations[$as] = new HasManyRelation(
85✔
111
            owner: $this->model,
85✔
112
            related: $related,
85✔
113
            local_key: $local_key,
85✔
114
            foreign_key: $foreign_key,
85✔
115
            as: $as,
85✔
116
            through: $through,
85✔
117
        );
85✔
118
    }
119

120
    /**
121
     * @param (Closure(Relation, string $as): ?Relation) $predicate
122
     *
123
     * @return Relation|null
124
     */
125
    public function find(Closure $predicate): ?Relation
126
    {
127
        foreach ($this->relations as $as => $relation) {
3✔
128
            if ($predicate($relation, $as)) {
3✔
129
                return $relation;
3✔
130
            }
131
        }
132

133
        return null;
×
134
    }
135
}
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

© 2026 Coveralls, Inc