rectorphp / rector

Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
https://getrector.com
MIT License
8.73k stars 686 forks source link

Php4ConstructorRector rule not applied #8100

Closed akash-grexit closed 1 year ago

akash-grexit commented 1 year ago

Bug Report

Subject Details
Rector version 0.17.7

Minimal PHP Code Causing Issue

foo.php

<?php

class Foo {
    function Foo() { 
        echo 'Constructed!';
    }
}

rector.php

<?php

use Rector\Config\RectorConfig;
use Rector\Php70\Rector\ClassMethod\Php4ConstructorRector;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([__DIR__ . '/public/src/foo.php']);
    // register single rule
    $rectorConfig->rule(Php4ConstructorRector::class);
};

Expected Behaviour

 <?php

 class Foo {
    function __construct() { 
         echo 'Constructed!';
     }
 }

Demo: https://getrector.com/demo/8e684b99-3c59-48a1-8730-d9cddc88aaac

I tested the rule in this demo environment, and it's working there. However, it's not working on my machine.

samsonasik commented 1 year ago

We unfortunatelly can't help without reproducible code, it possibly due to typo or some case sensitive in part of name class. I am closing it, feel free to re-open with reproducible code when you can, thank you,

dimov-cz commented 1 year ago

Title: Inconsistent Output between PHP 7 and PHP 8 in Rector v0.18.5

Description

Noticed the same issue, not applied rules. I'm observing different behaviors when running Rector v0.18.5 on PHP 7 and PHP 8, despite the composer.json indicating support for both versions ("php": "^7.2|^8.0").

Steps to Reproduce

  1. Install Rector v0.18.5.
  2. Run Rector on a codebase using PHP 7.

Expected Behavior

The output should be consistent regardless of the PHP version used.

Actual Behavior

The output is not the same when using PHP 7.

Environment

Additional Information

I noticed the same issue as above.

samsonasik commented 1 year ago

Again, we can't help without reproducible code.

dimov-cz commented 1 year ago

I have created minimal repo to reproduce, code taken from the report above.

Steps to reproduce:

git clone git@github.com:dimov-cz/rector-issue8100.git .
composer install
vendor/bin/rector process --dry-run --debug

PHP 8.1 environment output:

/mnt/devCode/y/src/foo.php

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

1) src/foo.php:0

    ---------- begin diff ----------
@@ @@
 <?php

 class Foo {
-    function Foo() {
+    function __construct() {
         echo 'Constructed!';
     }
 }
    ----------- end diff -----------

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

PHP 7.4 environment output:

/mnt/devCode/x/src/foo.php

 [OK] Rector is done!
samsonasik commented 1 year ago

I see, the :

if (!$classReflection->hasConstructor()) {

seems only detected on php8+, while on php 7.x, it not detected.