Completed
Push — master ( 9b25f2...d42705 )
by Tomáš
09:42 queued 07:08
created

PostSorter::compareTwoPosts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\PHP7_Sculpin\Renderable\Sorting;
11
12
use Symplify\PHP7_Sculpin\Renderable\File\PostFile;
13
14
final class PostSorter
15
{
16
    /**
17
     * @param PostFile[] $posts
18
     *
19
     * @return PostFile[]
20
     */
21
    public function sort(array $posts) : array
22
    {
23
        // todo: could be skipped by Finder order by name
24
        if (count($posts) < 2) {
25
            return $posts;
26
        }
27
28
        usort($posts, [$this, 'compareTwoPosts']);
29
30
        return $posts;
31
    }
32
33
    private function compareTwoPosts(PostFile $postA, PostFile $postB) : int
34
    {
35
        return strnatcmp($postA->getDate(), $postB->getDate());
36
    }
37
}
38