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

DateFormatter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 35
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 4
A format() 0 5 1
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