Skip to content

[Support] How to use MixedType with AddParamTypeDeclarationRector Rule? #6865

@SMAtaurRahman

Description

@SMAtaurRahman

Question

Hi, sorry for asking the question here.

I am currently upgrading a project to PHP 8.1 and Rector is just being awesome at it.

But, some of the builtin interfaces now has type declaration (eg. ArrayAccess, Iterator), and PHP throws Deprecation notice if any class implements them but contains no type declaration.

To avoid using #[\ReturnTypeWillChange], I want to add type declaration to the classes as they are in interfaces.
To do that I'm using two rules: AddParamTypeDeclarationRector & AddReturnTypeDeclarationRector. Both of them works fine except for MixedType.
When I'm trying to add mixed as parameter or return type, rector just ignoring it. Maybe I'm doing something wrong. Can you please guide me.

Here is a example code that I'm using:

use PHPStan\Type\MixedType;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector;
use Rector\TypeDeclaration\ValueObject\AddParamTypeDeclaration;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    $services = $containerConfigurator->services();
    $services->set(AddParamTypeDeclarationRector::class)
      ->configure(
          [
              new AddParamTypeDeclaration(\ArrayAccess::class, 'offsetSet', 0, new MixedType(true)),
              new AddParamTypeDeclaration(\ArrayAccess::class, 'offsetSet', 1, new MixedType(false)), // I don't know what isExplicitMixed means here
          ]
    );
};

Sorry for wasting your time.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions