Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DowngradePhp81] Handle New_ inside array on DowngradeNewInInitializerRector #1508

Merged
merged 6 commits into from
Dec 15, 2021

Conversation

samsonasik
Copy link
Member

@TomasVotruba I tried to solve on https://github.com/rectorphp/rector-src/runs/4537147879?check_suite_focus=true#step:14:80

I tried downgrade command locally and it seems cause create property, but assign new in property, which still invalid, I will looking into it.

➜  rector-src git:(handle-new-in-array) bin/rector process vendor/rector/rector-doctrine/src/NodeManipulator/ColumnPropertyTypeResolver.php -c build/config/config-downgrade.php --dry-run
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
1 file with changes
===================

1) vendor/rector/rector-doctrine/src/NodeManipulator/ColumnPropertyTypeResolver.php:23

    ---------- begin diff ----------
@@ @@
      * @var string
      */
     private const DATE_TIME_INTERFACE = 'DateTimeInterface';
-
     /**
+     * @readonly
+     * @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
+     */
+    private $phpDocInfoFactory;
+    /**
+     * @var \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory
+     */
+    private $typeFactory;
+    /**
+     * @var array<string, \PHPStan\Type\Type>
+     */
+    private $doctrineTypeToScalarType = [
+        'tinyint' => new BooleanType(),
+        // integers
+        'smallint' => new IntegerType(),
+        'mediumint' => new IntegerType(),
+        'int' => new IntegerType(),
+        'integer' => new IntegerType(),
+        'bigint' => new IntegerType(),
+        'numeric' => new IntegerType(),
+        // floats
+        'decimal' => new FloatType(),
+        'float' => new FloatType(),
+        'double' => new FloatType(),
+        'real' => new FloatType(),
+        // strings
+        'tinytext' => new StringType(),
+        'mediumtext' => new StringType(),
+        'longtext' => new StringType(),
+        'text' => new StringType(),
+        'varchar' => new StringType(),
+        'string' => new StringType(),
+        'char' => new StringType(),
+        'longblob' => new StringType(),
+        'blob' => new StringType(),
+        'mediumblob' => new StringType(),
+        'tinyblob' => new StringType(),
+        'binary' => new StringType(),
+        'varbinary' => new StringType(),
+        'set' => new StringType(),
+        // date time objects
+        'date' => new ObjectType(self::DATE_TIME_INTERFACE),
+        'datetime' => new ObjectType(self::DATE_TIME_INTERFACE),
+        'timestamp' => new ObjectType(self::DATE_TIME_INTERFACE),
+        'time' => new ObjectType(self::DATE_TIME_INTERFACE),
+        'year' => new ObjectType(self::DATE_TIME_INTERFACE),
+    ];
+    /**
      * @param array<string, Type> $doctrineTypeToScalarType
      * @see https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/basic-mapping.html#doctrine-mapping-types
      */
-    public function __construct(
-        private readonly PhpDocInfoFactory $phpDocInfoFactory,
-        private TypeFactory $typeFactory,
-        private array $doctrineTypeToScalarType = [
+    public function __construct(PhpDocInfoFactory $phpDocInfoFactory, TypeFactory $typeFactory, array $doctrineTypeToScalarType = null)
+    {
+        $doctrineTypeToScalarType = $doctrineTypeToScalarType ?? [
             'tinyint' => new BooleanType(),
             // integers
             'smallint' => new IntegerType(),
@@ @@
             'timestamp' => new ObjectType(self::DATE_TIME_INTERFACE),
             'time' => new ObjectType(self::DATE_TIME_INTERFACE),
             'year' => new ObjectType(self::DATE_TIME_INTERFACE),
-        ],
-    ) {
+        ];
+        $this->phpDocInfoFactory = $phpDocInfoFactory;
+        $this->typeFactory = $typeFactory;
+        $this->doctrineTypeToScalarType = $doctrineTypeToScalarType;
     }

     public function resolve(Property $property): ?Type
    ----------- end diff -----------

Applied rules:
 * DowngradeNullCoalescingOperatorRector (https://wiki.php.net/rfc/null_coalesce_equal_operator)
 * DowngradeTypedPropertyRector
 * DowngradeTrailingCommasInParamUseRector
 * DowngradeAttributeToAnnotationRector (https://php.watch/articles/php-attributes#syntax)
 * DowngradePropertyPromotionRector (https://wiki.php.net/rfc/constructor_promotion)
 * DowngradeNewInInitializerRector (https://wiki.php.net/rfc/new_in_initializers)
 * DowngradeReadonlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)


                                                                                                                        
 [OK] 1 file would have changed (dry-run) by Rector                                                                     
                                                                                                                        

@samsonasik
Copy link
Member Author

I added failing fixture c7c87ee for combination between:

Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector;
Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector;
Rector\DowngradePhp81\Rector\FunctionLike\DowngradeNewInInitializerRector;

@samsonasik
Copy link
Member Author

Fixed 🎉

@samsonasik
Copy link
Member Author

I tried the downgrade script and it seems working ok:

bin/rector process vendor/rector/rector-doctrine/src/NodeManipulator/ColumnPropertyTypeResolver.php -c build/config/config-downgrade.php --dry-run
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
1 file with changes
===================

1) vendor/rector/rector-doctrine/src/NodeManipulator/ColumnPropertyTypeResolver.php:23

    ---------- begin diff ----------
@@ @@
      * @var string
      */
     private const DATE_TIME_INTERFACE = 'DateTimeInterface';
-
     /**
+     * @readonly
+     * @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
+     */
+    private $phpDocInfoFactory;
+    /**
+     * @var \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory
+     */
+    private $typeFactory;
+    /**
+     * @var array<string, \PHPStan\Type\Type>
+     */
+    private $doctrineTypeToScalarType;
+    /**
      * @param array<string, Type> $doctrineTypeToScalarType
      * @see https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/basic-mapping.html#doctrine-mapping-types
      */
-    public function __construct(
-        private readonly PhpDocInfoFactory $phpDocInfoFactory,
-        private TypeFactory $typeFactory,
-        private array $doctrineTypeToScalarType = [
+    public function __construct(PhpDocInfoFactory $phpDocInfoFactory, TypeFactory $typeFactory, array $doctrineTypeToScalarType = null)
+    {
+        $doctrineTypeToScalarType = $doctrineTypeToScalarType ?? [
             'tinyint' => new BooleanType(),
             // integers
             'smallint' => new IntegerType(),
@@ @@
             'timestamp' => new ObjectType(self::DATE_TIME_INTERFACE),
             'time' => new ObjectType(self::DATE_TIME_INTERFACE),
             'year' => new ObjectType(self::DATE_TIME_INTERFACE),
-        ],
-    ) {
+        ];
+        $this->phpDocInfoFactory = $phpDocInfoFactory;
+        $this->typeFactory = $typeFactory;
+        $this->doctrineTypeToScalarType = $doctrineTypeToScalarType;
     }

     public function resolve(Property $property): ?Type
    ----------- end diff -----------

Applied rules:
 * DowngradeNullCoalescingOperatorRector (https://wiki.php.net/rfc/null_coalesce_equal_operator)
 * DowngradeTypedPropertyRector
 * DowngradeTrailingCommasInParamUseRector
 * DowngradeAttributeToAnnotationRector (https://php.watch/articles/php-attributes#syntax)
 * DowngradePropertyPromotionRector (https://wiki.php.net/rfc/constructor_promotion)
 * DowngradeNewInInitializerRector (https://wiki.php.net/rfc/new_in_initializers)
 * DowngradeReadonlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)

@samsonasik samsonasik enabled auto-merge (squash) December 15, 2021 19:33
@samsonasik
Copy link
Member Author

@TomasVotruba let's give it a try.

@samsonasik samsonasik merged commit e0ba9a3 into main Dec 15, 2021
@samsonasik samsonasik deleted the handle-new-in-array branch December 15, 2021 19:37
@samsonasik
Copy link
Member Author

@TomasVotruba it works 🎉

Screen Shot 2021-12-16 at 02 56 07

@TomasVotruba
Copy link
Member

Thank you for the rescue 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants