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 |
||
36 | class TaskController extends MvcController |
||
37 | { |
||
38 | /** |
||
39 | * Tasks Provider |
||
40 | * @var TaskProviderInterface $provider |
||
41 | */ |
||
42 | protected $provider; |
||
43 | |||
44 | /** |
||
45 | * Constructor for Admin Config controller |
||
46 | * @param AbstractModule $module |
||
47 | */ |
||
48 | public function __construct(AbstractModule $module) { |
||
49 | parent::__construct($module); |
||
50 | |||
51 | $this->provider = $this->module->getProvider(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Pages |
||
56 | */ |
||
57 | |||
58 | /** |
||
59 | * Task@trigger |
||
60 | */ |
||
61 | public function trigger() |
||
62 | { |
||
63 | $controller = new AjaxController(); |
||
64 | $controller->pageHeader(); |
||
65 | |||
66 | $task_name = Filter::get('task'); |
||
67 | $token_submitted = Filter::get('force'); |
||
68 | $token = $this->module->getSetting('MAJ_AT_FORCE_EXEC_TOKEN'); |
||
69 | |||
70 | $tasks = $this->provider->getTasksToRun($token == $token_submitted, $task_name); |
||
|
|||
71 | |||
72 | foreach($tasks as $task) { |
||
73 | $task->execute(); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Task@setStatus |
||
79 | */ |
||
80 | View Code Duplication | public function setStatus() { |
|
81 | $controller = new JsonController(); |
||
82 | |||
83 | $task_name = Filter::get('task'); |
||
84 | $task = $this->provider->getTask($task_name, false); |
||
85 | |||
86 | $controller->restrictAccess( |
||
87 | true // Filter::checkCsrf() -- Cannot use CSRF on a GET request (modules can only work with GET requests) |
||
88 | && Auth::isAdmin() |
||
89 | && $task |
||
90 | ); |
||
91 | |||
92 | $status = Filter::getBool('status'); |
||
93 | $res = array('task' => $task->getName() , 'error' => null); |
||
94 | try{ |
||
95 | $this->provider->setTaskStatus($task, $status); |
||
96 | $res['status'] = $status; |
||
97 | Log::addConfigurationLog('Module '.$this->module->getName().' : Admin Task "'.$task->getName().'" has been '. ($status ? 'enabled' : 'disabled') .'.'); |
||
98 | } |
||
99 | catch (\Exception $ex) { |
||
100 | $res['error'] = $ex->getMessage(); |
||
101 | Log::addErrorLog('Module '.$this->module->getName().' : Admin Task "'.$task->getName().'" could not be ' . ($status ? 'enabled' : 'disabled') .'. Error: '. $ex->getMessage()); |
||
102 | } |
||
103 | |||
104 | $controller->pageHeader(); |
||
105 | if($res['error']) http_response_code(500); |
||
106 | |||
107 | $controller->encode($res); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Task@edit |
||
112 | */ |
||
113 | public function edit() { |
||
114 | $tree = Globals::getTree(); |
||
115 | $task_name = Filter::get('task'); |
||
116 | $task = $this->provider->getTask($task_name, false); |
||
117 | |||
118 | Theme::theme(new AdministrationTheme)->init($tree); |
||
119 | $controller = new PageController(); |
||
120 | $controller |
||
121 | ->restrictAccess(Auth::isAdmin() && $task) |
||
122 | ->setPageTitle(I18N::translate('Edit the administrative task')) |
||
123 | ->addInlineJavascript(' |
||
124 | function toggleRemainingOccurrences() { |
||
125 | if($("input:radio[name=\'is_limited\']:checked").val() == 1) { |
||
126 | $("#nb_occurences").show(); |
||
127 | } |
||
128 | else { |
||
129 | $("#nb_occurences").hide(); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | $("[name=\'is_limited\']").on("change", toggleRemainingOccurrences); |
||
134 | toggleRemainingOccurrences(); |
||
135 | ') |
||
136 | ; |
||
137 | |||
138 | |||
139 | $data = new ViewBag(); |
||
140 | $data->set('title', $controller->getPageTitle()); |
||
141 | $data->set('admin_config_url', 'module.php?mod=' . $this->module->getName() . '&mod_action=AdminConfig&ged=' . $tree->getNameUrl()); |
||
142 | $data->set('module_title', $this->module->getTitle()); |
||
143 | $data->set('save_url', 'module.php?mod=' . $this->module->getName() . '&mod_action=Task@save&ged=' . $tree->getNameUrl()); |
||
144 | $data->set('task', $task); |
||
145 | |||
146 | ViewFactory::make('TaskEdit', $this, $controller, $data)->render(); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Task@save |
||
151 | */ |
||
152 | public function save() { |
||
208 | |||
209 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: