SetTypeGuesser   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 75
ccs 26
cts 27
cp 0.963
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
B guessType() 0 39 5
1
<?php
2
3
namespace Okapon\DoctrineSetTypeBundle\Form\Guess;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser;
7
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
8
use Symfony\Component\Form\Guess\Guess;
9
use Symfony\Component\Form\Guess\TypeGuess;
10
use Okapon\DoctrineSetTypeBundle\DBAL\Types\AbstractSetType;
11
use Okapon\DoctrineSetTypeBundle\Exception\InvalidClassSpecifiedException;
12
use Symfony\Component\HttpKernel\Kernel;
13
14
/**
15
 * SetTypeGuesser
16
 *
17
 * @author Yuichi Okada <[email protected]>
18
 */
19
class SetTypeGuesser extends DoctrineOrmTypeGuesser
20
{
21
    /**
22
     * @var AbstractSetType[] $registeredTypes Array of registered types
23
     */
24
    protected $registeredTypes = [];
25
26
    /**
27
     * @var string parentSetTypeClass
28
     */
29
    protected $parentSetTypeClass;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param ManagerRegistry $registry Registry
35
     * @param array $registeredTypes Array of registered SET types
36
     * @param string $parentSetTypeClass
37
     */
38 4
    public function __construct(ManagerRegistry $registry, array $registeredTypes, $parentSetTypeClass)
39
    {
40 4
        parent::__construct($registry);
41
42 4
        $this->parentSetTypeClass = $parentSetTypeClass;
43
44 4
        foreach ($registeredTypes as $type => $details) {
45 4
            $this->registeredTypes[$type] = $details['class'];
46 4
        }
47 4
    }
48
49
    /**
50
     * @param string $class
51
     * @param string $property
52
     * @return TypeGuess|null
53
     */
54 4
    public function guessType($class, $property)
55
    {
56 4
        $classMetadata = $this->getMetadata($class);
57 4
        if (!$classMetadata) {
58 1
            return;
59
        }
60
61
        /**
62
         * @var \Doctrine\ORM\Mapping\ClassMetadata $metadata
63
         * @var string $name
64
         */
65 3
        list($metadata) = $classMetadata;
66 3
        $fieldType = $metadata->getTypeOfField($property);
67
68 3
        if (!isset($this->registeredTypes[$fieldType])) {
69 1
            return;
70
        }
71
72 2
        $fullClassName = $this->registeredTypes[$fieldType];
73
74 2
        if (!is_subclass_of($fullClassName, $this->parentSetTypeClass)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $this->parentSetTypeClass can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
75 1
            return;
76
        }
77
78
        // render checkboxes
79
        $parameters = [
80 1
            'choices'  => array_flip($fullClassName::getChoices()),
81 1
            'expanded' => true,
82 1
            'multiple' => true,
83 1
            'required' => !$metadata->isNullable($property),
84 1
        ];
85
86 1
        if (Kernel::MAJOR_VERSION == 2) {
87 1
            $parameters['choices'] = $fullClassName::getChoices();
88 1
            return new TypeGuess('choice', $parameters, Guess::VERY_HIGH_CONFIDENCE);
89
        }
90
91
        return new TypeGuess(ChoiceType::class, $parameters, Guess::VERY_HIGH_CONFIDENCE);
92
    }
93
}
94