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

Add check for adding param to method #801

Merged
merged 1 commit into from
Sep 5, 2024

Conversation

AydinHassan
Copy link
Contributor

@AydinHassan AydinHassan commented Sep 5, 2024

Heyy,

We recently encountered #750 and I had a quick stab at implementing it, to start the conversation off. I guess we should also support removing. Feedback welcome!

Fixes #750

use function array_map;

/**
* Adding a parameter to a method on a non-final class is a BC break
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add the "why" in this comment, and then we can merge :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Updated :)

@Ocramius Ocramius added this to the 8.9.0 milestone Sep 5, 2024
Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢 thanks @AydinHassan!

@Ocramius Ocramius self-assigned this Sep 5, 2024
@Ocramius
Copy link
Member

Ocramius commented Sep 5, 2024

@AydinHassan uh-oh, some more tests to add to cover the last introduced mutations

Warning: Escaped Mutant for Mutator "LogicalOr":

--- Original
+++ New
@@ @@
 {
     public function __invoke(ReflectionMethod $fromMethod, ReflectionMethod $toMethod) : Changes
     {
-        if ($fromMethod->getDeclaringClass()->isFinal() || $fromMethod->isPrivate()) {
+        if ($fromMethod->getDeclaringClass()->isFinal() && $fromMethod->isPrivate()) {
             return Changes::empty();
         }
         $added = array_diff(array_map(static fn(ReflectionParameter $param) => $param->getName(), $toMethod->getParameters()), array_map(static fn(ReflectionParameter $param) => $param->getName(), $fromMethod->getParameters()));


Warning: Escaped Mutant for Mutator "UnwrapArrayDiff":

--- Original
+++ New
@@ @@
         if ($fromMethod->getDeclaringClass()->isFinal() || $fromMethod->isPrivate()) {
             return Changes::empty();
         }
-        $added = array_diff(array_map(static fn(ReflectionParameter $param) => $param->getName(), $toMethod->getParameters()), array_map(static fn(ReflectionParameter $param) => $param->getName(), $fromMethod->getParameters()));
+        $added = array_map(static fn(ReflectionParameter $param) => $param->getName(), $toMethod->getParameters());
         if ($toMethod->getNumberOfParameters() > $fromMethod->getNumberOfParameters()) {
             return Changes::fromList(...array_map(static fn(string $paramName): Change => Change::added(Str\format('Parameter %s was added to Method %s() of class %s', $paramName, $fromMethod->getName(), $fromMethod->getDeclaringClass()->getName())), $added));
         }


Warning: Escaped Mutant for Mutator "UnwrapArrayMap":

--- Original
+++ New
@@ @@
         if ($fromMethod->getDeclaringClass()->isFinal() || $fromMethod->isPrivate()) {
             return Changes::empty();
         }
-        $added = array_diff(array_map(static fn(ReflectionParameter $param) => $param->getName(), $toMethod->getParameters()), array_map(static fn(ReflectionParameter $param) => $param->getName(), $fromMethod->getParameters()));
+        $added = array_diff(array_map(static fn(ReflectionParameter $param) => $param->getName(), $toMethod->getParameters()), $fromMethod->getParameters());
         if ($toMethod->getNumberOfParameters() > $fromMethod->getNumberOfParameters()) {
             return Changes::fromList(...array_map(static fn(string $paramName): Change => Change::added(Str\format('Parameter %s was added to Method %s() of class %s', $paramName, $fromMethod->getName(), $fromMethod->getDeclaringClass()->getName())), $added));
         }


Warning: Escaped Mutant for Mutator "GreaterThan":

--- Original
+++ New
@@ @@
             return Changes::empty();
         }
         $added = array_diff(array_map(static fn(ReflectionParameter $param) => $param->getName(), $toMethod->getParameters()), array_map(static fn(ReflectionParameter $param) => $param->getName(), $fromMethod->getParameters()));
-        if ($toMethod->getNumberOfParameters() > $fromMethod->getNumberOfParameters()) {
+        if ($toMethod->getNumberOfParameters() >= $fromMethod->getNumberOfParameters()) {
             return Changes::fromList(...array_map(static fn(string $paramName): Change => Change::added(Str\format('Parameter %s was added to Method %s() of class %s', $paramName, $fromMethod->getName(), $fromMethod->getDeclaringClass()->getName())), $added));
         }
         return Changes::empty();
     }
 }

Warning:  Dashboard report has not been sent: The current process is a pull request build

This can be tested by expanding your data provider:

  • add examples that remove parameters -> shouldn't trip this comparator
  • add examples with private methods -> shouldn't trip this comparator

@AydinHassan
Copy link
Contributor Author

AydinHassan commented Sep 5, 2024

@Ocramius I tried adding a few cases which I've just pushed. I also added some other tests that I since deleted but it doesn't seem to fix the escaped mutants. Sorry I don't have any experience with Infection and looks like I am fundamentally missing something. Whatever I change seems to have no effect :D

edit: i'm a dumbass missed adding the provider cases

edit 2: Ok I managed to kill a few but I'm a bit lost with the GreaterThan mutant

edit 3: Fixed the last one by deleting code 🔪

@Ocramius could you so kindly approve the workflow :)

@Ocramius
Copy link
Member

Ocramius commented Sep 5, 2024

@Ocramius I tried adding a few cases which I've just pushed. I also added some other tests that I since deleted but it doesn't seem to fix the escaped mutants. Sorry I don't have any experience with Infection and looks like I am fundamentally missing something. Whatever I change seems to have no effect :D

edit: i'm a dumbass missed adding the provider cases

edit 2: Ok I managed to kill a few but I'm a bit lost with the GreaterThan mutant

edit 3: Fixed the last one by deleting code 🔪

@Ocramius could you so kindly approve the workflow :)

and now you know how it works :-)

Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢 excellent!

@Ocramius Ocramius assigned Ocramius and unassigned AydinHassan Sep 5, 2024
@Ocramius Ocramius merged commit 97dc077 into Roave:8.9.x Sep 5, 2024
12 checks passed
@AydinHassan
Copy link
Contributor Author

thanks for the guidance and speed @Ocramius ! 🏎️

@AydinHassan AydinHassan deleted the method-param-added-check branch September 5, 2024 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Why is this not a breaking change?
2 participants