Completed
Push — feature-output-formats ( c119c8...19993f )
by Arnaud
62:02 queued 59:50
created

Collection   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 72
Duplicated Lines 44.44 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 32
loc 72
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 10 2
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
     * Return all not virtual pages.
20
     */
21
    public function all(): self
22
    {
23
        $filteredPages = $this->filter(function (Page $page) {
24
            if ($page->isVirtual() === false) {
25
                return true;
26
            }
27
        });
28
29
        return $filteredPages;
30
    }
31
32
    /**
33
     * Sort items by date: the most recent first.
34
     *
35
     * @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...
36
     */
37 View Code Duplication
    public function sortByDate(): self
38
    {
39
        return $this->usort(function ($a, $b) {
40
            if (!isset($a['date'])) {
41
                return -1;
42
            }
43
            if (!isset($b['date'])) {
44
                return 1;
45
            }
46
            if ($a['date'] == $b['date']) {
47
                return 0;
48
            }
49
50
            return ($a['date'] > $b['date']) ? -1 : 1;
51
        });
52
    }
53
54
    /**
55
     * Sort items by title (natural sort).
56
     *
57
     * @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...
58
     */
59
    public function sortByTitle(): self
60
    {
61
        return $this->usort(function ($a, $b) {
62
            return strnatcmp($a['title'], $b['title']);
63
        });
64
    }
65
66
    /**
67
     * Sort by weight: the heaviest first.
68
     *
69
     * @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...
70
     */
71 View Code Duplication
    public function sortByWeight(): self
72
    {
73
        return $this->usort(function ($a, $b) {
74
            if (!isset($a['weight'])) {
75
                return -1;
76
            }
77
            if (!isset($b['weight'])) {
78
                return 1;
79
            }
80
            if ($a['weight'] == $b['weight']) {
81
                return 0;
82
            }
83
84
            return ($a['weight'] > $b['weight']) ? -1 : 1;
85
        });
86
    }
87
}
88