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:
| 1 | <?php |
||
| 6 | class SalesReport extends SS_Report |
||
|
|
|||
| 7 | { |
||
| 8 | protected static $full_export_select_statement = |
||
| 9 | array( |
||
| 10 | "`Order`.`ID`" => "Order number", |
||
| 11 | "`Order`.`Created`" => "Order date and time", |
||
| 12 | "GROUP_CONCAT(`Payment`.`Message` SEPARATOR ', ')" => "payment gateway reference", |
||
| 13 | "GROUP_CONCAT(`Payment`.`ID` SEPARATOR ', ')" => "Transaction Id", |
||
| 14 | "SUM(IF(Payment.Status = 'Success',`Payment`.`Amount`, 0)) RealPayments" => "Total Order Amount", |
||
| 15 | "IF(ProductVariationsForVariations.Title IS NOT NULL, CONCAT(ProductSiteTreeForVariations.Title,' : ', ProductVariationsForVariations.Title), IF(SiteTreeForProducts.Title IS NOT NULL, SiteTreeForProducts.Title, OrderAttribute.ClassName)) ProductOrModifierName" => "Product Name", |
||
| 16 | "IF(OrderItem.Quantity IS NOT NULL, OrderItem.Quantity, 1) ProductOrModifierQuantity" => "Quantity", |
||
| 17 | "IF(ProductSiteTreeForVariations.ID IS NOT NULL, ProductVariationsForVariations.Price, 0) + IF(ProductForProducts.Price IS NOT NULL, ProductForProducts.Price, 0) + IF(OrderModifier.Amount IS NOT NULL, OrderModifier.Amount, 0) ProductOrModifierPrice" => "Amount", |
||
| 18 | "IF(ProductForProducts.Price IS NOT NULL, ProductForProducts.Price, 0) ProductOrModifierPriceProduct" => "Product Amount", |
||
| 19 | "IF(ProductSiteTreeForVariations.ID IS NOT NULL, ProductVariationsForVariations.Price, 0) ProductOrModifierPriceVariation" => "Variation Amount", |
||
| 20 | "IF(OrderModifier.Amount IS NOT NULL, OrderModifier.Amount, 0) ProductOrModifierPriceModifier" => "Modifier Amount", |
||
| 21 | "CONCAT(Member.Address, ' ', Member.AddressLine2,' ', Member.City, ' ', Member.Country,' ', Member.HomePhone,' ', Member.MobilePhone,' ', Member.Notes,' ', Member.Notes ) MemberContactDetails" => "Customer contact details", |
||
| 22 | "IF(`Order`.`UseShippingAddress`, CONCAT(`Order`.`ShippingName`, ' ',`Order`.`ShippingAddress`, ' ',`Order`.`ShippingAddress2`, ' ',`Order`.`ShippingCity`, ' ',`Order`.`ShippingCountry`), 'no alternative delivery address') MemberShippingDetailsAddress" => "Costumer delivery", |
||
| 23 | "`Order`.`Status`" => "Order status", |
||
| 24 | "GROUP_CONCAT(`OrderStatusLogWithDetails`.`DispatchTicket` SEPARATOR ', ')" => "Dispatch ticket code", |
||
| 25 | "GROUP_CONCAT(`OrderStatusLogWithDetails`.`DispatchedOn` SEPARATOR ', ')" => "Dispatch date", |
||
| 26 | "GROUP_CONCAT(`OrderStatusLogWithDetails`.`DispatchedBy` SEPARATOR ', ')" => "Dispatched by", |
||
| 27 | "GROUP_CONCAT(`OrderStatusLog`.`Note` SEPARATOR ', ')" => "Dispatch notes" |
||
| 28 | ); |
||
| 29 | public static function set_full_export_select_statement($v) |
||
| 42 | |||
| 43 | protected static $full_export_join_statement = ' |
||
| 44 | INNER JOIN `Order` ON `Order`.`ID` = `OrderAttribute`.`OrderID` |
||
| 45 | INNER JOIN `Member` On `Order`.`MemberID` = `Member`.`ID` |
||
| 46 | LEFT JOIN `Payment` ON `Payment`.`OrderID` = `Order`.`ID` |
||
| 47 | LEFT JOIN `Product_versions` ProductForProducts ON `Product_OrderItem`.`ProductID` = ProductForProducts.`RecordID` AND `Product_OrderItem`.`ProductVersion` = ProductForProducts.`Version` |
||
| 48 | LEFT JOIN `SiteTree_versions` SiteTreeForProducts ON SiteTreeForProducts.`RecordID` = `Product_OrderItem`.`ProductID` AND `Product_OrderItem`.`ProductVersion` = SiteTreeForProducts.`Version` |
||
| 49 | LEFT JOIN `ProductVariation_versions` ProductVariationsForVariations ON `ProductVariation_OrderItem`.`ProductVariationID` = ProductVariationsForVariations.`RecordID` AND `ProductVariation_OrderItem`.`ProductVariationVersion` = ProductVariationsForVariations.`Version` |
||
| 50 | LEFT JOIN `SiteTree_versions` ProductSiteTreeForVariations ON `ProductSiteTreeForVariations`.`RecordID` = `Product_OrderItem`.`ProductID` AND `Product_OrderItem`.`ProductVersion` = `ProductSiteTreeForVariations`.`Version` |
||
| 51 | LEFT JOIN `OrderStatusLog` ON `OrderStatusLog`.`OrderID` = `Order`.`ID` |
||
| 52 | LEFT JOIN `OrderStatusLogWithDetails` ON `OrderStatusLogWithDetails`.`ID` = `OrderStatusLog`.`ID`'; |
||
| 53 | public static function set_full_export_join_statement($v) |
||
| 66 | |||
| 67 | protected static $full_export_file_name = "SalesExport"; |
||
| 68 | public static function set_full_export_file_name($v) |
||
| 81 | |||
| 82 | protected $title = 'All Orders'; |
||
| 83 | |||
| 84 | protected static $sales_array = array(); |
||
| 85 | |||
| 86 | protected $description = 'Search Orders'; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Return a {@link ComplexTableField} that shows |
||
| 90 | * all Order instances that are not printed. That is, |
||
| 91 | * Order instances with the property "Printed" value |
||
| 92 | * set to "0". |
||
| 93 | * |
||
| 94 | * @return ComplexTableField |
||
| 95 | */ |
||
| 96 | public function getReportField() |
||
| 148 | |||
| 149 | View Code Duplication | public function getCustomQuery() |
|
| 160 | |||
| 161 | public function getExportFields() |
||
| 168 | |||
| 169 | View Code Duplication | public function getExportQuery() |
|
| 179 | |||
| 180 | View Code Duplication | protected function statistic($type) |
|
| 222 | |||
| 223 | View Code Duplication | public function processform() |
|
| 231 | } |
||
| 232 | |||
| 306 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.