Skip to content

Incorrect behavior of NarrowWideUnionReturnTypeRector on assert() #9838

Description

@samsonasik

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.com/demo/8e48a73d-15a7-4c5b-b80d-84e652712f0b

<?php

final class DemoFile
{
    /**
     * @var array{
     *     cache: array{directory: null}
     * }
     */
    protected array $config = [
        'cache' => [
            'directory' => null,
        ],
    ];
    
    public function getCacheDirectory(): ?string
    {
        $dir = $this->config['cache']['directory'] ?? null;
        \assert(\is_string($dir) || $dir === null, 'Invalid cache directory.');

        return $dir;
    }
}

Responsible rules

  • NarrowWideUnionReturnTypeRector

Expected Behavior

On assert(), it can still execute, just show warning, so changing return type is invalid here, see https://3v4l.org/02vqB#v8.5.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions