Passed
Branch main (e7afe3)
by Michael
03:26
created

DateFormatter::format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Formatters\Collection;
6
7
use Carbon\CarbonInterface;
8
use Illuminate\Support\Carbon;
9
use Illuminate\Support\Collection;
10
use MichaelRubel\Formatters\Formatter;
11
12
class DateFormatter implements Formatter
13
{
14
    /**
15
     * @param string|CarbonInterface|null $date
16
     * @param string|null $timezone
17
     * @param string|null $date_format
18
     */
19 11
    public function __construct(
20
        public string|null|CarbonInterface $date = null,
21
        public string|null $timezone = null,
22
        public string|null $date_format = 'Y-m-d',
23
    ) {
24 11
        if (! $this->timezone) {
25 9
            $this->timezone = config('app.timezone', 'UTC');
26
        }
27
28 11
        if (! $this->date) {
29 2
            $this->date = now();
30
        }
31
32 11
        if (! $this->date instanceof CarbonInterface) {
33 6
            $this->date = app(Carbon::class)->parse($this->date);
34
        }
35
    }
36
37
    /**
38
     * Format the date.
39
     *
40
     * @return string
41
     */
42 11
    public function format(): string
43
    {
44 11
        return $this->date
45 11
            ->setTimezone($this->timezone)
0 ignored issues
show
Bug introduced by
The method setTimezone() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
            ->/** @scrutinizer ignore-call */ setTimezone($this->timezone)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46 11
            ->format($this->date_format);
47
    }
48
}
49