These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
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\Form; |
||
12 | |||
13 | use Doctrine\Common\Persistence\ManagerRegistry; |
||
14 | use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType; |
||
15 | use Fresh\DoctrineEnumBundle\Exception\EnumTypeIsRegisteredButClassDoesNotExistException; |
||
16 | use Fresh\DoctrineEnumBundle\Util\LegacyFormHelper; |
||
17 | use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser; |
||
18 | use Symfony\Component\Form\Guess\Guess; |
||
19 | use Symfony\Component\Form\Guess\TypeGuess; |
||
20 | |||
21 | /** |
||
22 | * EnumTypeGuesser. |
||
23 | * |
||
24 | * @author Artem Genvald <[email protected]> |
||
25 | * @author Jaik Dean <[email protected]> |
||
26 | */ |
||
27 | class EnumTypeGuesser extends DoctrineOrmTypeGuesser |
||
28 | { |
||
29 | /** |
||
30 | * @var AbstractEnumType[] $registeredEnumTypes Array of registered ENUM types |
||
31 | */ |
||
32 | protected $registeredEnumTypes = []; |
||
33 | |||
34 | /** |
||
35 | * Constructor. |
||
36 | * |
||
37 | * @param ManagerRegistry $registry Registry |
||
38 | * @param array $registeredTypes Array of registered ENUM types |
||
39 | */ |
||
40 | View Code Duplication | public function __construct(ManagerRegistry $registry, array $registeredTypes) |
|
0 ignored issues
–
show
|
|||
41 | { |
||
42 | parent::__construct($registry); |
||
43 | |||
44 | foreach ($registeredTypes as $type => $details) { |
||
45 | $this->registeredEnumTypes[$type] = $details['class']; |
||
46 | } |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Returns a field guess for a property name of a class. |
||
51 | * |
||
52 | * @param string $class The fully qualified class name |
||
53 | * @param string $property The name of the property to guess for |
||
54 | * |
||
55 | * @return TypeGuess A guess for the field's type and options |
||
56 | * |
||
57 | * @throws EnumTypeIsRegisteredButClassDoesNotExistException |
||
58 | */ |
||
59 | public function guessType($class, $property) |
||
60 | { |
||
61 | $classMetadata = $this->getMetadata($class); |
||
62 | |||
63 | // If no metadata for this class - can't guess anything |
||
64 | if (!$classMetadata) { |
||
65 | return null; |
||
66 | } |
||
67 | |||
68 | /** @var \Doctrine\ORM\Mapping\ClassMetadataInfo $metadata */ |
||
69 | list($metadata) = $classMetadata; |
||
70 | $fieldType = $metadata->getTypeOfField($property); |
||
71 | |||
72 | // This is not one of the registered ENUM types |
||
73 | if (!isset($this->registeredEnumTypes[$fieldType])) { |
||
74 | return null; |
||
75 | } |
||
76 | |||
77 | $enumTypeFullClassName = $this->registeredEnumTypes[$fieldType]; |
||
78 | |||
79 | if (!class_exists($enumTypeFullClassName)) { |
||
80 | throw new EnumTypeIsRegisteredButClassDoesNotExistException(sprintf( |
||
81 | 'ENUM type "%s" is registered as "%s", but that class does not exist', |
||
82 | $fieldType, |
||
83 | $enumTypeFullClassName |
||
84 | )); |
||
85 | } |
||
86 | |||
87 | $abstractEnumTypeFullClassName = 'Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType'; |
||
88 | |||
89 | if (get_parent_class($enumTypeFullClassName) !== $abstractEnumTypeFullClassName) { |
||
90 | return null; |
||
91 | } |
||
92 | |||
93 | // Get the choices from the fully qualified class name |
||
94 | $parameters = [ |
||
95 | 'choices' => $enumTypeFullClassName::getChoices(), |
||
96 | 'required' => !$metadata->isNullable($property), |
||
97 | ]; |
||
98 | |||
99 | // Compatibility with Symfony <3.0 |
||
100 | $fieldType = LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\ChoiceType'); |
||
101 | |||
102 | return new TypeGuess($fieldType, $parameters, Guess::VERY_HIGH_CONFIDENCE); |
||
103 | } |
||
104 | } |
||
105 |
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.