rector
rector copied to clipboard
ReturnTypeFromStrictScalarReturnExprRector adds invalid return type ?mixed
Bug Report
| Subject | Details |
|---|---|
| Rector version | v0.14.0 |
When a function returns either null or mixed, the ReturnTypeFromStrictScalarReturnExprRector rule adds ?mixed. This causes a fatal error:
Type mixed cannot be marked as nullable since mixed already includes null
Minimal PHP Code Causing Issue
<?php
function returnsNullOrMixed()
{
if (rand() < 0.5) {
return null;
}
return json_decode('something', null, 512, JSON_THROW_ON_ERROR);
}
Expected Behaviour
Should add mixed instead of ?mixed.
@thomasschiet could you try provide a PR patch? You can do the following steps:
- Add failing fixture on
https://github.com/rectorphp/rector-src/tree/b54c62993056f984ca68d75c654b98794f931582/rules-tests/CodeQuality/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector/Fixture
- Run test:
vendor/bin/phpunit rules-tests/CodeQuality/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector
- Provide a patch, this seems bug on
UnionTypeMapper, which mixed can't be included inNullableTypeat at https://github.com/rectorphp/rector-src/blob/b54c62993056f984ca68d75c654b98794f931582/packages/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php#L141
- if (! $this->nodeNameResolver->isName($nullabledTypeNode, 'false')) {
+ if (! $this->nodeNameResolver->isNames($nullabledTypeNode, ['false', 'mixed'])) {
Thank you.
@samsonasik Thanks! I'll provide the PR later today.