| Conditions | 10 |
| Paths | 15 |
| Total Lines | 57 |
| Lines | 18 |
| Ratio | 31.58 % |
| 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 |
||
| 61 | public function basicRulesAndTasksWork() |
||
| 62 | { |
||
| 63 | View Code Duplication | $task1 = new ProcessCallbackTask(function () { |
|
| 64 | touch(__DIR__ . "/task1.temp"); |
||
| 65 | |||
| 66 | for ($i = 0; $i < 10; $i++) { |
||
| 67 | usleep(50000); |
||
| 68 | } |
||
| 69 | |||
| 70 | unlink(__DIR__ . "/task1.temp"); |
||
| 71 | }); |
||
| 72 | |||
| 73 | View Code Duplication | $task2 = new ProcessCallbackTask(function () { |
|
| 74 | touch(__DIR__ . "/task2.temp"); |
||
| 75 | |||
| 76 | for ($i = 0; $i < 10; $i++) { |
||
| 77 | usleep(50000); |
||
| 78 | } |
||
| 79 | |||
| 80 | unlink(__DIR__ . "/task2.temp"); |
||
| 81 | }); |
||
| 82 | |||
| 83 | $rule = new InMemoryRule(); |
||
| 84 | $rule->setProcesses(1); |
||
| 85 | $rule->setMinimumProcessorUsage(0); |
||
| 86 | $rule->setMaximumProcessorUsage(100); |
||
| 87 | |||
| 88 | $added = false; |
||
| 89 | |||
| 90 | $this->manager->addRule($rule); |
||
| 91 | $this->manager->addTask($task1); |
||
| 92 | |||
| 93 | while ($this->manager->tick()) { |
||
| 94 | usleep(50000); |
||
| 95 | |||
| 96 | if (!$added) { |
||
| 97 | $this->manager->addTask($task2); |
||
| 98 | $added = true; |
||
| 99 | } |
||
| 100 | |||
| 101 | if (file_exists(__DIR__ . "/task1.temp") && file_exists(__DIR__ . "/task2.temp")) { |
||
| 102 | $this->fail("Tasks should not be run concurrently"); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | $this->manager->removeRule($rule); |
||
| 107 | $this->manager->addTask($task1); |
||
| 108 | $this->manager->addTask($task2); |
||
| 109 | |||
| 110 | if ($this->manager->tick()) { |
||
| 111 | usleep(50000); |
||
| 112 | |||
| 113 | if (!file_exists(__DIR__ . "/task1.temp") || !file_exists(__DIR__ . "/task2.temp")) { |
||
| 114 | $this->fail("Tasks should be run concurrently"); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 |