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

Incorrect behavior of DowngradeParameterTypeWideningRector #5994

Closed
leoloso opened this issue Mar 25, 2021 · 0 comments · Fixed by #5995
Closed

Incorrect behavior of DowngradeParameterTypeWideningRector #5994

leoloso opened this issue Mar 25, 2021 · 0 comments · Fixed by #5995
Labels

Comments

@leoloso
Copy link
Contributor

leoloso commented Mar 25, 2021

Bug Report

Subject Details
Rector version last dev-master
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.org/demo/1b8ee6a0-7d50-4fae-8e15-f19d19591fc5

<?php
trait StreamDecoratorTrait
{
    protected function createStream()
    {
        // ...
    }
}

class MultipartStream
{
    use StreamDecoratorTrait;

    protected function createStream(array $elements)
    {
        // ...
    }
}

Responsible rules

  • DowngradeParameterTypeWideningRector

Expected Behavior

The rule is currently doing this:

class MultipartStream
{
    use StreamDecoratorTrait;

-    protected function createStream(array $elements)
+    protected function createStream($elements)
    {
        // ...
    }
}

Instead, it should do nothing:

class MultipartStream
{
    use StreamDecoratorTrait;

    protected function createStream(array $elements)
    {
        // ...
    }
}

DowngradeParameterTypeWideningRector should not remove the type array of the param $elements, because the different method comes from a trait. But only parent classes and interfaces should be considered, not traits.

Debugging DowngradeParameterTypeWideningRector, I found out this:

// Remove the types in:
// - all ancestors + their descendant classes
// - all implemented interfaces + their implementing classes
$parameterTypesByParentClassLikes = $this->resolveParameterTypesByClassLike(
    $classReflection,
    $methodName,
    $position
);
var_dump($parameterTypesByParentClassLikes);

Prints:

array(2) {
  ["GuzzleHttp\Psr7\MultipartStream"]=>
  object(PHPStan\Type\ArrayType)#27706 (2) {
    ["keyType":"PHPStan\Type\ArrayType":private]=>
    object(PHPStan\Type\MixedType)#27715 (2) {
      ["isExplicitMixed":"PHPStan\Type\MixedType":private]=>
      bool(false)
      ["subtractedType":"PHPStan\Type\MixedType":private]=>
      NULL
    }
    ["itemType":"PHPStan\Type\ArrayType":private]=>
    object(PHPStan\Type\MixedType)#27701 (2) {
      ["isExplicitMixed":"PHPStan\Type\MixedType":private]=>
      bool(false)
      ["subtractedType":"PHPStan\Type\MixedType":private]=>
      NULL
    }
  }
  ["GuzzleHttp\Psr7\StreamDecoratorTrait"]=>
  object(PHPStan\Type\MixedType)#27710 (2) {
    ["isExplicitMixed":"PHPStan\Type\MixedType":private]=>
    bool(false)
    ["subtractedType":"PHPStan\Type\MixedType":private]=>
    NULL
  }
}

Hence, StreamDecoratorTrait is also being picked by resolveParameterTypesByClassLike, that's the problem.

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

Successfully merging a pull request may close this issue.

1 participant