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 |
||
27 | class ExpressionGuard |
||
28 | { |
||
29 | /** |
||
30 | * Expression language |
||
31 | * |
||
32 | * @var ExpressionLanguage |
||
33 | */ |
||
34 | private $language; |
||
35 | |||
36 | /** |
||
37 | * Subject manipulator |
||
38 | * |
||
39 | * @var SubjectManipulator |
||
40 | */ |
||
41 | private $subjectManipulator; |
||
42 | |||
43 | /** |
||
44 | * Workflow registry |
||
45 | * |
||
46 | * @var WorkflowRegistry |
||
47 | */ |
||
48 | private $workflowRegistry; |
||
49 | |||
50 | /** |
||
51 | * Logger |
||
52 | * |
||
53 | * @var LoggerInterface |
||
54 | */ |
||
55 | private $logger; |
||
56 | |||
57 | /** |
||
58 | * Maps guard event name to expression and workflow used to block or allow transition |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | private $supportedEventsConfig = []; |
||
63 | |||
64 | /** |
||
65 | * ExpressionGuard constructor |
||
66 | * |
||
67 | * @param ExpressionLanguage $language expression language |
||
68 | * @param SubjectManipulator $subjectManipulator subject manipulator |
||
69 | * @param WorkflowRegistry $workflowRegistry workflow registry |
||
70 | * @param LoggerInterface $logger logger |
||
71 | */ |
||
72 | View Code Duplication | public function __construct( |
|
83 | |||
84 | /** |
||
85 | * Registers guard expression |
||
86 | * |
||
87 | * @param string $eventName guard event name |
||
88 | * @param string $workflowName workflow name |
||
89 | * @param string $expression guard expression |
||
90 | */ |
||
91 | public function registerGuardExpression($eventName, $workflowName, $expression) |
||
95 | |||
96 | /** |
||
97 | * Blocks or allows workflow transitions by guard expression evaluation |
||
98 | * |
||
99 | * @param GuardEvent $event |
||
100 | * @param string $eventName |
||
101 | * |
||
102 | * @throws \Exception in case of failure |
||
103 | */ |
||
104 | public function guardTransition(GuardEvent $event, $eventName) |
||
170 | } |
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.