PostRepository   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 34
ccs 9
cts 9
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findById() 0 3 1
A __construct() 0 6 1
A findAll() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Model\Post;
6
7
use DateTimeImmutable;
8
9
use function array_values;
10
11
final class PostRepository
12
{
13
    /**
14
     * @var array<int, Post>
15
     */
16
    private array $posts;
17
18
    /**
19
     * @param array<int, Post>|null $posts
20
     */
21 10
    public function __construct(?array $posts = null)
22
    {
23 10
        $this->posts = $posts ?? [
24 10
            1 => new Post(1, 'Post #1', new DateTimeImmutable('+1 day')),
25 10
            2 => new Post(2, 'Post #2', new DateTimeImmutable('+2 day')),
26 10
            3 => new Post(3, 'Post #3', new DateTimeImmutable('+3 day')),
27
        ];
28
    }
29
30
    /**
31
     * @return Post[]
32
     */
33 3
    public function findAll(): array
34
    {
35 3
        return array_values($this->posts);
36
    }
37
38
    /**
39
     * @param int $id
40
     * @return Post|null
41
     */
42 7
    public function findById(int $id): ?Post
43
    {
44 7
        return $this->posts[$id] ?? null;
45
    }
46
}
47