Completed
Push — master ( bae860...fc1986 )
by Grégoire
11s
created

TypeGuesser::guessType()   D

Complexity

Conditions 22
Paths 39

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 4.1666
c 0
b 0
f 0
cc 22
nc 39
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\DoctrineMongoDBAdminBundle\Guesser;
15
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
17
use Sonata\AdminBundle\Model\ModelManagerInterface;
18
use Symfony\Component\Form\Guess\Guess;
19
use Symfony\Component\Form\Guess\TypeGuess;
20
21
class TypeGuesser extends AbstractTypeGuesser
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function guessType($class, $property, ModelManagerInterface $modelManager)
27
    {
28
        if (!$ret = $this->getParentMetadataForProperty($class, $property, $modelManager)) {
0 ignored issues
show
Compatibility introduced by
$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...
29
            return new TypeGuess('text', [], Guess::LOW_CONFIDENCE);
30
        }
31
32
        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...
33
34
        if ($metadata->hasAssociation($propertyName)) {
35
            $mapping = $metadata->fieldMappings[$propertyName];
36
37
            switch ($mapping['type']) {
38
                case ClassMetadataInfo::ONE:
39
                    return new TypeGuess('mongo_one', [], Guess::HIGH_CONFIDENCE);
40
41
                case ClassMetadataInfo::MANY:
42
                    return new TypeGuess('mongo_many', [], Guess::HIGH_CONFIDENCE);
43
            }
44
        }
45
46
        switch ($metadata->getTypeOfField($propertyName)) {
47
            case 'collection':
48
            case 'hash':
49
            case 'array':
50
              return new TypeGuess('array', [], Guess::HIGH_CONFIDENCE);
51
            case 'boolean':
52
                return new TypeGuess('boolean', [], Guess::HIGH_CONFIDENCE);
53
            case 'datetime':
54
            case 'vardatetime':
55
            case 'datetimetz':
56
            case 'timestamp':
57
                return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE);
58
            case 'date':
59
                return new TypeGuess('date', [], Guess::HIGH_CONFIDENCE);
60
            case 'decimal':
61
            case 'float':
62
                return new TypeGuess('number', [], Guess::MEDIUM_CONFIDENCE);
63
            case 'integer':
64
            case 'bigint':
65
            case 'smallint':
66
                return new TypeGuess('integer', [], Guess::MEDIUM_CONFIDENCE);
67
            case 'string':
68
                return new TypeGuess('text', [], Guess::MEDIUM_CONFIDENCE);
69
            case 'text':
70
                return new TypeGuess('textarea', [], Guess::MEDIUM_CONFIDENCE);
71
            case 'time':
72
                return new TypeGuess('time', [], Guess::HIGH_CONFIDENCE);
73
            default:
74
                return new TypeGuess('text', [], Guess::LOW_CONFIDENCE);
75
        }
76
    }
77
}
78