Conditions | 11 |
Paths | 20 |
Total Lines | 91 |
Code Lines | 50 |
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 |
||
94 | public function fromNetwork() |
||
95 | { |
||
96 | $dest_path = 'harvests/' . $this->name . '/'; |
||
97 | |||
98 | Storage::disk('local')->deleteDir($dest_path); |
||
99 | |||
100 | $latest = $dest_path . 'latest.xml'; |
||
101 | |||
102 | $client = new OaiPmhClient($this->url, [ |
||
103 | 'schema' => $this->schema, |
||
104 | 'user-agent' => 'Colligator/0.1', |
||
105 | 'max-retries' => $this->maxRetries, |
||
106 | 'sleep-time-on-error' => $this->sleepTimeOnError, |
||
107 | ]); |
||
108 | |||
109 | $client->on('request.error', function ($msg) { |
||
110 | $this->error($msg); |
||
111 | }); |
||
112 | |||
113 | // Store each response to disk just in case |
||
114 | $client->on('request.complete', function ($verb, $args, $body) use ($latest) { |
||
115 | Storage::disk('local')->put($latest, $body); |
||
116 | }); |
||
117 | |||
118 | $recordsHarvested = 0; |
||
119 | |||
120 | // Loop over all records using an iterator that pulls in more data when |
||
121 | // the buffer is exhausted. |
||
122 | $records = $client->records($this->start, $this->until, $this->set, $this->resume); |
||
123 | while (true) { |
||
124 | |||
125 | // If no records included in the last response |
||
126 | if (!$records->valid()) { |
||
127 | break 1; |
||
128 | } |
||
129 | |||
130 | $record = $records->current(); |
||
131 | ++$recordsHarvested; |
||
132 | |||
133 | // In case of a crash, it can be useful to have the resumption_token, |
||
134 | // but delete it when the harvest is complete |
||
135 | if ($this->resume != $records->getResumptionToken()) { |
||
136 | $this->resume = $records->getResumptionToken(); |
||
137 | if (is_null($this->resume)) { |
||
138 | Storage::disk('local')->delete($dest_path . '/resumption_token'); |
||
139 | } else { |
||
140 | Storage::disk('local')->put($dest_path . '/resumption_token', $this->resume); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | // Note that Bibsys doesn't start counting on 0, as given in the spec, |
||
145 | // but it doesn't really matter since we're only interested in a |
||
146 | // fixed order. |
||
147 | $currentIndex = $records->key(); |
||
148 | |||
149 | // Move to stable location |
||
150 | $destPath = sprintf('%s/response_%08d.xml', $dest_path, $currentIndex); |
||
151 | if (Storage::disk('local')->exists($latest)) { |
||
152 | Storage::disk('local')->move($latest, $destPath); |
||
153 | } |
||
154 | |||
155 | $this->dispatch(new ImportRecord($this->collection, $record->data)); |
||
156 | |||
157 | if ($recordsHarvested % $this->statusUpdateEvery == 0) { |
||
158 | if (is_null($this->start)) { |
||
159 | Event::fire(new OaiPmhHarvestStatus($recordsHarvested, $recordsHarvested)); |
||
160 | } else { |
||
161 | Event::fire(new OaiPmhHarvestStatus($recordsHarvested, $currentIndex)); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | $attempt = 1; |
||
166 | while (true) { |
||
167 | try { |
||
168 | $records->next(); |
||
169 | break 1; |
||
170 | } catch (BadRequestError $e) { |
||
171 | $this->error($e->getMessage()); |
||
172 | $this->error($e->getCode()); |
||
173 | $this->error('Bad request. Attempt ' . $attempt . ' of 100. Sleeping 10 secs.'); |
||
174 | if ($attempt > 100) { |
||
175 | throw $e; |
||
176 | } |
||
177 | ++$attempt; |
||
178 | sleep(10); |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | |||
183 | return $recordsHarvested; |
||
184 | } |
||
185 | |||
214 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.