t-kanstantsin /
fileupload
| 1 | <?php |
||||
| 2 | declare(strict_types=1); |
||||
| 3 | |||||
| 4 | namespace tkanstantsin\fileupload\saver; |
||||
| 5 | |||||
| 6 | use GuzzleHttp\Client as HttpClient; |
||||
| 7 | use Mimey\MimeTypes; |
||||
| 8 | use yii\web\UploadedFile; |
||||
|
0 ignored issues
–
show
|
|||||
| 9 | |||||
| 10 | /** |
||||
| 11 | * Class Factory |
||||
| 12 | */ |
||||
| 13 | class Factory |
||||
| 14 | { |
||||
| 15 | /** |
||||
| 16 | * @param string $content |
||||
| 17 | * @param string|null $fileName |
||||
| 18 | * @return null|UploadedFile |
||||
| 19 | */ |
||||
| 20 | public static function buildFromString(string $content, string $fileName = null): ?UploadedFile |
||||
| 21 | { |
||||
| 22 | return new UploadedFile([ |
||||
| 23 | 'name' => $fileName, |
||||
| 24 | 'tempName' => static::getTempFile($content), |
||||
| 25 | 'type' => (new MimeTypes())->getMimeType(pathinfo($fileName)['extension'] ?? null), |
||||
| 26 | 'size' => mb_strlen($content), |
||||
| 27 | ]); |
||||
| 28 | } |
||||
| 29 | |||||
| 30 | /** |
||||
| 31 | * @param string $url |
||||
| 32 | * @param string|null $fileName |
||||
| 33 | * @return null|UploadedFile |
||||
| 34 | * @throws \GuzzleHttp\Exception\GuzzleException |
||||
| 35 | */ |
||||
| 36 | public static function buildFromUrl(string $url, string $fileName = null): ?UploadedFile |
||||
| 37 | { |
||||
| 38 | // TODO: strict file type. |
||||
| 39 | // TODO: strict size. |
||||
| 40 | // TODO: use error codes. |
||||
| 41 | $client = new HttpClient(); |
||||
| 42 | $response = $client->request('GET', $url); |
||||
| 43 | |||||
| 44 | return new UploadedFile([ |
||||
| 45 | 'name' => $fileName ?? pathinfo($url, PATHINFO_BASENAME), |
||||
| 46 | 'tempName' => static::getTempFile((string) $response->getBody()), |
||||
| 47 | 'type' => $response->getHeaderLine('Content-Type'), |
||||
| 48 | 'size' => (int) $response->getHeaderLine('Content-Length'), |
||||
| 49 | ]); |
||||
| 50 | } |
||||
| 51 | |||||
| 52 | /** |
||||
| 53 | * @param string $fileName |
||||
| 54 | * @return null|UploadedFile |
||||
| 55 | */ |
||||
| 56 | public static function buildFromFile(string $fileName): ?UploadedFile |
||||
| 57 | { |
||||
| 58 | return new UploadedFile([ |
||||
| 59 | 'name' => $fileName, |
||||
| 60 | 'tempName' => $fileName, |
||||
| 61 | 'type' => (new MimeTypes())->getMimeType(pathinfo($fileName)['extension'] ?? null), |
||||
| 62 | 'size' => filesize($fileName), |
||||
| 63 | ]); |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | /** |
||||
| 67 | * @param string $content |
||||
| 68 | * @return bool|resource |
||||
| 69 | */ |
||||
| 70 | protected static function getTempFile(string $content) |
||||
| 71 | { |
||||
| 72 | $stream = fopen('php://memory','rb+'); |
||||
| 73 | fwrite($stream, $content); // write file into stream |
||||
|
0 ignored issues
–
show
It seems like
$stream can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 74 | rewind($stream); // reset stream pointer to start |
||||
|
0 ignored issues
–
show
It seems like
$stream can also be of type false; however, parameter $handle of rewind() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 75 | |||||
| 76 | return $stream; |
||||
| 77 | } |
||||
| 78 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths