Completed
Push — master ( 1c87cd...c75e6a )
by Alejandro
03:14
created

PhpEnumType::convertToDatabaseValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 2
crap 2
1
<?php
2
namespace Acelaya\Doctrine\Type;
3
4
use Acelaya\Doctrine\Exception\InvalidArgumentException;
5
use Doctrine\DBAL\DBALException;
6
use MyCLabs\Enum\Enum;
7
8
class PhpEnumType extends AbstractPhpEnumType
9
{
10
    /**
11
     * @param $typeNameOrEnumClass
12
     * @param null $enumClass
13
     * @throws InvalidArgumentException
14
     * @throws DBALException
15
     */
16 9
    public static function registerEnumType($typeNameOrEnumClass, $enumClass = null)
17
    {
18 9
        $typeName = $typeNameOrEnumClass;
19 9
        $enumClass = $enumClass ?: $typeNameOrEnumClass;
20
21 9 View Code Duplication
        if (! is_subclass_of($enumClass, Enum::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \MyCLabs\Enum\Enum::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22 1
            throw new InvalidArgumentException(sprintf(
23 1
                'Provided enum class "%s" is not valid. Enums must extend "%s"',
24 1
                $enumClass,
25
                Enum::class
26 1
            ));
27
        }
28
29
        // Register and customize the type
30 8
        self::addType($typeName, static::class);
31
        /** @var PhpEnumType $type */
32 8
        $type = self::getType($typeName);
33 8
        $type->name = $typeName;
34 8
        $type->enumClass = $enumClass;
35 8
    }
36
37
    /**
38
     * @param array $types
39
     * @throws InvalidArgumentException
40
     * @throws DBALException
41
     */
42 2 View Code Duplication
    public static function registerEnumTypes(array $types)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44 2
        foreach ($types as $typeName => $enumClass) {
45 2
            $typeName = is_string($typeName) ? $typeName : $enumClass;
46 2
            static::registerEnumType($typeName, $enumClass);
47 2
        }
48 2
    }
49
}
50