Completed
Push — 2.x ( 359f6e )
by Sullivan
14:45
created

TypeGuesser::guessType()   C

Complexity

Conditions 22
Paths 37

Size

Total Lines 55
Code Lines 42

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 55
rs 6.5597
cc 22
eloc 42
nc 37
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
/*
4
 * This file is part of the Sonata package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrineORMAdminBundle\Guesser;
13
14
use Symfony\Component\Form\Guess\Guess;
15
use Symfony\Component\Form\Guess\TypeGuess;
16
use Doctrine\ORM\Mapping\ClassMetadataInfo;
17
use Sonata\AdminBundle\Model\ModelManagerInterface;
18
19
class TypeGuesser extends AbstractTypeGuesser
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function guessType($class, $property, ModelManagerInterface $modelManager)
25
    {
26
        if (!$ret = $this->getParentMetadataForProperty($class, $property, $modelManager)) {
27
            return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
28
        }
29
30
        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...
31
32
        if ($metadata->hasAssociation($propertyName)) {
33
            $mapping = $metadata->getAssociationMapping($propertyName);
34
35
            switch ($mapping['type']) {
36
                case ClassMetadataInfo::ONE_TO_MANY:
37
                    return new TypeGuess('orm_one_to_many', array(), Guess::HIGH_CONFIDENCE);
38
39
                case ClassMetadataInfo::MANY_TO_MANY:
40
                    return new TypeGuess('orm_many_to_many', array(), Guess::HIGH_CONFIDENCE);
41
42
                case ClassMetadataInfo::MANY_TO_ONE:
43
                    return new TypeGuess('orm_many_to_one', array(), Guess::HIGH_CONFIDENCE);
44
45
                case ClassMetadataInfo::ONE_TO_ONE:
46
                    return new TypeGuess('orm_one_to_one', array(), Guess::HIGH_CONFIDENCE);
47
            }
48
        }
49
50
        switch ($metadata->getTypeOfField($propertyName)) {
51
            case 'array':
52
            case 'json':
53
                return new TypeGuess('array', array(), Guess::HIGH_CONFIDENCE);
54
            case 'boolean':
55
                return new TypeGuess('boolean', array(), Guess::HIGH_CONFIDENCE);
56
            case 'datetime':
57
            case 'vardatetime':
58
            case 'datetimetz':
59
                return new TypeGuess('datetime', array(), Guess::HIGH_CONFIDENCE);
60
            case 'date':
61
                return new TypeGuess('date', array(), Guess::HIGH_CONFIDENCE);
62
            case 'decimal':
63
            case 'float':
64
                return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);
65
            case 'integer':
66
            case 'bigint':
67
            case 'smallint':
68
                return new TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE);
69
            case 'string':
70
                return new TypeGuess('text', array(), Guess::MEDIUM_CONFIDENCE);
71
            case 'text':
72
                return new TypeGuess('textarea', array(), Guess::MEDIUM_CONFIDENCE);
73
            case 'time':
74
                return new TypeGuess('time', array(), Guess::HIGH_CONFIDENCE);
75
            default:
76
                return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
77
        }
78
    }
79
}
80