0

I am using PHPStan for static analysis on a PHP application, I have this set quite high which can be problematic at times and i'm getting the following error.

    public function generate(GetFileData $data): Response
    {
        /** @var File $fileResponse */
        $fileResponse = stripe()->files->retrieve(
            $data->file_ref
        );

        $mimetype = $fileResponse->type ? $this->getMimeType($fileResponse->type) : 'text/csv';
        $authKey = base64_encode(Str::ascii(config('stripe.keys.secret')));
        $response = Http::withHeaders([
            'Authorization' => 'Basic '.$authKey,
        ])->get($fileResponse->url);

        return response($response->body())->header('content-type', $mimetype);
    }

    private function getMimeType(string $mimetype): string
    {
        $mimetypes = [
            'apk' => 'application/vnd.android.package-archive',
            'csv' => 'text/csv',
            'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'gif' => 'image/gif',
            'html' => 'text/html',
            'jpeg' => 'image/jpeg',
            'json' => 'application/json',
            'jsonl' => 'application/jsonl',
            'markdown' => 'text/markdown',
            'pdf' => 'application/pdf',
            'png' => 'image/png',
            'svg' => 'image/svg+xml',
            'tiff' => 'image/tiff',
            'tsv' => 'text/tab-separated-values',
            'txt' => 'text/plain',
            'xlsl' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            'xml' => 'application/xml',
            'zip' => 'application/zip',
        ];

        return $mimetypes[$mimetype];
    }

In the generate() I get the following phpstan error

  92     Parameter #1 $url of method Illuminate\Http\Client\PendingRequest::get() expects string, string|null given.  
 ------ ------------------------------------------------------------------------------------------------------------- 

How should I fix this?

1
  • 1
    So $fileResponse->url can be null. What can you do with it? Commented Feb 9, 2024 at 11:08

2 Answers 2

0

Use one of the techniques described in Narrowing types https://phpstan.org/writing-php-code/narrowing-types to eliminate the possibility of the property value being null.

Sign up to request clarification or add additional context in comments.

Comments

0

You either have to check $fileResponse->url is not null and handle it in code before you reach the get.

Or if you are confident it will always be a string you could;

/** @var string $url */
$url = $fileResponse->url;
$response = Http::withHeaders([
    'Authorization' => 'Basic '.$authKey,
])->get($url);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.