Completed
Pull Request — master (#101)
by
unknown
02:36
created

AbstractEnumType::getReadableValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
 * This file is part of the FreshDoctrineEnumBundle
4
 *
5
 * (c) Artem Genvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Fresh\DoctrineEnumBundle\DBAL\Types;
12
13
use Doctrine\DBAL\Platforms\AbstractPlatform;
14
Doctrine\DBAL\Platforms\MySqlPlatform;
15
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
16
use Doctrine\DBAL\Platforms\SqlitePlatform;
17
use Doctrine\DBAL\Platforms\SQLServerPlatform;
18
use Doctrine\DBAL\Types\Type;
19
20
/**
21
 * AbstractEnumType.
22
 *
23
 * Provides support of MySQL ENUM type for Doctrine in Symfony applications.
24
 *
25
 * @author Artem Genvald <[email protected]>
26
 * @author Ben Davies <[email protected]>
27
 * @author Jaik Dean <[email protected]>
28
 */
29
abstract class AbstractEnumType extends Type
30
{
31
    /**
32
     * @var string
33
     */
34
    protected $name = '';
35
36
    /**
37
     * @var array Array of ENUM Values, where ENUM values are keys and their readable versions are values
38
     * @static
39
     */
40
    protected static $choices = [];
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
46
    {
47
        if (null === $value) {
48
            return;
49
        }
50
51
        if (!isset(static::$choices[$value])) {
52
            throw new \InvalidArgumentException(sprintf('Invalid value "%s" for ENUM "%s".', $value, $this->getName()));
53
        }
54
55
        return $value;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
62
    {
63
        $values = implode(
64
            ', ',
65
            array_map(
66
                function ($value) {
67
                    return "'{$value}'";
68
                },
69
                static::getValues()
70
            )
71
        );
72
73
        if ($platform instanceof SqlitePlatform) {
74
            return sprintf('TEXT CHECK(%s IN (%s))', $fieldDeclaration['name'], $values);
75
        }
76
77
        if ($platform instanceof PostgreSqlPlatform || $platform instanceof SQLServerPlatform) {
78
            return sprintf('VARCHAR(255) CHECK(%s IN (%s))', $fieldDeclaration['name'], $values);
79
        }
80
81
        return sprintf('ENUM(%s)', $values);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function requiresSQLCommentHint(AbstractPlatform $platform)
88
    {
89
        return true;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getName()
96
    {
97
        return $this->name ?: array_search(get_class($this), self::getTypesMap(), true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->name ?: array_sea...::getTypesMap(), true); of type false|integer|string adds false to the return on line 97 which is incompatible with the return type declared by the abstract method Doctrine\DBAL\Types\Type::getName of type string. It seems like you forgot to handle an error condition.
Loading history...
98
    }
99
100
    /**
101
     * Get readable choices for the ENUM field.
102
     *
103
     * @static
104
     *
105
     * @return array Values for the ENUM field
106
     */
107
    public static function getChoices()
108
    {
109
        return array_flip(static::$choices);
110
    }
111
112
    /**
113
     * Get values for the ENUM field.
114
     *
115
     * @static
116
     *
117
     * @return array Values for the ENUM field
118
     */
119
    public static function getValues()
120
    {
121
        return array_keys(static::$choices);
122
    }
123
124
    /**
125
     * Get array of ENUM Values, where ENUM values are keys and their readable versions are values.
126
     *
127
     * @static
128
     *
129
     * @return array Array of values with readable format
130
     */
131
    public static function getReadableValues()
132
    {
133
        return static::$choices;
134
    }
135
136
    /**
137
     * Get value in readable format.
138
     *
139
     * @param string $value ENUM value
140
     *
141
     * @static
142
     *
143
     * @return string|null $value Value in readable format
144
     *
145
     * @throws \InvalidArgumentException
146
     */
147
    public static function getReadableValue($value)
148
    {
149
        if (!isset(static::$choices[$value])) {
150
            throw new \InvalidArgumentException(sprintf('Invalid value "%s" for ENUM type "%s".', $value, get_called_class()));
151
        }
152
153
        return static::$choices[$value];
154
    }
155
156
    /**
157
     * Check if some string value exists in the array of ENUM values.
158
     *
159
     * @param string $value ENUM value
160
     *
161
     * @static
162
     *
163
     * @return bool
164
     */
165
    public static function isValueExist($value)
166
    {
167
        return isset(static::$choices[$value]);
168
    }
169
    
170
    /**
171
     * Gets an array of database types that map to this Doctrine type.
172
     *
173
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
174
     *
175
     * @return array
176
     */
177
    public function getMappedDatabaseTypes(AbstractPlatform $platform)
178
    {
179
        if ($platform instanceof MySqlPlatform) {
0 ignored issues
show
Bug introduced by
The class Fresh\DoctrineEnumBundle\DBAL\Types\MySqlPlatform does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
180
            return array_merge(
181
                parent::getMappedDatabaseTypes($platform),
182
                [
183
                    'enum',
184
                ]
185
            );
186
        }
187
        
188
        return parent::getMappedDatabaseTypes($platform);
189
    }
190
}
191