rectorphp / rector-phpunit

Rector upgrade rules for PHPUnit
http://getrector.com
MIT License
61 stars 46 forks source link

Is there any rule to float? #256

Closed eerison closed 10 months ago

eerison commented 10 months ago

Hello guys

I'm bumping phpunit to version 9 and I faced this issue in some places

$this->assertSame(15.28, $container->getUnsettledAmount());

//error:
//Failed asserting that 15.280000000000001 is identical to 15.28.

is there any rule to change

//from
$this->assertSame(15.28, $container->getUnsettledAmount());

//to
$this->assertEqualsWithDelta(15.28, $container->getUnsettledAmount(), 0.01);
TomasVotruba commented 10 months ago

Hi,

if there is such a rule, you can find it here: https://github.com/rectorphp/rector-phpunit/blob/main/docs/rector_rules_overview.md

eerison commented 10 months ago

there is this rule: https://github.com/rectorphp/rector-phpunit/blob/main/docs/rector_rules_overview.md#assertequalsparametertospecificmethodstyperector

But I didn't see a way to pass the delta value :/

TomasVotruba commented 10 months ago

I see. Would you like to improve it?

eerison commented 10 months ago

there is this rule: https://github.com/rectorphp/rector-phpunit/blob/main/docs/rector_rules_overview.md#assertequalsparametertospecificmethodstyperector

But I didn't see a way to pass the delta value :/

Yep, Should I improve this rule or create a new one?

TomasVotruba commented 10 months ago

What would you recommend from you as end user?

eerison commented 10 months ago

What would you recommend from you as end user?

I guess improve the current rule is more interesting 🤔, I will try that, and we can see in the PR ;)

eerison commented 10 months ago

Hey @TomasVotruba

I'm follwing those 2 examples

and when I'm debugging it is passing into getNodeTypes method, But it does not go into refactor method :/

it's my class

<?php

namespace crmi\tests;

use Mockery\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector extends AbstractRector
{
    /**
     * This method helps other to understand the rule and to generate documentation.
     */
    public function getRuleDefinition(): RuleDefinition
    {
        return new RuleDefinition(
            'Change method calls from set* to change*.',
            [
                new CodeSample(
                // code before
                    '$user->setPassword("123456");',
                    // code after
                    '$user->changePassword("123456");'
                ),
            ]
        );
    }

    /**
     * @return array<class-string<Node>>
     */
    public function getNodeTypes(): array
    {
        // what node types are we looking for?
        // pick any node from https://github.com/rectorphp/php-parser-nodes-docs/
        return [MethodCall::class];
    }

    public function refactor(Node $node): ?Node
    {
        $methodCallName = $this->getName($node->name);
        if ($methodCallName === null) {
            return null;
        }

        // we only care about "set*" method names
        if (! str_starts_with($methodCallName, 'set')) {
            // return null to skip it
            return null;
        }

        $newMethodCallName = preg_replace('#^set#', 'change', $methodCallName);

        $node->name = new Identifier($newMethodCallName);

        // return $node if you modified it
        return $node;
    }
}
Screenshot 2023-09-15 at 11 06 39

am I missing something?

Note: I'm doing it into the project just as a first try, after that I will pass to the lib ;)

TomasVotruba commented 10 months ago

Sure. The namespace use Mockery\MethodCall; is the trouble. It happened to me million times, IDE could be a bit smarter indeed :)

eerison commented 10 months ago

Ohhh man, after I change to use PhpParser\Node\Expr\MethodCall;, now it's passing into refactor, thank you :)

eerison commented 10 months ago

Are PRs allowed to main branch only? or is there other branch that accepts php 7.4?

eerison commented 10 months ago

Are PRs allowed to main branch only? or is there other branch that accepts php 7.4?

What? how is it possible? this repository requires php >=8.1, But I could install this in my project (php 7.4 🤯 )

Or is it possible, because I require rector in my dev dependency and in dev dependency does not have php restrictions 👀

TomasVotruba commented 10 months ago

We have only the main branch.

The code is downgraded to PHP 7.2, so it will be available to use on PHP 7.4 too.