Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I get PropertyNotSetInConstructor when extending the symfony Response class #290

Open
loevgaard opened this issue Dec 5, 2022 · 3 comments

Comments

@loevgaard
Copy link

Hi

I have this class:

<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Renderer;

use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\Response;

final class PdfResponse extends Response
{
    public function __construct(string $content, string $filename = 'gift_card.pdf')
    {
        parent::__construct($content);

        $disposition = HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, $filename);
        $this->headers->set('Content-Type', 'application/pdf');
        $this->headers->set('Content-Disposition', $disposition);
    }
}

I am using v4.0.0 of this Psalm plugin and I get the following errors:

ERROR: PropertyNotSetInConstructor - src/Renderer/PdfResponse.php:11:13 - Property Setono\SyliusGiftCardPlugin\Renderer\PdfResponse::$headers is not defined in constructor of Setono\SyliusGiftCardPlugin\Renderer\PdfResponse or in any private or final methods called in the constructor (see https://psalm.dev/074)
final class PdfResponse extends Response


ERROR: PropertyNotSetInConstructor - src/Renderer/PdfResponse.php:11:13 - Property Setono\SyliusGiftCardPlugin\Renderer\PdfResponse::$content is not defined in constructor of Setono\SyliusGiftCardPlugin\Renderer\PdfResponse or in any private or final methods called in the constructor (see https://psalm.dev/074)
final class PdfResponse extends Response


ERROR: PropertyNotSetInConstructor - src/Renderer/PdfResponse.php:11:13 - Property Setono\SyliusGiftCardPlugin\Renderer\PdfResponse::$version is not defined in constructor of Setono\SyliusGiftCardPlugin\Renderer\PdfResponse or in any private or final methods called in the constructor (see https://psalm.dev/074)
final class PdfResponse extends Response


ERROR: PropertyNotSetInConstructor - src/Renderer/PdfResponse.php:11:13 - Property Setono\SyliusGiftCardPlugin\Renderer\PdfResponse::$statusCode is not defined in constructor of Setono\SyliusGiftCardPlugin\Renderer\PdfResponse or in any private or final methods called in the constructor (see https://psalm.dev/074)
final class PdfResponse extends Response


ERROR: PropertyNotSetInConstructor - src/Renderer/PdfResponse.php:11:13 - Property Setono\SyliusGiftCardPlugin\Renderer\PdfResponse::$statusText is not defined in constructor of Setono\SyliusGiftCardPlugin\Renderer\PdfResponse or in any private or final methods called in the constructor (see https://psalm.dev/074)
final class PdfResponse extends Response


ERROR: PropertyNotSetInConstructor - src/Renderer/PdfResponse.php:11:13 - Property Setono\SyliusGiftCardPlugin\Renderer\PdfResponse::$charset is not defined in constructor of Setono\SyliusGiftCardPlugin\Renderer\PdfResponse or in any private or final methods called in the constructor (see https://psalm.dev/074)
final class PdfResponse extends Response


ERROR: UninitializedProperty - src/Renderer/PdfResponse.php:22:9 - Cannot use uninitialized property $this->headers (see https://psalm.dev/186)
        $this->headers->set('Content-Type', 'application/pdf');


ERROR: PossiblyNullReference - src/Renderer/PdfResponse.php:22:25 - Cannot call method set on possibly null value (see https://psalm.dev/083)
        $this->headers->set('Content-Type', 'application/pdf');


ERROR: UninitializedProperty - src/Renderer/PdfResponse.php:23:9 - Cannot use uninitialized property $this->headers (see https://psalm.dev/186)
        $this->headers->set('Content-Disposition', $disposition);


ERROR: PossiblyNullReference - src/Renderer/PdfResponse.php:23:25 - Cannot call method set on possibly null value (see https://psalm.dev/083)
        $this->headers->set('Content-Disposition', $disposition);

Am I doing something wrong or why doesn't Psalm understand that these are invalid errors?

@seferov
Copy link
Member

seferov commented Dec 5, 2022

@loevgaard hello,

can you please try it like the following?

<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Renderer;

use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\Response;

final class PdfResponse extends Response
{
    public function __construct(string $content, string $filename = 'gift_card.pdf')
    {
        $disposition = HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, $filename);
        
        parent::__construct($content, 200, [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => $disposition,
        ]);
    }
}

@loevgaard
Copy link
Author

Yes, that works. I guess the other problem is actually a Psalm issue. When the parent::__construct() call is made it initializes the Response::$header property. I would've thought it could detect that or maybe there is a good reason.

Thanks anyways!

@loevgaard
Copy link
Author

I was too fast. I forgot to clear the cache: Here are the errors I have after that change:

ERROR: PropertyNotSetInConstructor - src/Renderer/PdfResponse.php:11:13 - Property Setono\SyliusGiftCardPlugin\Renderer\PdfResponse::$headers is not defined in constructor of Setono\SyliusGiftCardPlugin\Renderer\PdfResponse or in any methods called in the constructor (see https://psalm.dev/074)
final class PdfResponse extends Response


ERROR: PropertyNotSetInConstructor - src/Renderer/PdfResponse.php:11:13 - Property Setono\SyliusGiftCardPlugin\Renderer\PdfResponse::$content is not defined in constructor of Setono\SyliusGiftCardPlugin\Renderer\PdfResponse or in any methods called in the constructor (see https://psalm.dev/074)
final class PdfResponse extends Response


ERROR: PropertyNotSetInConstructor - src/Renderer/PdfResponse.php:11:13 - Property Setono\SyliusGiftCardPlugin\Renderer\PdfResponse::$version is not defined in constructor of Setono\SyliusGiftCardPlugin\Renderer\PdfResponse or in any methods called in the constructor (see https://psalm.dev/074)
final class PdfResponse extends Response


ERROR: PropertyNotSetInConstructor - src/Renderer/PdfResponse.php:11:13 - Property Setono\SyliusGiftCardPlugin\Renderer\PdfResponse::$statusCode is not defined in constructor of Setono\SyliusGiftCardPlugin\Renderer\PdfResponse or in any methods called in the constructor (see https://psalm.dev/074)
final class PdfResponse extends Response


ERROR: PropertyNotSetInConstructor - src/Renderer/PdfResponse.php:11:13 - Property Setono\SyliusGiftCardPlugin\Renderer\PdfResponse::$statusText is not defined in constructor of Setono\SyliusGiftCardPlugin\Renderer\PdfResponse or in any methods called in the constructor (see https://psalm.dev/074)
final class PdfResponse extends Response


ERROR: PropertyNotSetInConstructor - src/Renderer/PdfResponse.php:11:13 - Property Setono\SyliusGiftCardPlugin\Renderer\PdfResponse::$charset is not defined in constructor of Setono\SyliusGiftCardPlugin\Renderer\PdfResponse or in any methods called in the constructor (see https://psalm.dev/074)
final class PdfResponse extends Response

@loevgaard loevgaard reopened this Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants