Conditions | 25 |
Paths | 45 |
Total Lines | 108 |
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 |
||
24 | public function guessType($class, $property, ModelManagerInterface $modelManager) |
||
25 | { |
||
26 | if (!$ret = $this->getParentMetadataForProperty($class, $property, $modelManager)) { |
||
|
|||
27 | return new TypeGuess('text', [], Guess::LOW_CONFIDENCE); |
||
28 | } |
||
29 | |||
30 | [$metadata, $propertyName, $parentAssociationMappings] = $ret; |
||
31 | |||
32 | if ($metadata->hasAssociation($propertyName)) { |
||
33 | $mapping = $metadata->fieldMappings[$propertyName]; |
||
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)) { |
||
45 | case Type::COLLECTION: |
||
46 | case Type::HASH: |
||
47 | return new TypeGuess('array', [], Guess::HIGH_CONFIDENCE); |
||
48 | case 'array': |
||
49 | @trigger_error( |
||
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( |
||
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( |
||
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( |
||
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( |
||
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( |
||
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( |
||
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( |
||
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( |
||
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 |
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.