TypeGuesser::guessType()   F
last analyzed

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
    public function guessType($class, $property, ModelManagerInterface $modelManager)
25
    {
26
        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...
27
            return new TypeGuess('text', [], Guess::LOW_CONFIDENCE);
28
        }
29
30
        [$metadata, $propertyName, $parentAssociationMappings] = $ret;
0 ignored issues
show
Bug introduced by
The variable $metadata does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $propertyName does not exist. Did you mean $property?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $parentAssociationMappings does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
31
32
        if ($metadata->hasAssociation($propertyName)) {
0 ignored issues
show
Bug introduced by
The variable $propertyName does not exist. Did you mean $property?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
33
            $mapping = $metadata->fieldMappings[$propertyName];
0 ignored issues
show
Bug introduced by
The variable $propertyName does not exist. Did you mean $property?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
34
35
            switch ($mapping['type']) {
36
                case ClassMetadata::ONE:
37
                    return new TypeGuess('mongo_one', [], Guess::HIGH_CONFIDENCE);
38
39
                case ClassMetadata::MANY:
40
                    return new TypeGuess('mongo_many', [], Guess::HIGH_CONFIDENCE);
41
            }
42
        }
43
44
        switch ($metadata->getTypeOfField($propertyName)) {
0 ignored issues
show
Bug introduced by
The variable $propertyName does not exist. Did you mean $property?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
45
            case Type::COLLECTION:
46
            case Type::HASH:
47
                return new TypeGuess('array', [], Guess::HIGH_CONFIDENCE);
48
            case 'array':
49
                @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...
50
                    'The array type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
51
                    E_USER_DEPRECATED
52
                );
53
54
                return new TypeGuess('array', [], Guess::HIGH_CONFIDENCE);
55
            case Type::BOOL:
56
            case Type::BOOLEAN:
57
                return new TypeGuess('boolean', [], Guess::HIGH_CONFIDENCE);
58
            case 'datetime':
59
                @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...
60
                    'The datetime type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
61
                    E_USER_DEPRECATED
62
                );
63
64
                return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE);
65
            case 'vardatetime':
66
                @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...
67
                    'The vardatetime type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
68
                    E_USER_DEPRECATED
69
                );
70
71
                return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE);
72
            case 'datetimetz':
73
                @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...
74
                    'The datetimetz type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
75
                    E_USER_DEPRECATED
76
                );
77
78
                return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE);
79
            case Type::TIMESTAMP:
80
                return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE);
81
            case Type::DATE:
82
            // NEXT_MAJOR: Use only the constant when dropping support for doctrine/mongodb-odm 1.3.
83
            // case Type::DATE_IMMUTABLE:
84
            case 'date_immutable':
85
                return new TypeGuess('date', [], Guess::HIGH_CONFIDENCE);
86
            case 'decimal':
87
                @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...
88
                    'The decimal type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
89
                    E_USER_DEPRECATED
90
                );
91
92
                return new TypeGuess('number', [], Guess::MEDIUM_CONFIDENCE);
93
            case Type::FLOAT:
94
                return new TypeGuess('number', [], Guess::MEDIUM_CONFIDENCE);
95
            case Type::INTEGER:
96
            case Type::INT:
97
                return new TypeGuess('integer', [], Guess::MEDIUM_CONFIDENCE);
98
            case 'bigint':
99
                @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...
100
                    'The bigint type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
101
                    E_USER_DEPRECATED
102
                );
103
104
                return new TypeGuess('integer', [], Guess::MEDIUM_CONFIDENCE);
105
            case 'smallint':
106
                @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...
107
                    'The smallint type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
108
                    E_USER_DEPRECATED
109
                );
110
111
                return new TypeGuess('integer', [], Guess::MEDIUM_CONFIDENCE);
112
            case Type::STRING:
113
                return new TypeGuess('text', [], Guess::MEDIUM_CONFIDENCE);
114
            case 'text':
115
                @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...
116
                    'The text type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
117
                    E_USER_DEPRECATED
118
                );
119
120
                return new TypeGuess('textarea', [], Guess::MEDIUM_CONFIDENCE);
121
            case 'time':
122
                @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...
123
                    'The time type is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'.
124
                    E_USER_DEPRECATED
125
                );
126
127
                return new TypeGuess('time', [], Guess::HIGH_CONFIDENCE);
128
            default:
129
                return new TypeGuess('text', [], Guess::LOW_CONFIDENCE);
130
        }
131
    }
132
}
133