Conditions | 16 |
Paths | 27 |
Total Lines | 76 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
32 | public function guessType($class, $property, ModelManagerInterface $modelManager) |
||
33 | { |
||
34 | if (!$ret = $this->getParentMetadataForProperty($class, $property, $modelManager)) { |
||
|
|||
35 | return false; |
||
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 |
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.