Skip to content

Ruleset: bug fix - correctly handle empty array property setting #865

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

Merged
merged 1 commit into from
Mar 12, 2025

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Mar 8, 2025

Description

While already handled correctly for inline properties set via phpcs:set, setting a property to an empty array from a ruleset file was not handled correctly until now.

As things were, when setting an array property from the ruleset to an empty array like so:

    <rule ref="Standard.Category.SniffName">
        <properties>
            <property name="arrayProperty" type="array"/>
        </properties>
    </rule>

the property value would be set to:

public $arrayProperty = [
    0 => '',
];

... while the expected behaviour would be:

public $arrayProperty = [];

While it is not expected that this type of property setting is encountered a lot in the wild, it should still work correctly.

Fixed now.

Includes test safeguarding the fix.

Suggested changelog entry

Setting an array property to an empty array from an XML ruleset now works correctly.
Previously, the property value would be set to [0 => ''].

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

While already handled correctly for inline properties set via `phpcs:set`, setting a property to an empty array from a ruleset file was not handled correctly until now.

As things were, when setting an array property from the ruleset to an empty array like so:
```xml
    <rule ref="Standard.Category.SniffName">
        <properties>
            <property name="arrayProperty" type="array"/>
        </properties>
    </rule>
```

 the property value would be set to:
```php
public $arrayProperty = [
    0 => '',
];
```
... while the expected behaviour would be:
```php
public $arrayProperty = [];
```

While it is not expected that this type of property setting is encountered a lot in the wild, it should still work correctly.

Fixed now.

Includes test safeguarding the fix.
@jrfnl jrfnl added this to the 3.12.0 milestone Mar 8, 2025
@jrfnl jrfnl requested a review from fredden March 8, 2025 19:42
@jrfnl
Copy link
Member Author

jrfnl commented Mar 8, 2025

@michalbundyra As you worked on this initially in squizlabs/PHP_CodeSniffer#1665, would you like to have a look at this/review this PR ?

Copy link
Contributor

@michalbundyra michalbundyra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrfnl nice one! It's been a while when I was working on it, and definitely it was a bug I wasn't aware of before. LGTM 👍

@jrfnl
Copy link
Member Author

jrfnl commented Mar 8, 2025

@michalbundyra Thanks for looking this over! I'll leave the PR open for a few more days to give others a chance to pitch in, but if there are no objections, I'll include it in the 3.12.0 release.

@jrfnl jrfnl merged commit 4e2c91d into master Mar 12, 2025
61 checks passed
@jrfnl jrfnl deleted the feature/ruleset-test-property-setting-empty-array branch March 12, 2025 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants