Completed
Push — master ( d28c7c...459941 )
by
unknown
01:31 queued 11s
created

TypeGuesser::guessType()   F

Complexity

Conditions 25
Paths 45

Size

Total Lines 108

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 108
rs 3.3333
c 0
b 0
f 0
cc 25
nc 45
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\ClassMetadata;
17
use Doctrine\ODM\MongoDB\Types\Type;
18
use Sonata\AdminBundle\Model\ModelManagerInterface;
19
use Symfony\Component\Form\Guess\Guess;
20
use Symfony\Component\Form\Guess\TypeGuess;
21
22
class TypeGuesser extends AbstractTypeGuesser
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function guessType($class, $property, ModelManagerInterface $modelManager)
28
    {
29
        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...
30
            return new TypeGuess('text', [], Guess::LOW_CONFIDENCE);
31
        }
32
33
        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...
34
35
        if ($metadata->hasAssociation($propertyName)) {
36
            $mapping = $metadata->fieldMappings[$propertyName];
37
38
            switch ($mapping['type']) {
39
                case ClassMetadata::ONE:
40
                    return new TypeGuess('mongo_one', [], Guess::HIGH_CONFIDENCE);
41
42
                case ClassMetadata::MANY:
43
                    return new TypeGuess('mongo_many', [], Guess::HIGH_CONFIDENCE);
44
            }
45
        }
46
47
        switch ($metadata->getTypeOfField($propertyName)) {
48
            case Type::COLLECTION:
49
            case Type::HASH:
50
                return new TypeGuess('array', [], Guess::HIGH_CONFIDENCE);
51
            case 'array':
52
                @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
53
                    'The array type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
54
                    E_USER_DEPRECATED
55
                );
56
57
                return new TypeGuess('array', [], Guess::HIGH_CONFIDENCE);
58
            case Type::BOOL:
59
            case Type::BOOLEAN:
60
                return new TypeGuess('boolean', [], Guess::HIGH_CONFIDENCE);
61
            case 'datetime':
62
                @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
63
                    'The datetime type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
64
                    E_USER_DEPRECATED
65
                );
66
67
                return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE);
68
            case 'vardatetime':
69
                @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
70
                    'The vardatetime type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
71
                    E_USER_DEPRECATED
72
                );
73
74
                return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE);
75
            case 'datetimetz':
76
                @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
77
                    'The datetimetz type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
78
                    E_USER_DEPRECATED
79
                );
80
81
                return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE);
82
            case Type::TIMESTAMP:
83
                return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE);
84
            case Type::DATE:
85
            // NEXT_MAJOR: Use only the constant when dropping support for doctrine/mongodb-odm 1.3.
86
            // case Type::DATE_IMMUTABLE:
87
            case 'date_immutable':
88
                return new TypeGuess('date', [], Guess::HIGH_CONFIDENCE);
89
            case 'decimal':
90
                @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
91
                    'The decimal type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
92
                    E_USER_DEPRECATED
93
                );
94
95
                return new TypeGuess('number', [], Guess::MEDIUM_CONFIDENCE);
96
            case Type::FLOAT:
97
                return new TypeGuess('number', [], Guess::MEDIUM_CONFIDENCE);
98
            case Type::INTEGER:
99
            case Type::INT:
100
                return new TypeGuess('integer', [], Guess::MEDIUM_CONFIDENCE);
101
            case 'bigint':
102
                @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
103
                    'The bigint type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
104
                    E_USER_DEPRECATED
105
                );
106
107
                return new TypeGuess('integer', [], Guess::MEDIUM_CONFIDENCE);
108
            case 'smallint':
109
                @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
110
                    'The smallint type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
111
                    E_USER_DEPRECATED
112
                );
113
114
                return new TypeGuess('integer', [], Guess::MEDIUM_CONFIDENCE);
115
            case Type::STRING:
116
                return new TypeGuess('text', [], Guess::MEDIUM_CONFIDENCE);
117
            case 'text':
118
                @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
119
                    'The text type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
120
                    E_USER_DEPRECATED
121
                );
122
123
                return new TypeGuess('textarea', [], Guess::MEDIUM_CONFIDENCE);
124
            case 'time':
125
                @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
126
                    'The time type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
127
                    E_USER_DEPRECATED
128
                );
129
130
                return new TypeGuess('time', [], Guess::HIGH_CONFIDENCE);
131
            default:
132
                return new TypeGuess('text', [], Guess::LOW_CONFIDENCE);
133
        }
134
    }
135
}
136