Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like TableRestProxy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TableRestProxy, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
78 | class TableRestProxy extends ServiceRestProxy implements ITable |
||
79 | { |
||
80 | use ServiceRestTrait; |
||
81 | |||
82 | /** |
||
83 | * @var Internal\IODataReaderWriter |
||
84 | */ |
||
85 | private $odataSerializer; |
||
86 | |||
87 | /** |
||
88 | * @var Internal\IMimeReaderWriter |
||
89 | */ |
||
90 | private $mimeSerializer; |
||
91 | |||
92 | /** |
||
93 | * Creates contexts for batch operations. |
||
94 | * |
||
95 | * @param array $operations The batch operations array. |
||
96 | * |
||
97 | * @return array |
||
98 | * |
||
99 | * @throws \InvalidArgumentException |
||
100 | */ |
||
101 | private function createOperationsContexts(array $operations) |
||
156 | |||
157 | /** |
||
158 | * Creates operation context for the API. |
||
159 | * |
||
160 | * @param string $table The table name. |
||
161 | * @param Entity $entity The entity object. |
||
162 | * @param string $type The API type. |
||
163 | * |
||
164 | * @return \MicrosoftAzure\Storage\Common\Internal\Http\HttpCallContext |
||
165 | * |
||
166 | * @throws \InvalidArgumentException |
||
167 | */ |
||
168 | private function getOperationContext($table, Entity $entity, $type) |
||
214 | |||
215 | /** |
||
216 | * Creates MIME part body for batch API. |
||
217 | * |
||
218 | * @param array $operations The batch operations. |
||
219 | * @param array $contexts The contexts objects. |
||
220 | * |
||
221 | * @return array |
||
222 | * |
||
223 | * @throws \InvalidArgumentException |
||
224 | */ |
||
225 | private function createBatchRequestBody(array $operations, array $contexts) |
||
274 | |||
275 | /** |
||
276 | * Constructs HTTP call context for deleteEntity API. |
||
277 | * |
||
278 | * @param string $table The name of the table. |
||
279 | * @param string $partitionKey The entity partition key. |
||
280 | * @param string $rowKey The entity row key. |
||
281 | * @param DeleteEntityOptions $options The optional parameters. |
||
282 | * |
||
283 | * @return HttpCallContext |
||
284 | */ |
||
285 | private function constructDeleteEntityContext( |
||
327 | |||
328 | /** |
||
329 | * Constructs HTTP call context for updateEntity, mergeEntity, |
||
330 | * insertOrReplaceEntity and insertOrMergeEntity. |
||
331 | * |
||
332 | * @param string $table The table name. |
||
333 | * @param Entity $entity The entity instance to use. |
||
334 | * @param string $verb The HTTP method. |
||
335 | * @param boolean $useETag The flag to include etag or not. |
||
336 | * @param TableServiceOptions $options The optional parameters. |
||
337 | * |
||
338 | * @return HttpCallContext |
||
339 | */ |
||
340 | private function constructPutOrMergeEntityContext( |
||
395 | |||
396 | /** |
||
397 | * Constructs HTTP call context for insertEntity API. |
||
398 | * |
||
399 | * @param string $table The name of the table. |
||
400 | * @param Entity $entity The table entity. |
||
401 | * @param TableServiceCreateOptions $options The optional parameters. |
||
402 | * |
||
403 | * @return HttpCallContext |
||
404 | */ |
||
405 | private function constructInsertEntityContext( |
||
454 | |||
455 | /** |
||
456 | * Constructs URI path for entity. |
||
457 | * |
||
458 | * @param string $table The table name. |
||
459 | * @param string $partitionKey The entity's partition key. |
||
460 | * @param string $rowKey The entity's row key. |
||
461 | * |
||
462 | * @return string |
||
463 | */ |
||
464 | private function getEntityPath($table, $partitionKey, $rowKey) |
||
471 | |||
472 | /** |
||
473 | * Creates a promie that does the actual work for update and merge entity |
||
474 | * APIs. |
||
475 | * |
||
476 | * @param string $table The table name. |
||
477 | * @param Entity $entity The entity instance to use. |
||
478 | * @param string $verb The HTTP method. |
||
479 | * @param boolean $useETag The flag to include etag or not. |
||
480 | * @param TableServiceOptions $options The optional parameters. |
||
481 | * |
||
482 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
483 | */ |
||
484 | private function putOrMergeEntityAsyncImpl( |
||
505 | |||
506 | /** |
||
507 | * Builds filter expression |
||
508 | * |
||
509 | * @param Filter $filter The filter object |
||
510 | * |
||
511 | * @return string |
||
512 | */ |
||
513 | private function buildFilterExpression(Filter $filter) |
||
520 | |||
521 | /** |
||
522 | * Builds filter expression |
||
523 | * |
||
524 | * @param Filter $filter The filter object |
||
525 | * @param string &$e The filter expression |
||
526 | * |
||
527 | * @return string |
||
528 | */ |
||
529 | private function buildFilterExpressionRec(Filter $filter, &$e) |
||
565 | |||
566 | /** |
||
567 | * Adds query object to the query parameter array |
||
568 | * |
||
569 | * @param array $queryParam The URI query parameters |
||
570 | * @param Query $query The query object |
||
571 | * |
||
572 | * @return array |
||
573 | */ |
||
574 | private function addOptionalQuery(array $queryParam, Query $query) |
||
610 | |||
611 | /** |
||
612 | * Encodes OData URI values |
||
613 | * |
||
614 | * @param array $values The OData URL values |
||
615 | * |
||
616 | * @return array |
||
617 | */ |
||
618 | private function encodeODataUriValues(array $values) |
||
628 | |||
629 | /** |
||
630 | * Encodes OData URI value |
||
631 | * |
||
632 | * @param string $value The OData URL value |
||
633 | * |
||
634 | * @return string |
||
635 | */ |
||
636 | private function encodeODataUriValue($value) |
||
647 | |||
648 | /** |
||
649 | * Initializes new TableRestProxy object. |
||
650 | * |
||
651 | * @param string $primaryUri The storage account primary uri. |
||
652 | * @param string $secondaryUri The storage account secondary uri. |
||
653 | * @param IODataReaderWriter $odataSerializer The odata serializer. |
||
654 | * @param IMimeReaderWriter $mimeSerializer The MIME serializer. |
||
655 | * @param ISerializer $dataSerializer The data serializer. |
||
656 | * @param array $options Array of options to pass to |
||
657 | * the service |
||
658 | */ |
||
659 | public function __construct( |
||
677 | |||
678 | /** |
||
679 | * Quries tables in the given storage account. |
||
680 | * |
||
681 | * @param QueryTablesOptions|string|Filter $options Could be optional |
||
682 | * parameters, table prefix |
||
683 | * or filter to apply. |
||
684 | * |
||
685 | * @return QueryTablesResult |
||
686 | * |
||
687 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/query-tables |
||
688 | */ |
||
689 | public function queryTables($options = null) |
||
693 | |||
694 | /** |
||
695 | * Creates promise to query the tables in the given storage account. |
||
696 | * |
||
697 | * @param QueryTablesOptions|string|Filter $options Could be optional |
||
698 | * parameters, table prefix |
||
699 | * or filter to apply. |
||
700 | * |
||
701 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
702 | * |
||
703 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/query-tables |
||
704 | */ |
||
705 | public function queryTablesAsync($options = null) |
||
803 | |||
804 | /** |
||
805 | * Creates new table in the storage account |
||
806 | * |
||
807 | * @param string $table The name of the table. |
||
808 | * @param TableServiceCreateOptions $options The optional parameters. |
||
809 | * |
||
810 | * @return \Psr\Http\Message\ResponseInterface |
||
811 | * |
||
812 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/create-table |
||
813 | */ |
||
814 | public function createTable($table, TableServiceCreateOptions $options = null) |
||
818 | |||
819 | /** |
||
820 | * Creates promise to create new table in the storage account |
||
821 | * |
||
822 | * @param string $table The name of the table. |
||
823 | * @param TableServiceCreateOptions $options The optional parameters. |
||
824 | * |
||
825 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
826 | * |
||
827 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/create-table |
||
828 | */ |
||
829 | View Code Duplication | public function createTableAsync( |
|
876 | |||
877 | /** |
||
878 | * Gets the table. |
||
879 | * |
||
880 | * @param string $table The name of the table. |
||
881 | * @param GetTableOptions $options The optional parameters. |
||
882 | * |
||
883 | * @return GetTableResult |
||
884 | */ |
||
885 | public function getTable($table, GetTableOptions $options = null) |
||
889 | |||
890 | /** |
||
891 | * Creates the promise to get the table. |
||
892 | * |
||
893 | * @param string $table The name of the table. |
||
894 | * @param GetTableOptions $options The optional parameters. |
||
895 | * |
||
896 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
897 | */ |
||
898 | View Code Duplication | public function getTableAsync( |
|
941 | |||
942 | /** |
||
943 | * Deletes the specified table and any data it contains. |
||
944 | * |
||
945 | * @param string $table The name of the table. |
||
946 | * @param TableServiceOptions $options optional parameters |
||
947 | * |
||
948 | * @return void |
||
949 | * |
||
950 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179387.aspx |
||
951 | */ |
||
952 | public function deleteTable($table, TableServiceOptions$options = null) |
||
956 | |||
957 | /** |
||
958 | * Creates promise to delete the specified table and any data it contains. |
||
959 | * |
||
960 | * @param string $table The name of the table. |
||
961 | * @param TableServiceOptions $options optional parameters |
||
962 | * |
||
963 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
964 | * |
||
965 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179387.aspx |
||
966 | */ |
||
967 | View Code Duplication | public function deleteTableAsync( |
|
995 | |||
996 | /** |
||
997 | * Quries entities for the given table name |
||
998 | * |
||
999 | * @param string $table The name of |
||
1000 | * the table. |
||
1001 | * @param QueryEntitiesOptions|string|Filter $options Coule be optional |
||
1002 | * parameters, query |
||
1003 | * string or filter to |
||
1004 | * apply. |
||
1005 | * |
||
1006 | * @return QueryEntitiesResult |
||
1007 | * |
||
1008 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/query-entities |
||
1009 | */ |
||
1010 | public function queryEntities($table, $options = null) |
||
1014 | |||
1015 | /** |
||
1016 | * Quries entities for the given table name |
||
1017 | * |
||
1018 | * @param string $table The name of the table. |
||
1019 | * @param QueryEntitiesOptions|string|Filter $options Coule be optional |
||
1020 | * parameters, query |
||
1021 | * string or filter to |
||
1022 | * apply. |
||
1023 | * |
||
1024 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
1025 | * |
||
1026 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/query-entities |
||
1027 | */ |
||
1028 | public function queryEntitiesAsync($table, $options = null) |
||
1106 | |||
1107 | /** |
||
1108 | * Inserts new entity to the table. |
||
1109 | * |
||
1110 | * @param string $table name of the table. |
||
1111 | * @param Entity $entity table entity. |
||
1112 | * @param TableServiceCreateOptions $options optional parameters. |
||
1113 | * |
||
1114 | * @return InsertEntityResult |
||
1115 | * |
||
1116 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/insert-entity |
||
1117 | */ |
||
1118 | public function insertEntity( |
||
1125 | |||
1126 | /** |
||
1127 | * Inserts new entity to the table. |
||
1128 | * |
||
1129 | * @param string $table name of the table. |
||
1130 | * @param Entity $entity table entity. |
||
1131 | * @param TableServiceCreateOptions $options optional parameters. |
||
1132 | * |
||
1133 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
1134 | * |
||
1135 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/insert-entity |
||
1136 | */ |
||
1137 | public function insertEntityAsync( |
||
1163 | |||
1164 | /** |
||
1165 | * Updates an existing entity or inserts a new entity if it does not exist |
||
1166 | * in the table. |
||
1167 | * |
||
1168 | * @param string $table name of the table |
||
1169 | * @param Entity $entity table entity |
||
1170 | * @param TableServiceOptions $options optional parameters |
||
1171 | * |
||
1172 | * @return UpdateEntityResult |
||
1173 | * |
||
1174 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/hh452241.aspx |
||
1175 | */ |
||
1176 | public function insertOrMergeEntity( |
||
1183 | |||
1184 | /** |
||
1185 | * Creates promise to update an existing entity or inserts a new entity if |
||
1186 | * it does not exist in the table. |
||
1187 | * |
||
1188 | * @param string $table name of the table |
||
1189 | * @param Entity $entity table entity |
||
1190 | * @param TableServiceOptions $options optional parameters |
||
1191 | * |
||
1192 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
1193 | * |
||
1194 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/hh452241.aspx |
||
1195 | */ |
||
1196 | public function insertOrMergeEntityAsync( |
||
1209 | |||
1210 | /** |
||
1211 | * Replaces an existing entity or inserts a new entity if it does not exist in |
||
1212 | * the table. |
||
1213 | * |
||
1214 | * @param string $table name of the table |
||
1215 | * @param Entity $entity table entity |
||
1216 | * @param TableServiceOptions $options optional parameters |
||
1217 | * |
||
1218 | * @return UpdateEntityResult |
||
1219 | * |
||
1220 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/hh452242.aspx |
||
1221 | */ |
||
1222 | public function insertOrReplaceEntity( |
||
1233 | |||
1234 | /** |
||
1235 | * Creates a promise to replace an existing entity or inserts a new entity if it does not exist in the table. |
||
1236 | * |
||
1237 | * @param string $table name of the table |
||
1238 | * @param Entity $entity table entity |
||
1239 | * @param TableServiceOptions $options optional parameters |
||
1240 | * |
||
1241 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
1242 | * |
||
1243 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/hh452242.aspx |
||
1244 | */ |
||
1245 | public function insertOrReplaceEntityAsync( |
||
1258 | |||
1259 | /** |
||
1260 | * Updates an existing entity in a table. The Update Entity operation replaces |
||
1261 | * the entire entity and can be used to remove properties. |
||
1262 | * |
||
1263 | * @param string $table The table name. |
||
1264 | * @param Entity $entity The table entity. |
||
1265 | * @param TableServiceOptions $options The optional parameters. |
||
1266 | * |
||
1267 | * @return UpdateEntityResult |
||
1268 | * |
||
1269 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179427.aspx |
||
1270 | */ |
||
1271 | public function updateEntity( |
||
1278 | |||
1279 | /** |
||
1280 | * Creates promise to update an existing entity in a table. The Update Entity |
||
1281 | * operation replaces the entire entity and can be used to remove properties. |
||
1282 | * |
||
1283 | * @param string $table The table name. |
||
1284 | * @param Entity $entity The table entity. |
||
1285 | * @param TableServiceOptions $options The optional parameters. |
||
1286 | * |
||
1287 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
1288 | * |
||
1289 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179427.aspx |
||
1290 | */ |
||
1291 | public function updateEntityAsync( |
||
1304 | |||
1305 | /** |
||
1306 | * Updates an existing entity by updating the entity's properties. This operation |
||
1307 | * does not replace the existing entity, as the updateEntity operation does. |
||
1308 | * |
||
1309 | * @param string $table The table name. |
||
1310 | * @param Entity $entity The table entity. |
||
1311 | * @param TableServiceOptions $options The optional parameters. |
||
1312 | * |
||
1313 | * @return Models\UpdateEntityResult |
||
1314 | * |
||
1315 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179392.aspx |
||
1316 | */ |
||
1317 | public function mergeEntity( |
||
1324 | |||
1325 | /** |
||
1326 | * Creates promise to update an existing entity by updating the entity's |
||
1327 | * properties. This operation does not replace the existing entity, as the |
||
1328 | * updateEntity operation does. |
||
1329 | * |
||
1330 | * @param string $table The table name. |
||
1331 | * @param Entity $entity The table entity. |
||
1332 | * @param TableServiceOptions $options The optional parameters. |
||
1333 | * |
||
1334 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
1335 | * |
||
1336 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179392.aspx |
||
1337 | */ |
||
1338 | public function mergeEntityAsync( |
||
1351 | |||
1352 | /** |
||
1353 | * Deletes an existing entity in a table. |
||
1354 | * |
||
1355 | * @param string $table The name of the table. |
||
1356 | * @param string $partitionKey The entity partition key. |
||
1357 | * @param string $rowKey The entity row key. |
||
1358 | * @param DeleteEntityOptions $options The optional parameters. |
||
1359 | * |
||
1360 | * @return void |
||
1361 | * |
||
1362 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd135727.aspx |
||
1363 | */ |
||
1364 | public function deleteEntity( |
||
1372 | |||
1373 | /** |
||
1374 | * Creates promise to delete an existing entity in a table. |
||
1375 | * |
||
1376 | * @param string $table The name of the table. |
||
1377 | * @param string $partitionKey The entity partition key. |
||
1378 | * @param string $rowKey The entity row key. |
||
1379 | * @param DeleteEntityOptions $options The optional parameters. |
||
1380 | * |
||
1381 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
1382 | * |
||
1383 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd135727.aspx |
||
1384 | */ |
||
1385 | public function deleteEntityAsync( |
||
1400 | |||
1401 | /** |
||
1402 | * Gets table entity. |
||
1403 | * |
||
1404 | * @param string $table The name of the table. |
||
1405 | * @param string $partitionKey The entity partition key. |
||
1406 | * @param string $rowKey The entity row key. |
||
1407 | * @param GetEntityOptions|null $options The optional parameters. |
||
1408 | * |
||
1409 | * @return GetEntityResult |
||
1410 | * |
||
1411 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179421.aspx |
||
1412 | */ |
||
1413 | public function getEntity( |
||
1426 | |||
1427 | /** |
||
1428 | * Creates promise to get table entity. |
||
1429 | * |
||
1430 | * @param string $table The name of the table. |
||
1431 | * @param string $partitionKey The entity partition key. |
||
1432 | * @param string $rowKey The entity row key. |
||
1433 | * @param GetEntityOptions|null $options The optional parameters. |
||
1434 | * |
||
1435 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
1436 | * |
||
1437 | * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179421.aspx |
||
1438 | */ |
||
1439 | public function getEntityAsync( |
||
1491 | |||
1492 | /** |
||
1493 | * Does batch of operations on the table service. |
||
1494 | * |
||
1495 | * @param BatchOperations $batchOperations The operations to apply. |
||
1496 | * @param TableServiceOptions $options The optional parameters. |
||
1497 | * |
||
1498 | * @return BatchResult |
||
1499 | */ |
||
1500 | public function batch( |
||
1506 | |||
1507 | /** |
||
1508 | * Creates promise that does batch of operations on the table service. |
||
1509 | * |
||
1510 | * @param BatchOperations $batchOperations The operations to apply. |
||
1511 | * @param TableServiceOptions $options The optional parameters. |
||
1512 | * |
||
1513 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
1514 | */ |
||
1515 | public function batchAsync( |
||
1570 | |||
1571 | /** |
||
1572 | * Gets the access control list (ACL) |
||
1573 | * |
||
1574 | * @param string $table The table name. |
||
1575 | * @param TableServiceOptions $options The optional parameters. |
||
1576 | * |
||
1577 | * @return TableACL |
||
1578 | * |
||
1579 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/get-table-acl |
||
1580 | */ |
||
1581 | public function getTableAcl( |
||
1587 | |||
1588 | /** |
||
1589 | * Creates the promise to gets the access control list (ACL) |
||
1590 | * |
||
1591 | * @param string $table The table name. |
||
1592 | * @param TableServiceOptions $options The optional parameters. |
||
1593 | * |
||
1594 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
1595 | * |
||
1596 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/get-table-acl |
||
1597 | */ |
||
1598 | View Code Duplication | public function getTableAclAsync( |
|
1639 | |||
1640 | /** |
||
1641 | * Sets the ACL. |
||
1642 | * |
||
1643 | * @param string $table name |
||
1644 | * @param TableACL $acl access control list for Table |
||
1645 | * @param TableServiceOptions $options optional parameters |
||
1646 | * |
||
1647 | * @return void |
||
1648 | * |
||
1649 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/set-table-acl |
||
1650 | */ |
||
1651 | public function setTableAcl( |
||
1658 | |||
1659 | /** |
||
1660 | * Creates promise to set the ACL |
||
1661 | * |
||
1662 | * @param string $table name |
||
1663 | * @param TableACL $acl access control list for Table |
||
1664 | * @param TableServiceOptions $options optional parameters |
||
1665 | * |
||
1666 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
1667 | * |
||
1668 | * @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/set-table-acl |
||
1669 | */ |
||
1670 | View Code Duplication | public function setTableAclAsync( |
|
1708 | } |
||
1709 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.