Skip to content

Reducing the set visibility of a property (private(set), protected(set)) is not reported #1020

Open

Description

@NopeCharon

PHP 8.4 asymmetric visibility allows restricting who can write to a property (public private(set), public protected(set), ...). Reducing the set visibility of an existing property isn't reported, but it breaks every write from the scopes that lost access:

// before
class Foo
{
    public int $count = 0;
}

// after
class Foo
{
    public private(set) int $count = 0;
}

// downstream
$foo->count = 5; // Error: Cannot modify private(set) property Foo::$count from global scope

The same applies to public protected(set) (breaks writes from outside the class hierarchy), and to protected(set) → private(set) or protected → protected private(set) in non-final classes, which breaks writes from child classes.

The tool currently reports nothing for any of these changes: PropertyVisibilityReduced only compares the read visibility.

A PropertySetVisibilityReduced check, run wherever PropertyVisibilityReduced runs, would cover this. BetterReflection already exposes ReflectionProperty#isPrivateSet() and #isProtectedSet(). A few cases should not be reported (or are already reported elsewhere):

  • a symmetric visibility reduction (e.g. public → protected), already reported by PropertyVisibilityReduced
  • a property becoming readonly, which is implicitly protected(set) and already reported by PropertyBecameReadonly
  • protected(set) → private(set) in a final class, which has no child classes

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions