Conditions | 12 |
Paths | 28 |
Total Lines | 73 |
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 |
||
96 | public static function prepareEntityDetailsFromInput($input) |
||
97 | { |
||
98 | $entityGuids = ! empty($input['entityIds']) ? array_map('sanitize_text_field', (array) $input['entityIds']) : []; |
||
99 | $entityType = ! empty($input['entityType']) ? sanitize_text_field($input['entityType']) : null; |
||
100 | |||
101 | /** |
||
102 | * Make sure we have the IDs and entity type |
||
103 | */ |
||
104 | if (empty($entityGuids) || empty($entityType)) { |
||
105 | throw new UserError( |
||
106 | // translators: the placeholders are the names of the fields |
||
107 | sprintf(esc_html__('%1$s and %2$s are required.', 'event_espresso'), 'entityIds', 'entityType') |
||
108 | ); |
||
109 | } |
||
110 | |||
111 | $model = EE_Registry::instance()->load_model($entityType); |
||
112 | |||
113 | if (!($model instanceof EEM_Base)) { |
||
114 | throw new UserError( |
||
115 | esc_html__( |
||
116 | 'A valid data model could not be obtained. Did you supply a valid entity type?', |
||
117 | 'event_espresso' |
||
118 | ) |
||
119 | ); |
||
120 | } |
||
121 | |||
122 | // convert GUIDs to DB IDs |
||
123 | $entityDbids = array_map(function ($entityGuid) { |
||
124 | $id_parts = Relay::fromGlobalId($entityGuid); |
||
125 | return ! empty($id_parts['id']) ? absint($id_parts['id']) : 0; |
||
126 | }, (array) $entityGuids); |
||
127 | // remove 0 values |
||
128 | $entityDbids = array_filter($entityDbids); |
||
129 | |||
130 | /** |
||
131 | * If we could not get DB IDs for some GUIDs |
||
132 | */ |
||
133 | if (count($entityDbids) !== count($entityGuids)) { |
||
134 | throw new UserError( |
||
135 | esc_html__('Sorry, operation cancelled due to missing or invalid entity IDs.', 'event_espresso') |
||
136 | ); |
||
137 | } |
||
138 | |||
139 | // e.g. DTT_ID, TKT_ID |
||
140 | $primaryKey = $model->get_primary_key_field()->get_name(); |
||
141 | // e.g. "DTT_ID" will give us "DTT" |
||
142 | $keyPrefix = explode('_', $primaryKey)[0]; |
||
143 | $deletedKey = $keyPrefix . '_deleted'; // e.g. "TKT_deleted" |
||
144 | |||
145 | $entities = $model::instance()->get_all([ |
||
146 | [ |
||
147 | $primaryKey => ['IN', $entityDbids], |
||
148 | $deletedKey => ['IN', [true, false]], |
||
149 | ], |
||
150 | ]); |
||
151 | |||
152 | /** |
||
153 | * If we could not get exactly same number of entities for the given DB IDs |
||
154 | */ |
||
155 | if (count($entityDbids) !== count($entities)) { |
||
156 | throw new UserError(esc_html__('Sorry, operation cancelled due to missing entities.', 'event_espresso')); |
||
157 | } |
||
158 | |||
159 | // Make sure we have an instance for every ID. |
||
160 | foreach ($entityDbids as $entityDbid) { |
||
161 | if (isset($entities[ $entityDbid ]) && $entities[ $entityDbid ] instanceof EE_Base_Class) { |
||
162 | continue; |
||
163 | } |
||
164 | throw new UserError(esc_html__('Sorry, operation cancelled due to invalid entities.', 'event_espresso')); |
||
165 | } |
||
166 | |||
167 | return compact('entities', 'entityGuids', 'entityDbids', 'entityType', 'keyPrefix'); |
||
168 | } |
||
169 | } |
||
170 |