| Conditions | 13 |
| Paths | 223 |
| Total Lines | 80 |
| Code Lines | 58 |
| 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 |
||
| 100 | public function start(): bool |
||
| 101 | { |
||
| 102 | if (isset($_GET['t']) && !empty($_GET['t'])) { |
||
| 103 | $token = $_GET['t']; |
||
| 104 | if (isset($_GET['download'])) { |
||
| 105 | $download = (bool) $_GET['download']; |
||
| 106 | } else { |
||
| 107 | $download = false; |
||
| 108 | } |
||
| 109 | |||
| 110 | try { |
||
| 111 | $node = $this->sharelink->findNodeWithShareToken($token); |
||
| 112 | $share = $node->getAppAttributes('Balloon\\App\\Sharelink'); |
||
| 113 | |||
| 114 | if (array_key_exists('password', $share)) { |
||
| 115 | $valid = false; |
||
| 116 | if (isset($_POST['password'])) { |
||
| 117 | $valid = hash('sha256', $_POST['password']) === $share['password']; |
||
| 118 | } |
||
| 119 | |||
| 120 | if (false === $valid) { |
||
| 121 | echo "<form method=\"post\">\n"; |
||
| 122 | echo "Password: <input type=\"password\" name=\"password\"/>\n"; |
||
| 123 | echo "<input type=\"submit\" value=\"Submit\"/>\n"; |
||
| 124 | echo "</form>\n"; |
||
| 125 | exit(); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | if ($node instanceof Collection) { |
||
| 130 | $mime = 'application/zip'; |
||
| 131 | $stream = $node->getZip(); |
||
| 132 | $name = $node->getName().'.zip'; |
||
| 133 | } else { |
||
| 134 | $mime = $node->getContentType(); |
||
| 135 | $stream = $node->get(); |
||
| 136 | $name = $node->getName(); |
||
| 137 | } |
||
| 138 | |||
| 139 | if (true === $download || preg_match('#html#', $mime)) { |
||
| 140 | header('Content-Disposition: attachment; filename*=UTF-8\'\''.rawurlencode($name)); |
||
| 141 | header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); |
||
| 142 | header('Content-Type: application/octet-stream'); |
||
| 143 | header('Content-Length: '.$node->getSize()); |
||
| 144 | header('Content-Transfer-Encoding: binary'); |
||
| 145 | } else { |
||
| 146 | header('Content-Disposition: inline; filename="'.$name.'"'); |
||
| 147 | header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); |
||
| 148 | header('Content-Type: '.$mime); |
||
| 149 | } |
||
| 150 | |||
| 151 | if (null === $stream) { |
||
| 152 | exit(); |
||
| 153 | } |
||
| 154 | |||
| 155 | while (!feof($stream)) { |
||
| 156 | echo fread($stream, 8192); |
||
| 157 | } |
||
| 158 | } catch (\Exception $e) { |
||
| 159 | $this->logger->error("failed load node with access token [$token]", [ |
||
| 160 | 'category' => get_class($this), |
||
| 161 | 'exception' => $e, |
||
| 162 | ]); |
||
| 163 | |||
| 164 | (new Response()) |
||
| 165 | ->setOutputFormat('text') |
||
| 166 | ->setCode(404) |
||
| 167 | ->setBody('Token is invalid or share link is expired') |
||
| 168 | ->send(); |
||
| 169 | } |
||
| 170 | } else { |
||
| 171 | (new Response()) |
||
| 172 | ->setOutputFormat('text') |
||
| 173 | ->setCode(401) |
||
| 174 | ->setBody('No token submited') |
||
| 175 | ->send(); |
||
| 176 | } |
||
| 177 | |||
| 178 | return true; |
||
| 179 | } |
||
| 180 | } |
||
| 181 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: