TypeGuesser::guessType()   D
last analyzed

Complexity

Conditions 28
Paths 49

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 4.1666
c 0
b 0
f 0
cc 28
nc 49
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\DoctrineORMAdminBundle\Guesser;
15
16
use Doctrine\ORM\Mapping\ClassMetadata;
17
use Sonata\AdminBundle\Model\ModelManagerInterface;
18
use Sonata\AdminBundle\Templating\TemplateRegistry;
19
use Symfony\Component\Form\Guess\Guess;
20
use Symfony\Component\Form\Guess\TypeGuess;
21
22
class TypeGuesser extends AbstractTypeGuesser
23
{
24
    /**
25
     * This is a mapping between the old deprecated value we provided in the TypeGuesser
26
     * and the value we should use to get the correct SonataAdminBundle template. Making the change
27
     * directly in the TypeGuesser would be a BC-break if a user overrides the templates config.
28
     *
29
     * NEXT_MAJOR: remove this constant.
30
     *
31
     * @internal
32
     */
33
    public const DEPRECATED_TYPES = [
34
        'orm_one_to_many' => TemplateRegistry::TYPE_ONE_TO_MANY,
35
        'orm_many_to_many' => TemplateRegistry::TYPE_MANY_TO_MANY,
36
        'orm_many_to_one' => TemplateRegistry::TYPE_MANY_TO_ONE,
37
        'orm_one_to_one' => TemplateRegistry::TYPE_ONE_TO_ONE,
38
        'number' => TemplateRegistry::TYPE_FLOAT,
39
    ];
40
41
    public function guessType($class, $property, ModelManagerInterface $modelManager)
42
    {
43
        if (!$ret = $this->getParentMetadataForProperty($class, $property, $modelManager)) {
44
            return new TypeGuess('text', [], Guess::LOW_CONFIDENCE);
45
        }
46
47
        list($metadata, $propertyName, $parentAssociationMappings) = $ret;
0 ignored issues
show
Unused Code introduced by
The assignment to $parentAssociationMappings is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
48
49
        if ($metadata->hasAssociation($propertyName)) {
50
            $mapping = $metadata->getAssociationMapping($propertyName);
51
52
            switch ($mapping['type']) {
53
                case ClassMetadata::ONE_TO_MANY:
54
                    // NEXT_MAJOR: return new TypeGuess(TemplateRegistry::TYPE_ONE_TO_MANY, [], Guess::HIGH_CONFIDENCE)
55
                    return new TypeGuess('orm_one_to_many', [], Guess::HIGH_CONFIDENCE);
56
57
                case ClassMetadata::MANY_TO_MANY:
58
                    // NEXT_MAJOR: return new TypeGuess(TemplateRegistry::TYPE_MANY_TO_MANY, [], Guess::HIGH_CONFIDENCE)
59
                    return new TypeGuess('orm_many_to_many', [], Guess::HIGH_CONFIDENCE);
60
61
                case ClassMetadata::MANY_TO_ONE:
62
                    // NEXT_MAJOR: return new TypeGuess(TemplateRegistry::TYPE_MANY_TO_ONE, [], Guess::HIGH_CONFIDENCE)
63
                    return new TypeGuess('orm_many_to_one', [], Guess::HIGH_CONFIDENCE);
64
65
                case ClassMetadata::ONE_TO_ONE:
66
                    // NEXT_MAJOR: return new TypeGuess(TemplateRegistry::TYPE_ONE_TO_ONE, [], Guess::HIGH_CONFIDENCE)
67
                    return new TypeGuess('orm_one_to_one', [], Guess::HIGH_CONFIDENCE);
68
            }
69
        }
70
71
        switch ($metadata->getTypeOfField($propertyName)) {
72
            case 'array':
73
            case 'simple_array':
74
            case 'json':
75
            case 'json_array':
76
                return new TypeGuess(TemplateRegistry::TYPE_ARRAY, [], Guess::HIGH_CONFIDENCE);
77
            case 'boolean':
78
                return new TypeGuess(TemplateRegistry::TYPE_BOOLEAN, [], Guess::HIGH_CONFIDENCE);
79
            case 'datetime':
80
            case 'datetime_immutable':
81
            case 'vardatetime':
82
            case 'datetimetz':
83
            case 'datetimetz_immutable':
84
                return new TypeGuess(TemplateRegistry::TYPE_DATETIME, [], Guess::HIGH_CONFIDENCE);
85
            case 'date':
86
            case 'date_immutable':
87
                return new TypeGuess(TemplateRegistry::TYPE_DATE, [], Guess::HIGH_CONFIDENCE);
88
            case 'decimal':
89
            case 'float':
90
                // NEXT_MAJOR: return new TypeGuess(TemplateRegistry::TYPE_FLOAT, [], Guess::LOW_CONFIDENCE)
91
                return new TypeGuess('number', [], Guess::MEDIUM_CONFIDENCE);
92
            case 'integer':
93
            case 'bigint':
94
            case 'smallint':
95
                return new TypeGuess(TemplateRegistry::TYPE_INTEGER, [], Guess::MEDIUM_CONFIDENCE);
96
            case 'string':
97
                return new TypeGuess(TemplateRegistry::TYPE_TEXT, [], Guess::MEDIUM_CONFIDENCE);
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Templ...lateRegistry::TYPE_TEXT has been deprecated with message: since sonata-project/admin-bundle 3.68, to be removed in 4.0. Use Sonata\AdminBundle\Templating\TemplateRegistry::TYPE_STRING instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
98
            case 'text':
99
                return new TypeGuess(TemplateRegistry::TYPE_TEXTAREA, [], Guess::MEDIUM_CONFIDENCE);
100
            case 'time':
101
            case 'time_immutable':
102
                return new TypeGuess(TemplateRegistry::TYPE_TIME, [], Guess::HIGH_CONFIDENCE);
103
            default:
104
                // NEXT_MAJOR: return new TypeGuess(TemplateRegistry::TYPE_STRING, [], Guess::LOW_CONFIDENCE)
105
                return new TypeGuess(TemplateRegistry::TYPE_TEXT, [], Guess::LOW_CONFIDENCE);
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Templ...lateRegistry::TYPE_TEXT has been deprecated with message: since sonata-project/admin-bundle 3.68, to be removed in 4.0. Use Sonata\AdminBundle\Templating\TemplateRegistry::TYPE_STRING instead.

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
106
        }
107
    }
108
}
109