How to add explicit mixed
type?
#7033
-
I need to create a rule that adds explicit - public $foo;
+ public mixed $foo; - public function foo($bar)
+ public function foo(mixed $bar): mixed I’ve tried passing For example: <?php
declare(strict_types = 1);
use PHPStan\Type\MixedType;
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector;
use Rector\TypeDeclaration\ValueObject\AddReturnTypeDeclaration;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function(ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(AddReturnTypeDeclarationRector::class)
->configure([
new AddReturnTypeDeclaration('my\Class', 'foo', new MixedType()),
]);
}; Is there a way to achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
mixed type on property is currently not supported as far as I know due to can be various implementation:
that will require a very careful implementation, on very specific use case, and require check child/parent use case for check above use cases. You can however, create your own rule for it to fill it: $node->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(new MixedType(true), TypeKind::PROPERTY());
return $node; |
Beta Was this translation helpful? Give feedback.
-
@brandonkelly I stand corrected, it actually supported on return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(AddPropertyTypeDeclarationRector::class)
->configure([
new AddPropertyTypeDeclaration('my\Class', 'value', new MixedType(true)),
]);
}; |
Beta Was this translation helpful? Give feedback.
@brandonkelly I stand corrected, it actually supported on
AddPropertyTypeDeclarationRector
, you can define explicit mixed typenew MixedType(true)
like the following: