| Conditions | 6 |
| Paths | 3 |
| Total Lines | 69 |
| 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 |
||
| 91 | public function updateServer($server) |
||
| 92 | { |
||
| 93 | if (!isset($server['address'])) { |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | $server['address'] = trim($server['address']); |
||
| 97 | $app = $this; |
||
| 98 | if (isset($app->jobMap[$server['address']])) { |
||
| 99 | //\PHPDaemon\Daemon::log('already doing: '.$server['address']); |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | $job = new \PHPDaemon\Core\ComplexJob(function ($job) use ($app, $server) { |
||
| 103 | unset($app->jobMap[$server['address']]); |
||
| 104 | //\PHPDaemon\Daemon::log('Removed job for '.$server['address']. ' ('.sizeof($app->jobMap).')'); |
||
| 105 | $set = $job->results['info']; |
||
| 106 | $set['address'] = $server['address']; |
||
| 107 | $set['players'] = $job->results['players']; |
||
| 108 | $set['latency'] = $job->results['latency']; |
||
| 109 | $set['atime'] = time(); |
||
| 110 | if (0) { |
||
| 111 | \PHPDaemon\Core\Daemon::log('Updated server (' . round(memory_get_usage(true) / 1024 / 1024, |
||
| 112 | 5) . '): ' . $server['address'] . ' latency = ' . round($set['latency'] * 1000, 2) . ' ==== ' |
||
| 113 | . (isset($server['atime']) ? |
||
| 114 | round($set['atime'] - $server['atime']) . ' secs. from last update.' |
||
| 115 | : ' =---= ' . json_encode($server)) |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | try { |
||
| 119 | $app->servers->upsert(['_id' => $server['_id']], ['$set' => $set]); |
||
| 120 | } catch (\MongoException $e) { |
||
| 121 | \PHPDaemon\Core\Daemon::uncaughtExceptionHandler($e); |
||
| 122 | $app->servers->upsert(['_id' => $server['_id']], ['$set' => ['atime' => time()]]); |
||
| 123 | } |
||
| 124 | }); |
||
| 125 | $app->jobMap[$server['address']] = $job; |
||
| 126 | //\PHPDaemon\Daemon::log('Added job for '.$server['address']); |
||
| 127 | |||
| 128 | $job('info', function ($jobname, $job) use ($app, $server) { |
||
| 129 | $app->client->requestInfo($server['address'], |
||
| 130 | function ($conn, $result) use ($app, $server, $jobname, $job) { |
||
| 131 | |||
| 132 | $job('players', function ($jobname, $job) use ($app, $server, $conn) { |
||
| 133 | |||
| 134 | $conn->requestPlayers(function ($conn, $result) use ($app, $jobname, $job) { |
||
| 135 | |||
| 136 | $job->setResult($jobname, $result); |
||
| 137 | $conn->finish(); |
||
| 138 | |||
| 139 | }); |
||
| 140 | }); |
||
| 141 | |||
| 142 | $job->setResult($jobname, $result); |
||
| 143 | }); |
||
| 144 | }); |
||
| 145 | |||
| 146 | $job('latency', function ($jobname, $job) use ($app, $server) { |
||
| 147 | |||
| 148 | $app->client->ping($server['address'], function ($conn, $result) use ($app, $jobname, $job) { |
||
| 149 | |||
| 150 | $job->setResult($jobname, $result); |
||
| 151 | |||
| 152 | $conn->finish(); |
||
| 153 | |||
| 154 | }); |
||
| 155 | |||
| 156 | }); |
||
| 157 | |||
| 158 | $job(); |
||
| 159 | } |
||
| 160 | |||
| 248 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.