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

src/Guesser/TypeGuesser.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\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)) {
29
            return new TypeGuess('text', [], Guess::LOW_CONFIDENCE);
30
        }
31
32
        list($metadata, $propertyName, $parentAssociationMappings) = $ret;
0 ignored issues
show
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