DateTimeFormatter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 5 1
A __construct() 0 14 3
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 DateTimeFormatter implements Formatter
12
{
13
    /**
14
     * @param  string|null|CarbonInterface  $datetime
15
     * @param  string|null  $timezone
16
     * @param  string|null  $datetime_format
17
     */
18 12
    public function __construct(
19
        public string|null|CarbonInterface $datetime = null,
20
        public ?string $timezone = null,
21
        public ?string $datetime_format = 'Y-m-d H:i',
22
    ) {
23 12
        if (empty($this->datetime)) {
24 2
            $this->datetime = null;
25
        }
26
27 12
        if (is_string($this->datetime)) {
28 6
            $this->datetime = app(Carbon::class)->parse($this->datetime);
29
        }
30
31 12
        $this->timezone ??= config('app.timezone', 'UTC');
32
    }
33
34
    /**
35
     * Format the date and time.
36
     *
37
     * @return string|null
38
     */
39 12
    public function format(): ?string
40
    {
41 12
        return $this->datetime
42 12
            ?->setTimezone($this->timezone)
43 12
            ->translatedFormat($this->datetime_format);
44
    }
45
}
46