| Conditions | 1 |
| Paths | 1 |
| Total Lines | 65 |
| Lines | 7 |
| Ratio | 10.77 % |
| 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 |
||
| 20 | public function onReady() |
||
| 21 | { |
||
| 22 | |||
| 23 | // Adding listener |
||
| 24 | // ComplexJob - STATE_WAITING |
||
| 25 | $job = new \PHPDaemon\Core\ComplexJob(function ($job) { |
||
| 26 | // ComplexJob - STATE_DONE |
||
| 27 | /*array ( |
||
| 28 | 'bar' => |
||
| 29 | array ( |
||
| 30 | 'job' => 'bar', |
||
| 31 | 'success' => false, |
||
| 32 | 'line' => 63, |
||
| 33 | ), |
||
| 34 | 'foo' => |
||
| 35 | array ( |
||
| 36 | 'job' => 'foo', |
||
| 37 | 'success' => true, |
||
| 38 | 'line' => 84, |
||
| 39 | 'arg' => |
||
| 40 | array ( |
||
| 41 | 'param' => 'value', |
||
| 42 | ), |
||
| 43 | ), |
||
| 44 | 'baz' => |
||
| 45 | array ( |
||
| 46 | 'job' => 'baz', |
||
| 47 | 'success' => false, |
||
| 48 | 'line' => 94, |
||
| 49 | ), |
||
| 50 | )*/ |
||
| 51 | \PHPDaemon\Core\Daemon::log($job->results); |
||
| 52 | }); |
||
| 53 | |||
| 54 | // Adding listener |
||
| 55 | // ComplexJob - STATE_WAITING |
||
| 56 | $job->addListener(function ($job) { |
||
|
|
|||
| 57 | // ComplexJob - STATE_DONE |
||
| 58 | }); |
||
| 59 | |||
| 60 | // Adding async job foo |
||
| 61 | $job('foo', $this->foo(['param' => 'value'])); |
||
| 62 | |||
| 63 | // Adding with 1 sec delay |
||
| 64 | \PHPDaemon\Core\Timer::add(function ($event) use ($job) { |
||
| 65 | |||
| 66 | // Adding async job bar |
||
| 67 | View Code Duplication | $job('bar', function ($jobname, $job) { |
|
| 68 | \PHPDaemon\Core\Timer::add(function ($event) use ($jobname, $job) { |
||
| 69 | // Job done |
||
| 70 | $job->setResult($jobname, ['job' => 'bar', 'success' => false, 'line' => __LINE__]); |
||
| 71 | $event->finish(); |
||
| 72 | }, 1e3 * 50); |
||
| 73 | }); |
||
| 74 | |||
| 75 | // Adding async job baz. Equal $job('baz', $this->baz()); |
||
| 76 | $job->addJob('baz', $this->baz()); |
||
| 77 | |||
| 78 | // Run jobs. All listeners will be called when the jobs done |
||
| 79 | // ComplexJob - STATE_RUNNING |
||
| 80 | $job(); |
||
| 81 | |||
| 82 | $event->finish(); |
||
| 83 | }, 1e6 * 1); |
||
| 84 | } |
||
| 85 | |||
| 108 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.