Skip to content

SplitGroupedClassConstantsRector drops the type of every constant after the first #9928

Closed
rectorphp/rector-src
#8650

Description

@lts-bob

Bug Report

Subject Details
Rector version 2.7.0
Rule SplitGroupedClassConstantsRector

SplitGroupedClassConstantsRector drops the declared type, and any attribute groups, from every constant after the first when it splits a typed group.

Minimal PHP Code Causing Issue

<?php

declare(strict_types=1);

class OpenGroup
{
    public const int B = 2, C = 3;
}

Run with the CODING_STYLE set (the rule is in CodingStyleLevel::RULES).

Responsible rules

  • SplitGroupedClassConstantsRector

Expected Behavior

-    public const int B = 2, C = 3;
+    public const int B = 2;
+    public const int C = 3;

Actual Behavior

-    public const int B = 2, C = 3;
+    public const int B = 2;
+    public const C = 3;

The same happens in an interface and a trait. In a final class AddTypeToConstRector (TYPE_DECLARATION) then re-adds int from the value, which hides the bug there. Anywhere else the declared type is lost.

Cause

createNextClassConsts() builds each new statement as

new ClassConst([$const], $classConst->flags, $classConst->getAttributes());

which passes neither $classConst->attrGroups nor $classConst->type. Passing both would keep the type and the attributes:

new ClassConst([$const], $classConst->flags, $classConst->getAttributes(), $classConst->attrGroups, $classConst->type);

Whether the attribute groups should be copied onto every split constant is your call. They currently apply to the whole group, so copying them preserves the meaning.

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