GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PlatenumDoctrineType   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 48
dl 0
loc 132
rs 10
c 1
b 0
f 0
ccs 56
cts 56
cp 1
wmc 19

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getSQLDeclaration() 0 3 1
A registerString() 0 13 2
A getName() 0 3 1
A allTraitsOf() 0 17 5
A convertToPHPValue() 0 8 2
A requiresSQLCommentHint() 0 3 1
A registerInteger() 0 11 1
A registerCallback() 0 17 3
A convertToDatabaseValue() 0 12 3
1
<?php
2
declare(strict_types=1);
3
namespace Thunder\Platenum\Doctrine;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Types\Type;
7
use Thunder\Platenum\Enum\AbstractConstantsEnum;
8
use Thunder\Platenum\Enum\EnumTrait;
9
10
/** @psalm-suppress PropertyNotSetInConstructor, MissingConstructor */
11
final class PlatenumDoctrineType extends Type
12
{
13
    /** @var class-string */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
14
    private $platenumClass;
15
    /** @var string */
16
    private $platenumAlias;
17
    /** @var callable(mixed):mixed */
18
    private $platenumCallback;
19
    /** @psalm-var callable(array,AbstractPlatform):string */
20
    private $platenumSql;
21
22
    /**
23
     * @param string $alias
24
     * @psalm-param class-string $class
25 3
     */
26
    public static function registerInteger(string $alias, string $class): void
27
    {
28 3
        /** @psalm-suppress MissingClosureParamType */
29 1
        $toInteger = function($value): int {
30 3
            return (int)$value;
31 3
        };
32 1
        $sql = function(array $declaration, AbstractPlatform $platform): string {
33 3
            return $platform->getIntegerTypeDeclarationSQL([]);
34
        };
35 3
36 2
        self::registerCallback($alias, $class, $toInteger, $sql);
37
    }
38
39
    /**
40
     * @param string $alias
41
     * @psalm-param class-string $class
42 5
     */
43
    public static function registerString(string $alias, string $class): void
44
    {
45 5
        /** @psalm-suppress MissingClosureParamType */
46 2
        $toString = function($value): string {
47 5
            return (string)$value;
48 5
        };
49 1
        $sql = function(array $declaration, AbstractPlatform $platform): string {
50 5
            return method_exists($platform, 'getStringTypeDeclarationSQL')
51
                ? $platform->getStringTypeDeclarationSQL([])
52 5
                : $platform->getVarcharTypeDeclarationSQL([]);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Platforms\...harTypeDeclarationSQL() has been deprecated: Use {@link getStringTypeDeclarationSQL()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

52
                : /** @scrutinizer ignore-deprecated */ $platform->getVarcharTypeDeclarationSQL([]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
53 5
        };
54
55
        self::registerCallback($alias, $class, $toString, $sql);
56
    }
57
58
    /**
59
     * @param string $alias
60
     * @psalm-param class-string $class
61 6
     * @param callable(int|string):mixed $callback
62
     * @psalm-param callable(array<mixed>,AbstractPlatform):string $sql
63 6
     */
64 1
    private static function registerCallback(string $alias, string $class, callable $callback, callable $sql): void
65
    {
66 6
        if(self::hasType($alias)) {
67 1
            throw new \LogicException(sprintf('Alias `%s` was already registered in PlatenumDoctrineType.', $class));
68
        }
69
        if(false === in_array(EnumTrait::class, self::allTraitsOf($class), true)) {
70 5
            throw new \LogicException(sprintf('PlatenumDoctrineType allows only Platenum enumerations, `%s` given.', $class));
71
        }
72
73 5
        self::addType($alias, self::class);
74 5
75 5
        /** @var static $type */
76 5
        $type = self::getType($alias);
77 5
        $type->platenumAlias = $alias;
78 5
        $type->platenumClass = $class;
79
        $type->platenumCallback = $callback;
80
        $type->platenumSql = $sql;
81
    }
82
83
    /**
84 6
     * @param class-string $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
85
     * @psalm-return list<string>
86 6
     */
87
    private static function allTraitsOf(string $class): array
88
    {
89 6
        $traits = [];
90 5
91
        do {
92 6
            foreach(class_uses($class, true) as $fqcn) {
93
                $traits[] = $fqcn;
94 6
            }
95 5
        } while($class = get_parent_class($class));
96 5
97
        foreach ($traits as /* $trait => */ $same) {
98
            foreach(class_uses($same, true) as $fqcn) {
99
                $traits[] = $fqcn;
100 6
            }
101
        }
102
103 1
        return $traits;
104
    }
105 1
106
    public function getName(): string
107
    {
108
        return $this->platenumAlias;
109 1
    }
110
111 1
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
112
    {
113
        return ($this->platenumSql)($column, $platform);
114 3
    }
115
116 3
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
117 1
    {
118
        if (null === $value) {
119 3
            return null;
120 1
        }
121 1
        if(false === is_object($value)) {
122
            $message = 'Impossible situation: `%s` allows to register only Platenum types, `%s` given.';
123
            throw new \LogicException(sprintf($message, self::class, gettype($value)));
124
        }
125 2
126
        /** @var AbstractConstantsEnum $value */
127
        return call_user_func($this->platenumCallback, $value->getValue());
128 1
    }
129
130 1
    public function convertToPHPValue($value, AbstractPlatform $platform)
131 1
    {
132
        if (null === $value) {
133
            return null;
134
        }
135 1
136
        /** @psalm-suppress MixedMethodCall */
137
        return ($this->platenumClass)::fromValue(($this->platenumCallback)($value));
138 1
    }
139
140 1
    public function requiresSQLCommentHint(AbstractPlatform $platform): bool
141
    {
142
        return true;
143
    }
144
}
145