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

Collection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 55.17 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 32
loc 58
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sortByDate() 16 16 5
A sortByTitle() 0 6 1
A sortByWeight() 16 16 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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