Completed
Push — master ( 3a9e59...e67d5e )
by
unknown
07:03
created

FilterTypeGuesser::guessType()   C

Complexity

Conditions 16
Paths 27

Size

Total Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 5.5666
c 0
b 0
f 0
cc 16
nc 27
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\Bundle\MongoDBBundle\Form\Type\DocumentType;
17
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
18
use Sonata\AdminBundle\Model\ModelManagerInterface;
19
use Sonata\CoreBundle\Form\Type\BooleanType;
20
use Sonata\CoreBundle\Form\Type\EqualType;
21
use Sonata\DoctrineMongoDBAdminBundle\Model\MissingPropertyMetadataException;
22
use Symfony\Component\Form\Extension\Core\Type\NumberType;
23
use Symfony\Component\Form\Extension\Core\Type\TextType;
24
use Symfony\Component\Form\Guess\Guess;
25
use Symfony\Component\Form\Guess\TypeGuess;
26
27
class FilterTypeGuesser extends AbstractTypeGuesser
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function guessType($class, $property, ModelManagerInterface $modelManager)
33
    {
34
        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...
35
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Sonata\AdminBundle\Guess...serInterface::guessType of type Symfony\Component\Form\Guess\Guess|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
36
        }
37
38
        $options = [
39
            'field_type' => null,
40
            'field_options' => [],
41
            'options' => [],
42
        ];
43
44
        list($metadata, $propertyName, $parentAssociationMappings) = $ret;
45
46
        $options['parent_association_mappings'] = $parentAssociationMappings;
47
48
        if ($metadata->hasAssociation($propertyName)) {
49
            $mapping = $metadata->fieldMappings[$propertyName];
50
51
            switch ($mapping['type']) {
52
                case ClassMetadata::ONE:
53
                case ClassMetadata::MANY:
54
                    $options['operator_type'] = EqualType::class;
55
                    $options['operator_options'] = [];
56
57
                    $options['field_type'] = DocumentType::class;
58
                    $options['field_options'] = [
59
                        'class' => $mapping['targetDocument'],
60
                    ];
61
62
                    $options['field_name'] = $mapping['fieldName'];
63
                    $options['mapping_type'] = $mapping['type'];
64
65
                    return new TypeGuess('doctrine_mongo_model', $options, Guess::HIGH_CONFIDENCE);
66
            }
67
        }
68
69
        if (!\array_key_exists($propertyName, $metadata->fieldMappings)) {
70
            throw new MissingPropertyMetadataException($class, $property);
71
        }
72
73
        $options['field_name'] = $metadata->fieldMappings[$propertyName]['fieldName'];
74
75
        switch ($metadata->getTypeOfField($propertyName)) {
76
            case 'boolean':
77
                $options['field_type'] = BooleanType::class;
78
                $options['field_options'] = [];
79
80
                return new TypeGuess('doctrine_mongo_boolean', $options, Guess::HIGH_CONFIDENCE);
81
//            case 'datetime':
82
//            case 'vardatetime':
83
//            case 'datetimetz':
84
//                return new TypeGuess('doctrine_orm_datetime', $options, Guess::HIGH_CONFIDENCE);
85
//            case 'date':
86
//                return new TypeGuess('doctrine_orm_date', $options, Guess::HIGH_CONFIDENCE);
87
            case 'decimal':
88
            case 'float':
89
                return new TypeGuess('doctrine_mongo_number', $options, Guess::MEDIUM_CONFIDENCE);
90
            case 'int':
91
            case 'bigint':
92
            case 'smallint':
93
                $options['field_type'] = NumberType::class;
94
95
                return new TypeGuess('doctrine_mongo_number', $options, Guess::MEDIUM_CONFIDENCE);
96
            case 'id':
97
            case 'string':
98
            case 'text':
99
                $options['field_type'] = TextType::class;
100
101
                return new TypeGuess('doctrine_mongo_string', $options, Guess::MEDIUM_CONFIDENCE);
102
            case 'time':
103
                return new TypeGuess('doctrine_mongo_time', $options, Guess::HIGH_CONFIDENCE);
104
            default:
105
                return new TypeGuess('doctrine_mongo_string', $options, Guess::LOW_CONFIDENCE);
106
        }
107
    }
108
}
109