Conditions | 52 |
Paths | 192 |
Total Lines | 136 |
Code Lines | 82 |
Lines | 62 |
Ratio | 45.59 % |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
43 | public function workflow($fromaddress, $fromname, $subject, $body, $phone, $phonecode, $mobile_number, $helptopic, $sla, $priority, $source, $collaborator, $dept, $assign, $team_assign, $ticket_status, $form_data, $auto_response) |
||
44 | { |
||
45 | $contact_details = ['email' => $fromaddress, 'email_name' => $fromname, 'subject' => $subject, 'message' => $body]; |
||
46 | $ticket_settings_details = ['help_topic' => $helptopic, 'sla' => $sla, 'priority' => $priority, 'source' => $source, 'dept' => $dept, 'assign' => $assign, 'team' => $team_assign, 'status' => $ticket_status, 'reject' => false]; |
||
47 | // get all the workflow common to the entire system which includes any type of ticket creation where the execution order of the workflow should be starting with ascending order |
||
48 | $workflows = WorkflowName::where('target', '=', 'A-0')->where('status', '=', '1')->orderBy('order', 'asc')->get(); |
||
49 | foreach ($workflows as $workflow) { |
||
50 | // checking if any workflow defined in the system |
||
51 | if ($workflow) { |
||
52 | // get all the rules of workflow which has a foreign key of those workflow which are applied to creating any ticket from any source |
||
53 | $worklfow_rules = WorkflowRules::where('workflow_id', '=', $workflow->id)->get(); |
||
54 | foreach ($worklfow_rules as $worklfow_rule) { |
||
55 | // checking for the workflow rules to which workflow rule type it is |
||
56 | if ($worklfow_rule->matching_scenario == 'email') { |
||
57 | if ($rule_condition = $this->checkRuleCondition($contact_details['email'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
58 | $ticket_settings_details = $this->applyActionCondition($workflow->id, $ticket_settings_details); |
||
59 | } |
||
60 | } elseif ($worklfow_rule->matching_scenario == 'email_name') { |
||
61 | if ($rule_condition = $this->checkRuleCondition($contact_details['email_name'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
62 | $ticket_settings_details = $this->applyActionCondition($workflow->id, $ticket_settings_details); |
||
63 | } |
||
64 | } elseif ($worklfow_rule->matching_scenario == 'subject') { |
||
65 | if ($rule_condition = $this->checkRuleCondition($contact_details['subject'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
66 | $ticket_settings_details = $this->applyActionCondition($workflow->id, $ticket_settings_details); |
||
67 | } |
||
68 | } elseif ($worklfow_rule->matching_scenario == 'message') { |
||
69 | if ($rule_condition = $this->checkRuleCondition($contact_details['message'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
70 | $ticket_settings_details = $this->applyActionCondition($workflow->id, $ticket_settings_details); |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | if ($source == 1) { |
||
77 | // get all the workflow which are applied to ticket generated via webforms and in ascending order |
||
78 | $workflows_webs = WorkflowName::where('target', '=', 'A-1')->where('status', '=', '1')->orderBy('order', 'asc')->get(); |
||
79 | foreach ($workflows_webs as $workflows_web) { |
||
80 | if ($workflows_web) { |
||
81 | $worklfow_rules = WorkflowRules::where('workflow_id', '=', $workflows_web->id)->get(); |
||
82 | foreach ($worklfow_rules as $worklfow_rule) { |
||
83 | if ($worklfow_rule) { |
||
84 | // checking for the workflow rules to which workflow rule type it is |
||
85 | if ($worklfow_rule->matching_scenario == 'email') { |
||
86 | if ($this->checkRuleCondition($contact_details['email'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
87 | $ticket_settings_details = $this->applyActionCondition($workflows_web->id, $ticket_settings_details); |
||
88 | } |
||
89 | } elseif ($worklfow_rule->matching_scenario == 'email_name') { |
||
90 | if ($this->checkRuleCondition($contact_details['email_name'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
91 | $ticket_settings_details = $this->applyActionCondition($workflows_web->id, $ticket_settings_details); |
||
92 | } |
||
93 | } elseif ($worklfow_rule->matching_scenario == 'subject') { |
||
94 | if ($this->checkRuleCondition($contact_details['subject'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
95 | $ticket_settings_details = $this->applyActionCondition($workflows_web->id, $ticket_settings_details); |
||
96 | } |
||
97 | } elseif ($worklfow_rule->matching_scenario == 'message') { |
||
98 | if ($this->checkRuleCondition($contact_details['message'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
99 | $ticket_settings_details = $this->applyActionCondition($workflows_web->id, $ticket_settings_details); |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | View Code Duplication | if ($source == 2) { |
|
108 | // get all the workflow which are applied to ticket generated via emails and in ascending order |
||
109 | $workflows_emails = WorkflowName::where('target', '=', 'A-2')->where('status', '=', '1')->orderBy('order', 'asc')->get(); |
||
110 | foreach ($workflows_emails as $workflows_email) { |
||
111 | if ($workflows_email) { |
||
112 | $worklfow_rules = WorkflowRules::where('workflow_id', '=', $workflows_email->id)->get(); |
||
113 | foreach ($worklfow_rules as $worklfow_rule) { |
||
114 | if ($worklfow_rule) { |
||
115 | // checking for the workflow rules to which workflow rule type it is |
||
116 | if ($worklfow_rule->matching_scenario == 'email') { |
||
117 | if ($rule_condition = $this->checkRuleCondition($contact_details['email'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
118 | $ticket_settings_details = $this->applyActionCondition($workflows_email->id, $ticket_settings_details); |
||
119 | } |
||
120 | } elseif ($worklfow_rule->matching_scenario == 'email_name') { |
||
121 | if ($rule_condition = $this->checkRuleCondition($contact_details['email_name'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
122 | $ticket_settings_details = $this->applyActionCondition($workflows_email->id, $ticket_settings_details); |
||
123 | } |
||
124 | } elseif ($worklfow_rule->matching_scenario == 'subject') { |
||
125 | if ($rule_condition = $this->checkRuleCondition($contact_details['subject'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
126 | $ticket_settings_details = $this->applyActionCondition($workflows_email->id, $ticket_settings_details); |
||
127 | } |
||
128 | } elseif ($worklfow_rule->matching_scenario == 'message') { |
||
129 | if ($rule_condition = $this->checkRuleCondition($contact_details['message'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
130 | $ticket_settings_details = $this->applyActionCondition($workflows_email->id, $ticket_settings_details); |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | View Code Duplication | if ($source == 4) { |
|
139 | // get all the workflow which are applied to ticket generated via API and in ascending order |
||
140 | $workflows_apis = WorkflowName::where('target', '=', 'A-4')->where('status', '=', '1')->orderBy('order', 'asc')->get(); |
||
141 | foreach ($workflows_apis as $workflows_api) { |
||
142 | if ($workflows_api) { |
||
143 | $worklfow_rules = WorkflowRules::where('workflow_id', '=', $workflows_api->id)->get(); |
||
144 | foreach ($worklfow_rules as $worklfow_rule) { |
||
145 | if ($worklfow_rule) { |
||
146 | // checking for the workflow rules to which workflow rule type it is |
||
147 | if ($worklfow_rule->matching_scenario == 'email') { |
||
148 | if ($rule_condition = $this->checkRuleCondition($contact_details['email'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
149 | $ticket_settings_details = $this->applyActionCondition($workflows_api->id, $ticket_settings_details); |
||
150 | } |
||
151 | } elseif ($worklfow_rule->matching_scenario == 'email_name') { |
||
152 | if ($rule_condition = $this->checkRuleCondition($contact_details['email_name'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
153 | $ticket_settings_details = $this->applyActionCondition($workflows_api->id, $ticket_settings_details); |
||
154 | } |
||
155 | } elseif ($worklfow_rule->matching_scenario == 'subject') { |
||
156 | if ($rule_condition = $this->checkRuleCondition($contact_details['subject'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
157 | $ticket_settings_details = $this->applyActionCondition($workflows_api->id, $ticket_settings_details); |
||
158 | } |
||
159 | } elseif ($worklfow_rule->matching_scenario == 'message') { |
||
160 | if ($rule_condition = $this->checkRuleCondition($contact_details['message'], $worklfow_rule->matching_relation, $worklfow_rule->matching_value) == true) { |
||
161 | $ticket_settings_details = $this->applyActionCondition($workflows_api->id, $ticket_settings_details); |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | |||
170 | //dd($form_data); |
||
171 | if ($ticket_settings_details['reject'] == true) { |
||
172 | return ['0' => false, '1' => false]; |
||
173 | } else { |
||
174 | $create_ticket = $this->TicketController->create_user($contact_details['email'], $contact_details['email_name'], $contact_details['subject'], $contact_details['message'], $phone, $phonecode, $mobile_number, $ticket_settings_details['help_topic'], $ticket_settings_details['sla'], $ticket_settings_details['priority'], $source, $collaborator, $ticket_settings_details['dept'], $ticket_settings_details['assign'], $form_data, $auto_response, $ticket_settings_details['status']); |
||
175 | |||
176 | return $create_ticket; |
||
177 | } |
||
178 | } |
||
179 | |||
513 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: