Conditions | 13 |
Paths | 2 |
Total Lines | 60 |
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 |
||
29 | public function init() |
||
30 | { |
||
31 | if (!isset($this->attrs->server['FR_PATH'])) { |
||
32 | $this->status(404); |
||
33 | $this->finish(); |
||
34 | return; |
||
35 | } |
||
36 | $job = new \PHPDaemon\Core\ComplexJob(function ($job) { |
||
|
|||
37 | $this->wakeup(); |
||
38 | }); |
||
39 | $this->job = $job; |
||
40 | $this->sleep(5, true); |
||
41 | $this->attrs->server['FR_PATH'] = \PHPDaemon\FS\FileSystem::sanitizePath($this->attrs->server['FR_PATH']); |
||
42 | $job('stat', function ($name, $job) { |
||
43 | /** @var \PHPDaemon\Core\ComplexJob $job */ |
||
44 | \PHPDaemon\FS\FileSystem::stat($this->attrs->server['FR_PATH'], function ($path, $stat) use ($job) { |
||
45 | if ($stat === -1) { |
||
46 | $this->fileNotFound(); |
||
47 | $job->setResult('stat', false); |
||
48 | return; |
||
49 | } |
||
50 | if ($stat['type'] === 'd') { |
||
51 | if (!\PHPDaemon\FS\FileSystem::$supported) { |
||
52 | $this->file(rtrim($path, '/') . '/index.html'); |
||
53 | } else { |
||
54 | $job('readdir', function ($name, $job) use ($path) { |
||
55 | /** @var \PHPDaemon\Core\ComplexJob $job */ |
||
56 | \PHPDaemon\FS\FileSystem::readdir(rtrim($path, '/'), function ($path, $dir) use ($job) { |
||
57 | $found = false; |
||
58 | if (is_array($dir)) { |
||
59 | foreach ($dir['dents'] as $file) { |
||
60 | if ($file['type'] === \EIO_DT_REG) { // is file |
||
61 | if (in_array($file['name'], $this->appInstance->indexFiles)) { |
||
62 | $this->file($path . '/' . $file['name']); |
||
63 | $found = true; |
||
64 | break; |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | if (!$found) { |
||
70 | if (isset($this->attrs->server['FR_AUTOINDEX']) && $this->attrs->server['FR_AUTOINDEX']) { |
||
71 | $this->autoindex($path, $dir); |
||
72 | } else { |
||
73 | $this->fileNotFound(); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | $job->setResult('readdir'); |
||
78 | }, \EIO_READDIR_STAT_ORDER | \EIO_READDIR_DENTS); |
||
79 | }); |
||
80 | } |
||
81 | } elseif ($stat['type'] === 'f') { |
||
82 | $this->file($path); |
||
83 | } |
||
84 | $job->setResult('stat', $stat); |
||
85 | }); |
||
86 | }); |
||
87 | $job(); |
||
88 | } |
||
89 | |||
258 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.