Completed
Push — master ( 3b4b20...e80e3c )
by
unknown
12:23
created

src/Guesser/FilterTypeGuesser.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineMongoDBAdminBundle\Guesser;
15
16
use Doctrine\Bundle\MongoDBBundle\Form\Type\DocumentType;
17
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
18
use Sonata\AdminBundle\Model\ModelManagerInterface;
19
use Sonata\CoreBundle\Form\Type\BooleanType;
20
use Sonata\CoreBundle\Form\Type\EqualType;
21
use Sonata\DoctrineMongoDBAdminBundle\Model\MissingPropertyMetadataException;
22
use Symfony\Component\Form\Extension\Core\Type\NumberType;
23
use Symfony\Component\Form\Extension\Core\Type\TextType;
24
use Symfony\Component\Form\Guess\Guess;
25
use Symfony\Component\Form\Guess\TypeGuess;
26
27
class FilterTypeGuesser extends AbstractTypeGuesser
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function guessType($class, $property, ModelManagerInterface $modelManager)
33
    {
34
        if (!$ret = $this->getParentMetadataForProperty($class, $property, $modelManager)) {
0 ignored issues
show
$modelManager of type object<Sonata\AdminBundl...\ModelManagerInterface> is not a sub-type of object<Sonata\DoctrineMo...dle\Model\ModelManager>. It seems like you assume a concrete implementation of the interface Sonata\AdminBundle\Model\ModelManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
35
            return false;
36
        }
37
38
        $options = [
39
            'field_type' => null,
40
            'field_options' => [],
41
            'options' => [],
42
        ];
43
44
        list($metadata, $propertyName, $parentAssociationMappings) = $ret;
45
46
        $options['parent_association_mappings'] = $parentAssociationMappings;
47
48
        if ($metadata->hasAssociation($propertyName)) {
49
            $mapping = $metadata->fieldMappings[$propertyName];
50
51
            switch ($mapping['type']) {
52
                case ClassMetadataInfo::ONE:
53
                case ClassMetadataInfo::MANY:
54
                    //case ClassMetadataInfo::MANY_TO_ONE:
55
                    //case ClassMetadataInfo::MANY_TO_MANY:
56
57
                    $options['operator_type'] = EqualType::class;
58
                    $options['operator_options'] = [];
59
60
                    $options['field_type'] = DocumentType::class;
61
                    $options['field_options'] = [
62
                        'class' => $mapping['targetDocument'],
63
                    ];
64
65
                    $options['field_name'] = $mapping['fieldName'];
66
                    $options['mapping_type'] = $mapping['type'];
67
68
                    return new TypeGuess('doctrine_mongo_model', $options, Guess::HIGH_CONFIDENCE);
69
            }
70
        }
71
72
        if (!array_key_exists($propertyName, $metadata->fieldMappings)) {
73
            throw new MissingPropertyMetadataException($class, $property);
74
        }
75
76
        $options['field_name'] = $metadata->fieldMappings[$propertyName]['fieldName'];
77
78
        switch ($metadata->getTypeOfField($propertyName)) {
79
            case 'boolean':
80
                $options['field_type'] = BooleanType::class;
81
                $options['field_options'] = [];
82
83
                return new TypeGuess('doctrine_mongo_boolean', $options, Guess::HIGH_CONFIDENCE);
84
//            case 'datetime':
85
//            case 'vardatetime':
86
//            case 'datetimetz':
87
//                return new TypeGuess('doctrine_orm_datetime', $options, Guess::HIGH_CONFIDENCE);
88
//            case 'date':
89
//                return new TypeGuess('doctrine_orm_date', $options, Guess::HIGH_CONFIDENCE);
90
            case 'decimal':
91
            case 'float':
92
                return new TypeGuess('doctrine_mongo_number', $options, Guess::MEDIUM_CONFIDENCE);
93
            case 'int':
94
            case 'bigint':
95
            case 'smallint':
96
                $options['field_type'] = NumberType::class;
97
98
                return new TypeGuess('doctrine_mongo_number', $options, Guess::MEDIUM_CONFIDENCE);
99
            case 'id':
100
            case 'string':
101
            case 'text':
102
                $options['field_type'] = TextType::class;
103
104
                return new TypeGuess('doctrine_mongo_string', $options, Guess::MEDIUM_CONFIDENCE);
105
            case 'time':
106
                return new TypeGuess('doctrine_mongo_time', $options, Guess::HIGH_CONFIDENCE);
107
            default:
108
                return new TypeGuess('doctrine_mongo_string', $options, Guess::LOW_CONFIDENCE);
109
        }
110
    }
111
}
112