DateFormatter::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 3
dl 0
loc 14
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Formatters\Collection;
6
7
use Carbon\CarbonInterface;
0 ignored issues
show
Bug introduced by
The type Carbon\CarbonInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Illuminate\Support\Carbon;
9
use MichaelRubel\Formatters\Formatter;
10
11
class DateFormatter implements Formatter
12
{
13
    /**
14
     * @param  string|CarbonInterface|null  $date
15
     * @param  string|null  $timezone
16
     * @param  string|null  $date_format
17
     */
18 13
    public function __construct(
19
        public string|null|CarbonInterface $date = null,
20
        public ?string $timezone = null,
21
        public ?string $date_format = 'Y-m-d',
22
    ) {
23 13
        if (empty($this->date)) {
24 2
            $this->date = null;
25
        }
26
27 13
        if (is_string($this->date)) {
28 6
            $this->date = app(Carbon::class)->parse($this->date);
29
        }
30
31 13
        $this->timezone ??= config('app.timezone', 'UTC');
32
    }
33
34
    /**
35
     * Format the date.
36
     *
37
     * @return string|null
38
     */
39 13
    public function format(): ?string
40
    {
41 13
        return $this->date
42 13
            ?->setTimezone($this->timezone)
43 13
            ->translatedFormat($this->date_format);
44
    }
45
}
46