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

Incorrect behavior of ReturnTypeFromStrictNewArrayRector #8635

Closed
benharold opened this issue May 13, 2024 · 0 comments · Fixed by rectorphp/rector-src#5875
Closed

Incorrect behavior of ReturnTypeFromStrictNewArrayRector #8635

benharold opened this issue May 13, 2024 · 0 comments · Fixed by rectorphp/rector-src#5875
Labels

Comments

@benharold
Copy link

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/630b6283-e769-46d0-b928-542ad1bae349

<?php

class Example
{
    /**
     * @param array $data
     * @return array
     */
    public function canReturnNull($data)
    {
        $styles = [
            'key' => 'value',
        ];

        if (is_array($data)) {
            foreach ($data as $propertyName => $propertyData) {
                $data[$propertyName] = $this->coerceStyles(
                    $propertyData,
                    $styles
                );
            }
        }
        
        return $data;
    }
}

Responsible rules

  • ReturnTypeFromStrictNewArrayRector

Expected Behavior

The docblock here is intentionally incorrect, which seems to be a contributing factor to this issue. Although the documentation indicates that this method should only accept an array, in reality it can accept anything. If, for example, null is passed instead of an array, the method will return null. Rector should not add the : array return type declaration in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant