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

Cecilapp / Cecil / 15735686664

18 Jun 2025 02:29PM UTC coverage: 82.609% (-0.02%) from 82.627%
15735686664

push

github

ArnaudLigny
refactor: code quality

0 of 1 new or added line in 1 file covered. (0.0%)

79 existing lines in 5 files now uncovered.

3116 of 3772 relevant lines covered (82.61%)

0.83 hits per line

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

74.47
/src/Collection/Page/Collection.php
1
<?php
2

3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <arnaud@ligny.fr>
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
declare(strict_types=1);
13

14
namespace Cecil\Collection\Page;
15

16
use Cecil\Collection\Collection as CecilCollection;
17
use Cecil\Exception\RuntimeException;
18

19
/**
20
 * Pages collection class.
21
 *
22
 * Represents a collection of pages, providing methods to filter and sort them.
23
 */
24
class Collection extends CecilCollection
25
{
26
    /**
27
     * Returns all "showable" pages.
28
     */
29
    public function showable(): self
30
    {
31
        return $this->filter(function (Page $page) {
1✔
32
            if (
33
                $page->getVariable('published') === true      // page is published
1✔
34
                && (
35
                    $page->getVariable('excluded') !== true   // page is listed
1✔
36
                    && $page->getVariable('exclude') !== true // backward compatibility
1✔
37
                )
38
                && $page->isVirtual() === false               // page is created from a file
1✔
39
                && $page->getVariable('redirect') === null    // page is not a redirection
1✔
40
            ) {
41
                return true;
1✔
42
            }
43
        });
1✔
44
    }
45

46
    /**
47
     * Alias of showable().
48
     */
49
    public function all(): self
50
    {
51
        return $this->showable();
×
52
    }
53

54
    /**
55
     * Sorts pages by.
56
     *
57
     * $options:
58
     * [date|updated|title|weight]
59
     * or
60
     * [
61
     *   variable   => date|updated|title|weight
62
     *   desc_title => false|true
63
     *   reverse    => false|true
64
     * ]
65
     */
66
    public function sortBy(string|array|null $options): self
67
    {
68
        $sortBy = \is_string($options) ? $options : $options['variable'] ?? 'date';
1✔
69
        $sortMethod = \sprintf('sortBy%s', ucfirst(str_replace('updated', 'date', $sortBy)));
1✔
70
        if (!method_exists($this, $sortMethod)) {
1✔
UNCOV
71
            throw new RuntimeException(\sprintf('"%s" is not a valid value for `sortby` to sort collection "%s".', $sortBy, $this->getId()));
×
72
        }
73

74
        return $this->$sortMethod($options);
1✔
75
    }
76

77
    /**
78
     * Sorts pages by date (or 'updated'): the most recent first.
79
     */
80
    public function sortByDate(string|array|null $options = null): self
81
    {
82
        $opt = [];
1✔
83
        // backward compatibility (i.e. $options = 'updated')
84
        if (\is_string($options)) {
1✔
85
            $opt['variable'] = $options;
1✔
86
        }
87
        // options
88
        $opt['variable'] = $options['variable'] ?? 'date';
1✔
89
        $opt['descTitle'] = $options['descTitle'] ?? false;
1✔
90
        $opt['reverse'] = $options['reverse'] ?? false;
1✔
91
        // sort
92
        $pages = $this->usort(function ($a, $b) use ($opt) {
1✔
93
            if ($a[$opt['variable']] == $b[$opt['variable']]) {
1✔
94
                // if dates are equal and "descTitle" is true
95
                if ($opt['descTitle'] && (isset($a['title']) && isset($b['title']))) {
1✔
UNCOV
96
                    return strnatcmp($b['title'], $a['title']);
×
97
                }
98

99
                return 0;
1✔
100
            }
101

102
            return $a[$opt['variable']] > $b[$opt['variable']] ? -1 : 1;
1✔
103
        });
1✔
104
        if ($opt['reverse']) {
1✔
UNCOV
105
            $pages = $pages->reverse();
×
106
        }
107

108
        return $pages;
1✔
109
    }
110

111
    /**
112
     * Sorts pages by title (natural sort).
113
     */
114
    public function sortByTitle(string|array|null $options = null): self
115
    {
116
        $opt = [];
1✔
117
        // options
118
        $opt['reverse'] = $options['reverse'] ?? false;
1✔
119
        // sort
120
        return $this->usort(function ($a, $b) use ($opt) {
1✔
121
            return ($opt['reverse'] ? -1 : 1) * strnatcmp($a['title'], $b['title']);
1✔
122
        });
1✔
123
    }
124

125
    /**
126
     * Sorts by weight (the heaviest first).
127
     */
128
    public function sortByWeight(string|array|null $options = null): self
129
    {
UNCOV
130
        $opt = [];
×
131
        // options
132
        $opt['reverse'] = $options['reverse'] ?? false;
×
133
        // sort
UNCOV
134
        return $this->usort(function ($a, $b) use ($opt) {
×
UNCOV
135
            if ($a['weight'] == $b['weight']) {
×
136
                return 0;
×
137
            }
138

UNCOV
139
            return ($opt['reverse'] ? -1 : 1) * ($a['weight'] < $b['weight'] ? -1 : 1);
×
UNCOV
140
        });
×
141
    }
142

143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function get(string $id): Page
147
    {
148
        return parent::get($id);
1✔
149
    }
150

151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function first(): ?Page
155
    {
156
        return parent::first();
1✔
157
    }
158

159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function filter(\Closure $callback): self
163
    {
164
        return parent::filter($callback);
1✔
165
    }
166

167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function usort(?\Closure $callback = null): self
171
    {
172
        return parent::usort($callback);
1✔
173
    }
174

175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function reverse(): self
179
    {
UNCOV
180
        return parent::reverse();
×
181
    }
182
}
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