Calendar::padEnd()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Breadthe\SimpleCalendar;
4
5
use Carbon\Carbon;
6
use Carbon\CarbonImmutable;
7
use Carbon\CarbonPeriod;
8
9
class Calendar
10
{
11
    private $today; // today as a reference (any day passed by the user)
12
    private $padWithNull = false; // pad beginning or end of month with null instead of dates
13
    private $startOfMonth; // first day of the current month
14
    private $endOfMonth; // first day of the current month
15
    private $currentMonth; // the calendar for the current month
16
17
    public $startOfPrevMonth;
18
    public $startOfNextMonth;
19
20
    public function __construct(string $today)
21
    {
22
        $this->today = CarbonImmutable::make($today);
23
        $this->startOfMonth = $this->today->startOfMonth();
24
        $this->endOfMonth = $this->today->endOfMonth();
25
        $this->startOfPrevMonth = Carbon::make($this->today)->startOfMonth()->subDay()->startOfMonth();
26
        $this->startOfNextMonth = Carbon::make($this->today)->endOfMonth()->addDay();
27
    }
28
29
    public static function make(string $today): self
30
    {
31
        return new static($today);
32
    }
33
34
    public function padWithNull(): self
35
    {
36
        $this->padWithNull = true;
37
38
        return $this;
39
    }
40
41
    public function grid(): array
42
    {
43
        $this->currentMonth = CarbonPeriod::create($this->startOfMonth, $this->endOfMonth)->toArray();
44
45
        $this->padStart();
46
47
        $this->padEnd();
48
49
        return $this->currentMonth;
50
    }
51
52
    // If first day of month > 1 (first week doesn't start on a Monday)...
53
    // ...then pad the first week with remaining days from previous month
54 View Code Duplication
    private function padStart(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        // what day in the week is the first of the month?
57
        $startOfMonthWeekday = $this->startOfMonth->isoWeekday();
58
59
        if ($startOfMonthWeekday > 1) {
60
            $currentMonth = $this->currentMonth;
61
62
            $daysToPad = $startOfMonthWeekday - 1;
63
            $prevMonth = CarbonPeriod::create(
64
                $this->startOfMonth->subDays($daysToPad),
65
                $this->startOfMonth->subDay()
66
            )->toArray();
67
68
            $this->padWithNull
69
                ? $this->currentMonth = array_pad($currentMonth, -(count($currentMonth) + $daysToPad), null)
70
                : $this->currentMonth = array_merge($prevMonth, $currentMonth);
71
        }
72
    }
73
74
    // If last day of month < 7 (last week doesn't end on a Sunday)...
75
    // ...then pad the last week with beginning days from next month
76 View Code Duplication
    private function padEnd(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        // what day in the week is the last of the month?
79
        $endOfMonthWeekday = $this->endOfMonth->isoWeekday();
80
81
        if ($endOfMonthWeekday < 7) {
82
            $currentMonth = $this->currentMonth;
83
84
            $daysToPad = 7 - $endOfMonthWeekday;
85
            $nextMonth = CarbonPeriod::create(
86
                $this->endOfMonth->addDay(),
87
                $this->endOfMonth->addDays($daysToPad)
88
            )->toArray();
89
90
            $this->padWithNull
91
                ? $this->currentMonth = array_pad($currentMonth, count($currentMonth) + $daysToPad, null)
92
                : $this->currentMonth = array_merge($currentMonth, $nextMonth);
93
        }
94
    }
95
}
96