infection icon indicating copy to clipboard operation
infection copied to clipboard

`declare(strict_types = 1);` mutator

Open HenkPoley opened this issue 4 years ago • 5 comments

Is your feature request related to a problem? Please describe. Maybe there are technical issues, but I couldn't find any mutator that attempts to add declare(strict_types = 1); and check if your program's tests still function (or find 'the bug').

As a way to promote stricter PHP types wherever possible.

HenkPoley avatar Aug 13 '20 08:08 HenkPoley

  1. There's a declare_strict_types rule for PHP-CS-Fixer, which does exactly that.
  2. Infection works starting from coverage reports. These header lines are not executed per se during testing, therefore Infection couldn't know what tests to run to test against this addition.

With these two thoughts in mind I think Infection isn't the right tool for the job: you can get around with much simpler means like running that fixer rule, and testing after.

sanmai avatar Aug 13 '20 08:08 sanmai

Well, what Infection does is up to you 😅.

Strict types is local to a file, so that could mean all the tests that touch a line in that file?

Or are there further reaching effects? E.g. an object created from a strict type file, has strict type processing when passed to other files, or something??

Anyways, if this is a 'won't fix', I don't mind. Just thought it would be one of these things that could be tested part by part, instead of in all files at once (like what php-cs-fixer probably would do). Which is a thing that Infection does.

HenkPoley avatar Aug 13 '20 10:08 HenkPoley

As far as Infection concerned, these lines are not covered by any test, just like any other uncovered lines. The thing is for uncovered lines Infection won't run any tests. This line, or any others.

You can see what Infection sees for yourself like this:

phpdbg -qrr vendor/bin/phpunit --coverage-html=coverage
open coverage/index.html

sanmai avatar Aug 13 '20 11:08 sanmai

Ah I see, it only mutates lines of the coverage report. This strict_types statement is outside of its "foreach-loop" that goes over the coverage data.

It just feels that Infection is sort of at the appropriate place to twiddle that toggle and re-run tests. But I guess, it's not quite looking at the files from the right angle to do this.

HenkPoley avatar Aug 13 '20 12:08 HenkPoley

It just feels that Infection is sort of at the appropriate place to twiddle that toggle and re-run tests. But I guess, it's not quite looking at the files from the right angle to do this.

That's true but it's ignoring the major performance cost that mutation testing has in general. So it's a lot more practical to just either apply strict_types everywhere or nowhere with a CS-Fixer and run the tests once manually than relying on mutation testing for it.

theofidry avatar Aug 17 '20 10:08 theofidry