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

[PHP83] Implements a rule to add types to class constants #5290

Merged
merged 8 commits into from
Dec 3, 2023

Conversation

peterfox
Copy link
Contributor

@peterfox peterfox commented Nov 27, 2023

With PHP 8.3 being released I thought I'd add another rule. This time for Types Class Constants.

The rule will change classes, traits and interfaces that contain constants.

The behavior of this rule is limited to mostly the type being obvious.

  • The rule won't try to detect types based on operations.
  • The rule won't apply if a parent class has the constant, or an interface extending interfaces.
  • The rule won't apply if a trait being used has the constant.
  • The rule won't try to override anything for instance if the type is a union already.
  • The rule won't apply if referencing another constant.

Some of these scenarios could be added over time but it makes sense to at least have a start that picks up the basics.

@TomasVotruba
Copy link
Member

Hi, thanks for kicking of this PHP 8.3 rule. I look foward to it for many years.

Let's ship it so we can test and iterate improvements 👍

@TomasVotruba TomasVotruba merged commit 52b6654 into rectorphp:main Dec 3, 2023
39 checks passed
@pepeh
Copy link

pepeh commented Dec 4, 2023

This rule is broken when you have private and then public const:

<?php

class Test
{
    private const A = 1000;
    public const B = 'foo';
    public const C = 'bar';
}

will be changed to

<?php

class Test
{
    private const int A = 1000;
    public const int B = 'foo'; // int?
    public const int C = 'bar';
}

And when first variable is public, then it applied to private only.

<?php

class Test
{
    public const A = 1000;
    private const string B = 'foo';
    private const string  C = 'bar';
}

@peterfox
Copy link
Contributor Author

peterfox commented Dec 5, 2023

Will take a look into 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.

4 participants