Completed
Push — feature-output-formats ( 361247...47e05f )
by Arnaud
02:09
created

Collection::sortByWeight()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4222
c 0
b 0
f 0
cc 5
nc 1
nop 0
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Collection\Page;
10
11
use Cecil\Collection\Collection as CecilCollection;
12
13
/**
14
 * Class Collection.
15
 */
16
class Collection extends CecilCollection
17
{
18
    /**
19
     * Sort items by date: the most recent first.
20
     *
21
     * @return self
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
22
     */
23 View Code Duplication
    public function sortByDate(): self
24
    {
25
        return $this->usort(function ($a, $b) {
26
            if (!isset($a['date'])) {
27
                return -1;
28
            }
29
            if (!isset($b['date'])) {
30
                return 1;
31
            }
32
            if ($a['date'] == $b['date']) {
33
                return 0;
34
            }
35
36
            return ($a['date'] > $b['date']) ? -1 : 1;
37
        });
38
    }
39
40
    /**
41
     * Sort items by title (natural sort).
42
     *
43
     * @return self
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
44
     */
45
    public function sortByTitle(): self
46
    {
47
        return $this->usort(function ($a, $b) {
48
            return strnatcmp($a['title'], $b['title']);
49
        });
50
    }
51
52
    /**
53
     * Sort by weight: the heaviest first.
54
     *
55
     * @return self
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
56
     */
57 View Code Duplication
    public function sortByWeight(): self
58
    {
59
        return $this->usort(function ($a, $b) {
60
            if (!isset($a['weight'])) {
61
                return -1;
62
            }
63
            if (!isset($b['weight'])) {
64
                return 1;
65
            }
66
            if ($a['weight'] == $b['weight']) {
67
                return 0;
68
            }
69
70
            return ($a['weight'] > $b['weight']) ? -1 : 1;
71
        });
72
    }
73
}
74