Skip to content

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

Merged
TomasVotruba merged 8 commits intorectorphp:mainfrom
peterfox:feature/typed-consts
Dec 3, 2023
Merged

[PHP83] Implements a rule to add types to class constants#5290
TomasVotruba merged 8 commits intorectorphp:mainfrom
peterfox:feature/typed-consts

Conversation

@peterfox
Copy link
Copy Markdown
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.

@peterfox peterfox requested a review from samsonasik November 27, 2023 11:01
@peterfox peterfox requested a review from samsonasik November 27, 2023 17:46
@TomasVotruba
Copy link
Copy Markdown
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
@pepeh
Copy link
Copy Markdown

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
Copy Markdown
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