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 |
||
20 | class ApiController extends FOSRestController |
||
21 | { |
||
22 | /** |
||
23 | * @Route("/api/paywall/pricelist/{currency}/{locale}.{_format}", defaults={"_format"="json"}, name="newscoop_gimme_paywall_pricelist") |
||
24 | * |
||
25 | * @Method("GET") |
||
26 | * @View() |
||
27 | */ |
||
28 | public function listAction(Request $request, $currency, $locale = null) |
||
29 | { |
||
30 | $criteria = new SubscriptionCriteria(); |
||
31 | $criteria->locale = $locale; |
||
32 | $paywallService = $this->get('paywall.subscription.service'); |
||
33 | $currencyContext = $this->get('newscoop_paywall.currency_context'); |
||
34 | $paginator = $this->get('newscoop.paginator.paginator_service'); |
||
35 | $criteria->type = $request->query->get('type'); |
||
36 | $list = $paywallService->getSubscriptionsByCriteria($criteria); |
||
37 | $paginator->setUsedRouteParams(array('currency' => $currency)); |
||
38 | $currencyContext->setCurrency($currency); |
||
39 | |||
40 | $priceList = $paginator->paginate( |
||
41 | $list->items, |
||
42 | array( |
||
43 | 'distinct' => false, |
||
44 | ) |
||
45 | ); |
||
46 | |||
47 | return $priceList; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @Route("/api/paywall/my-subscriptions/{locale}.{_format}", defaults={"_format"="json"}, name="newscoop_gimme_paywall_my") |
||
52 | * |
||
53 | * @Method("GET") |
||
54 | * @View() |
||
55 | */ |
||
56 | public function myAction(Request $request, $locale = null) |
||
77 | |||
78 | /** |
||
79 | * @Route("/api/paywall/discounts.{_format}", defaults={"_format"="json"}, name="newscoop_gimme_paywall_discount") |
||
80 | * |
||
81 | * @Method("GET") |
||
82 | * @View() |
||
83 | */ |
||
84 | View Code Duplication | public function discountAction(Request $request) |
|
100 | |||
101 | /** |
||
102 | * @Route("/api/paywall/currencies.{_format}", defaults={"_format"="json"}, name="newscoop_gimme_paywall_currency") |
||
103 | * |
||
104 | * @Method("GET") |
||
105 | * @View() |
||
106 | */ |
||
107 | View Code Duplication | public function currencyAction(Request $request) |
|
122 | |||
123 | /** |
||
124 | * @Route("/api/paywall/gateways.{_format}", defaults={"_format"="json"}, name="newscoop_gimme_paywall_gateways") |
||
125 | * |
||
126 | * @Method("GET") |
||
127 | * @View() |
||
128 | */ |
||
129 | View Code Duplication | public function gatewaysAction(Request $request) |
|
145 | } |
||
146 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.