Failed Conditions
Push — master ( e75200...fab761 )
by Adrien
02:43
created

ChronosType   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 15
c 1
b 0
f 0
dl 0
loc 38
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToDatabaseValue() 0 11 3
A convertToPHPValue() 0 17 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\DBAL\Types;
6
7
use Cake\Chronos\Chronos;
1 ignored issue
show
Bug introduced by
The type Cake\Chronos\Chronos 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 DateTimeInterface;
9
use Doctrine\DBAL\Platforms\AbstractPlatform;
10
use Doctrine\DBAL\Types\ConversionException;
11
use Doctrine\DBAL\Types\DateTimeType;
12
13
final class ChronosType extends DateTimeType
14
{
15
    /**
16
     * @return ($value is null ? null : string)
17
     */
18 2
    public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
19
    {
20 2
        if ($value === null) {
21 1
            return $value;
22
        }
23
24 2
        if ($value instanceof DateTimeInterface) {
25 1
            return $value->format($platform->getDateTimeFormatString());
26
        }
27
28 1
        throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'Chronos']);
29
    }
30
31
    /**
32
     * @return ($value is null ? null : Chronos)
33
     */
34 2
    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Chronos
35
    {
36 2
        if ($value === null || $value instanceof Chronos) {
37 1
            return $value;
38
        }
39
40 2
        if (!is_string($value) && !$value instanceof DateTimeInterface) {
41 1
            throw ConversionException::conversionFailedFormat(
42 1
                $value,
43 1
                $this->getName(),
44 1
                $platform->getDateTimeFormatString(),
45 1
            );
46
        }
47
48 1
        $val = new Chronos($value);
49
50 1
        return $val;
51
    }
52
}
53