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

ICanBoogie / ActiveRecord / 8587980914

07 Apr 2024 10:12AM UTC coverage: 85.614% (+1.7%) from 83.927%
8587980914

push

github

olvlvl
Tidy Query

13 of 22 new or added lines in 3 files covered. (59.09%)

116 existing lines in 8 files now uncovered.

1345 of 1571 relevant lines covered (85.61%)

24.54 hits per line

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

96.55
/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 Closure;
15
use ICanBoogie\ActiveRecord\Config\Association;
16

17
use function is_string;
18

19
/**
20
 * Relation collection of a model.
21
 */
22
readonly class RelationCollection
23
{
24
    /**
25
     * @var array<string, Relation>
26
     *     Where _key_ is a getter name e.g. 'comments'
27
     */
28
    private array $relations;
29

30
    public function __construct(
31
        public Model $model,
32
        ?Association $association
33
    ) {
34
        $relations = [];
59✔
35

36
        if ($association) {
59✔
37
            foreach ($association->belongs_to as $r) {
59✔
38
                $as = $r->as;
43✔
39
                $relations[$as] = new BelongsToRelation(
43✔
40
                    owner: $this->model,
43✔
41
                    related: $r->associate,
43✔
42
                    local_key: $r->local_key,
43✔
43
                    foreign_key: $r->foreign_key,
43✔
44
                    as: $as,
43✔
45
                );
43✔
46
            }
47

48
            if ($association->has_many) {
59✔
49
                assert(is_string($this->model->primary));
50
            }
51

52
            foreach ($association->has_many as $r) {
59✔
53
                $as = $r->as;
43✔
54
                $relations[$as] = new HasManyRelation(
43✔
55
                    owner: $this->model,
43✔
56
                    related: $r->associate,
43✔
57
                    foreign_key: $r->foreign_key,
43✔
58
                    as: $as,
43✔
59
                    through: $r->through,
43✔
60
                );
43✔
61
            }
62
        }
63

64
        $this->relations = $relations;
59✔
65
    }
66

67
    /**
68
     * Whether a relation exists.
69
     *
70
     * @param non-empty-string $relation_name
71
     */
72
    public function has(string $relation_name): bool
73
    {
74
        return isset($this->relations[$relation_name]);
2✔
75
    }
76

77
    /**
78
     * Returns an existing relation.
79
     *
80
     * @param non-empty-string $relation_name
81
     *
82
     * @throws RelationNotDefined if the relation doesn't exist.
83
     */
84
    public function get(string $relation_name): ?Relation
85
    {
86
        return $this->relations[$relation_name]
3✔
87
            ?? throw new RelationNotDefined($relation_name, $this);
3✔
88
    }
89

90
    /**
91
     * @param (Closure(Relation, string $as): bool) $predicate
92
     */
93
    public function find(Closure $predicate): ?Relation
94
    {
95
        foreach ($this->relations as $as => $relation) {
3✔
96
            if ($predicate($relation, $as)) {
3✔
97
                return $relation;
3✔
98
            }
99
        }
100

UNCOV
101
        return null;
×
102
    }
103
}
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