psalm-plugin-symfony icon indicating copy to clipboard operation
psalm-plugin-symfony copied to clipboard

#[Required] must supress PropertyNotSetInConstructor

Open Punk-UnDeaD opened this issue 4 years ago • 1 comments

Hello

we need something like this

<?php

declare(strict_types=1);

namespace App\Psalm;

use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use Psalm\Codebase;
use Psalm\FileSource;
use Psalm\Plugin\Hook\AfterClassLikeVisitInterface;
use Psalm\Storage\ClassLikeStorage;
use Symfony\Contracts\Service\Attribute\Required;

class RequiredPropertyHandler implements AfterClassLikeVisitInterface
{
    public static function afterClassLikeVisit(
        ClassLike $stmt,
        ClassLikeStorage $storage,
        FileSource $statements_source,
        Codebase $codebase,
        array &$file_replacements = []
    ) {
        if (!$stmt instanceof Class_) {
            return;
        }
        foreach ($storage->properties as $name => $property) {
            foreach ($property->attributes as $attribute) {
                if (Required::class === $attribute->fq_class_name) {
                    $storage->initialized_properties[$name] = true;
                    break;
                }
            }
        }
    }
}

Punk-UnDeaD avatar Feb 09 '21 20:02 Punk-UnDeaD

hi @Punk-UnDeaD,

Thank you for the issue. The code seems valid. Can you please create a PR for it?

seferov avatar Feb 11 '21 06:02 seferov