fix offset type (#5119)

* fix offset type

* skip ChangeGlobalVariablesToPropertiesRector if empty
This commit is contained in:
Tomas Votruba 2021-01-09 11:50:47 +01:00 committed by GitHub
parent 2cf2ee3f41
commit 7d441c1806
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 2 deletions

View File

@ -5,6 +5,8 @@ declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Type;
@ -23,7 +25,7 @@ final class HasOffsetTypeMapper implements TypeMapperInterface
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
throw new ShouldNotHappenException();
return new ArrayTypeNode(new IdentifierTypeNode('mixed'));
}
/**
@ -39,6 +41,6 @@ final class HasOffsetTypeMapper implements TypeMapperInterface
*/
public function mapToDocString(Type $type, ?Type $parentType = null): string
{
return 'hasOfset()';
return 'mixed[]';
}
}

View File

@ -91,6 +91,10 @@ CODE_SAMPLE
$this->collectGlobalVariableNamesAndRefactorToPropertyFetch($node);
if ($this->globalVariableNames === []) {
return null;
}
foreach ($this->globalVariableNames as $globalVariableName) {
$this->addPropertyToClass($classLike, null, $globalVariableName);
}

View File

@ -0,0 +1,46 @@
<?php
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayReturnDocTypeRector\Fixture;
final class HasOffset
{
private function convertArguments($service)
{
if (! is_array($service)) {
return $service;
}
if (! isset($service['arguments'])) {
return $service;
}
return $service;
}
}
?>
-----
<?php
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayReturnDocTypeRector\Fixture;
final class HasOffset
{
/**
* @return mixed|mixed[]
*/
private function convertArguments($service)
{
if (! is_array($service)) {
return $service;
}
if (! isset($service['arguments'])) {
return $service;
}
return $service;
}
}
?>