Skip to content

feat: add optional rule to report properties that should be promoted #4115

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

Closed
wants to merge 1 commit into from

Conversation

calebdw
Copy link
Contributor

@calebdw calebdw commented Jul 19, 2025

Hello!

Not sure if this would be better suited for phpstan-strict-rules, but I figured I'd try it here first.

This adds an optional rule that reports properties that can easily be promoted, which have the following form:

class Foo
{
    public int $count;
    public function __construct(int $count)
    {
        $this->count = $count;
    }
}

// should be

class Foo
{
    public function __construct(public int $count)
    {
    }
}

Thanks!

@calebdw calebdw force-pushed the calebdw/push-zypoqzwrlsns branch 2 times, most recently from 691ef12 to 81dd717 Compare July 19, 2025 02:04
@calebdw calebdw force-pushed the calebdw/push-zypoqzwrlsns branch from 81dd717 to 7c50a04 Compare July 19, 2025 03:05
@VincentLanglet
Copy link
Contributor

Not sure if this would be better suited for phpstan-strict-rules, but I figured I'd try it here first.

This seems to me really opinionated indeed. Also

  • It does not really provide any benefit on the code quality or analysis
  • You will report unresolvable errors on PHP < 8.0

Such behavior is better on a automated fixable tool, like php-cs-fixer or rector IMHO.
And rector already have such rule https://getrector.com/rule-detail/class-property-assign-to-constructor-promotion-rector

@calebdw
Copy link
Contributor Author

calebdw commented Jul 19, 2025

Ah good to know, I've been meaning to take a look at rector but just haven't done it yet. Thanks!

@calebdw calebdw closed this Jul 19, 2025
@calebdw calebdw deleted the calebdw/push-zypoqzwrlsns branch July 19, 2025 13:25
@ondrejmirtes
Copy link
Member

Personally I'm using "slevomat/coding-standard" for this.

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

Successfully merging this pull request may close these issues.

3 participants