DefaultDateFormatter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 73
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B format() 0 38 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Marek\SiteAccessAwareDateFormatBundle\Core;
6
7
use DateTimeInterface;
8
use eZ\Publish\Core\MVC\ConfigResolverInterface;
9
use eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface;
10
use IntlDateFormatter;
11
use Marek\SiteAccessAwareDateFormatBundle\API\DateFormatter;
12
use Marek\SiteAccessAwareDateFormatBundle\DependencyInjection\Configuration;
13
14
final class DefaultDateFormatter implements DateFormatter
15
{
16
    /**
17
     * @var \eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface
18
     */
19
    private $localeConverter;
20
21
    /**
22
     * @var array
23
     */
24
    private $languages;
25
26
    /**
27
     * @var \eZ\Publish\Core\MVC\ConfigResolverInterface
28
     */
29
    private $configResolver;
30
31
    /**
32
     * DefaultDateFormatter constructor.
33
     *
34
     * @param \eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface $localeConverter
35
     * @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver
36
     * @param array $languages
37
     */
38
    public function __construct(LocaleConverterInterface $localeConverter, ConfigResolverInterface $configResolver, array $languages)
39
    {
40
        $this->localeConverter = $localeConverter;
41
        $this->languages = $languages;
42
        $this->configResolver = $configResolver;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function format(DateTimeInterface $date, string $dateFormatIdentifier): string
49
    {
50
        $defaultFormat = 'short';
51
        if ($this->configResolver->hasParameter('defaults', Configuration::TREE_ROOT)) {
52
            $defaultFormat = $this->configResolver->getParameter('defaults', Configuration::TREE_ROOT);
53
            $defaultFormat = $defaultFormat['format'];
54
        }
55
56
        switch ($defaultFormat) {
57
            case 'none':
58
                $dateConstant = IntlDateFormatter::NONE;
59
60
                break;
61
            case 'full':
62
                $dateConstant = IntlDateFormatter::FULL;
63
64
                break;
65
            case 'long':
66
                $dateConstant = IntlDateFormatter::LONG;
67
68
                break;
69
            case 'medium':
70
                $dateConstant = IntlDateFormatter::MEDIUM;
71
72
                break;
73
            default:
74
                $dateConstant = IntlDateFormatter::SHORT;
75
76
                break;
77
        }
78
        $formatter = new IntlDateFormatter(
79
            $this->localeConverter->convertToPOSIX($this->languages[0]),
80
            $dateConstant,
81
            $dateConstant
82
        );
83
84
        return $formatter->format($date);
0 ignored issues
show
Documentation introduced by
$date is of type object<DateTimeInterface>, but the function expects a integer|object<DateTime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
    }
86
}
87