Conditions | 6 |
Paths | 10 |
Total Lines | 70 |
Lines | 0 |
Ratio | 0 % |
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:
1 | <?php |
||
95 | public function process() |
||
96 | { |
||
97 | // construct limit statement if query_limit is valid int value |
||
98 | $limit = ''; |
||
99 | $query_limit = $this->config()->get('query_limit'); |
||
100 | if (is_numeric($query_limit) && $query_limit >= 0) { |
||
101 | $limit = ' LIMIT ' . ((int)$query_limit); |
||
102 | } |
||
103 | |||
104 | $statusList = implode('\', \'', $this->config()->get('cleanup_statuses')); |
||
105 | switch ($this->config()->get('cleanup_method')) { |
||
106 | // If Age, we need to get jobs that are at least n days old |
||
107 | case "age": |
||
108 | $cutOff = date( |
||
109 | "Y-m-d H:i:s", |
||
110 | strtotime(DBDatetime::now() . |
||
111 | " - " . |
||
112 | $this->config()->cleanup_value . |
||
113 | " days") |
||
114 | ); |
||
115 | $stale = DB::query( |
||
116 | 'SELECT "ID" |
||
117 | FROM "QueuedJobDescriptor" |
||
118 | WHERE "JobStatus" |
||
119 | IN (\'' . $statusList . '\') |
||
120 | AND "LastEdited" < \'' . $cutOff . '\'' . $limit |
||
121 | ); |
||
122 | $staleJobs = $stale->column("ID"); |
||
123 | break; |
||
124 | // If Number, we need to save n records, then delete from the rest |
||
125 | case "number": |
||
126 | $fresh = DB::query( |
||
127 | 'SELECT "ID" |
||
128 | FROM "QueuedJobDescriptor" |
||
129 | ORDER BY "LastEdited" |
||
130 | ASC LIMIT ' . $this->config()->cleanup_value |
||
131 | ); |
||
132 | $freshJobIDs = implode('\', \'', $fresh->column("ID")); |
||
133 | |||
134 | $stale = DB::query( |
||
135 | 'SELECT "ID" |
||
136 | FROM "QueuedJobDescriptor" |
||
137 | WHERE "ID" |
||
138 | NOT IN (\'' . $freshJobIDs . '\') |
||
139 | AND "JobStatus" |
||
140 | IN (\'' . $statusList . '\')' . $limit |
||
141 | ); |
||
142 | $staleJobs = $stale->column("ID"); |
||
143 | break; |
||
144 | default: |
||
145 | $this->addMessage("Incorrect configuration values set. Cleanup ignored"); |
||
146 | $this->isComplete = true; |
||
147 | return; |
||
148 | } |
||
149 | if (empty($staleJobs)) { |
||
150 | $this->addMessage("No jobs to clean up."); |
||
151 | $this->isComplete = true; |
||
152 | $this->reenqueue(); |
||
153 | return; |
||
154 | } |
||
155 | $numJobs = count($staleJobs); |
||
156 | $staleJobs = implode('\', \'', $staleJobs); |
||
157 | DB::query('DELETE FROM "QueuedJobDescriptor" |
||
158 | WHERE "ID" |
||
159 | IN (\'' . $staleJobs . '\')'); |
||
160 | $this->addMessage($numJobs . " jobs cleaned up."); |
||
161 | // let's make sure there is a cleanupJob in the queue |
||
162 | $this->reenqueue(); |
||
163 | $this->isComplete = true; |
||
164 | } |
||
165 | |||
178 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.