Rector: ParamTypeDeclarationRector applying one of two types

Created on 6 Nov 2019  路  6Comments  路  Source: rectorphp/rector

| Subject | Details |
| :------------- | :----------------------------------------------------------- |
| Rector version | v0.5.19 |
| PHP version | PHP 7.3 |
| Full Command | vendor\bin\rector.bat process FFI --config rector.yaml |

Current Behaviour

I'm not done with the issue analysis yet, but I make the issue now. I will complete it later with more info and a PR if needed.

I have a file like this:

namespace {
    class FFI{
        /**
         * Returns size of C data type of the given FFI\CData or FFI\CType.
         *
         * @param CData|CType $pointer
         * @return int
         */
        public static function sizeof(&$pointer): int {}
    }
}

When I apply rector, it become:

namespace {
    class FFI{
        /**
         * Returns size of C data type of the given FFI\CData or FFI\CType.
         *
         * @param CData|CType $pointer
         * @return int
         */
        public static function sizeof(\CData &$pointer): int {}
    }
}

It seems it choose one of the type and added it to the signature, It shouldn't do that (I checked, the two classes don't have a common parent)

Here is the complete log with --debug:


C:\Users\username\PhpstormProjects\phpstorm-stubs>vendor\bin\rector.bat process FFI --config rector.yaml --debug
Rector v0.5.19
Config file: C:\Users\username\PhpstormProjects\phpstorm-stubs\rector.yaml

[parsing] FFI/FFI.php

./FFI/FFI.php required for FFI

./FFI/FFI.php required for FFI
[refactoring] FFI/FFI.php
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
    [applying] Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedParamTypeRector
    [applying] Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector
[printing] FFI/FFI.php


1 file with changes
===================

1) FFI/FFI.php

    ---------- begin diff ----------
--- Original
+++ New
@@ -182,7 +182,7 @@
          * @param CData|CType $pointer
          * @return int
          */
-        public static function sizeof(&$pointer): int {}
+        public static function sizeof(\CData &$pointer): int {}

         /**
          * Returns size of C data type of the given FFI\CData or FFI\CType.
@@ -190,7 +190,7 @@
          * @param CData|CType $pointer
          * @return int
          */
-        public static function alignof(&$pointer): int {}
+        public static function alignof(\CData &$pointer): int {}

         /**
          * Copies $size bytes from memory area $source to memory area $target.
    ----------- end diff -----------

Applied rectors:

 * Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector



 [OK] Rector is done! 1 changed files                                                                                   




Thanks

bug

All 6 comments

What's the relation of CData and CType? Is one children of another?

No relation between the two. They don't have parent nor childrens

I see. Expected behavior is no change?

Yes. It seems that union types may come soon(https://wiki.php.net/rfc/union_types_v2) but in the meantime, it shouldn't add one of the two classes

Cool! Thanks!

Sure :+1:

Was this page helpful?
0 / 5 - 0 ratings