Skip to content

EnforceReadonlyPublicPropertyRule vs. get property hook in interface #300

@kaskader201

Description

@kaskader201

When I want to use the property hook (only get) on a public readonly property the rule says that the property is not marked as readonly, which is not possible in the interface.

In my opinion, the attached example should be fine.

https://phpstan.org/r/01ab1923-5648-46da-803d-e024aeb82ddf

I have a commit ready to fix it, but I can't push it.

        if ($this->phpVersion->supportsPropertyHooks()) {
            foreach ($node->getHooks() as $hook) {
                if ($hook->name->toString() === 'set') {
                    $error = RuleErrorBuilder::message("Public property `{$node->getName()}` cannot have a setter hook, because is mark as readonly.")
                        ->identifier('shipmonk.publicPropertyNotReadonly')
                        ->build();

                    return [$error];
                }
            }

            if ($classReflection->isInterface()) {
                return [];
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions