What's new in Infection 0.20.0

Nov 1, 2020

Release: https://github.com/infection/infection/releases/tag/0.20.0

New features and enhancements

Github Annotations logger πŸš€

This logger is supposed to be used only with GitHub Actions. It prints GitHub Annotation warnings for escaped Mutants right in the Pull Request:

GitHub Annotation Escaped Mutant

Usage (look at the real example how Infection uses it itself):

# this is needed on GitHub Actions to fetch the base branch to make a diff
git fetch --depth=1 origin $GITHUB_BASE_REF

infection.phar --logger-github --git-diff-filter=AM

Read below why you may need to use --git-diff-filter option

It’s also possible to configure this logger in infection.json file. Results will be printed to stdout:

{
"logs": {
"github": true
}
}

--git-diff-filter option

Allows filtering files to mutate by using git diff with --diff-filter option. Sensible values are: AM - added and modified files. A - only added files.

Best to be used during pull request builds on CI, e.g. with GitHub Actions, Travis CI and so on.

Usage:

# this is needed on GitHub Actions to fetch the base branch to make a diff
git fetch --depth=1 origin $GITHUB_BASE_REF

infection.phar --git-diff-filter=A

This command will mutate only those files, that were added in the Pull Request. The diff is done between the current branch and the base branch.

It’s possible to configure the base branch, see --git-diff-base option

--git-diff-base option

Supposed to be used only with --git-diff-filter option. Configures the base branch for git diff command.

Usage:

# this is needed on GitHub Actions to fetch the base branch to make a diff
git fetch --depth=1 origin $GITHUB_BASE_REF

infection.phar --git-diff-base=origin/$GITHUB_BASE_REF --git-diff-filter=AM

New Mutators

Ternary mutator

This mutator mutates a ternary operator:

- isset($b) ? 'B' : 'C';
+ isset($b) ? 'C' : 'B';
$foo = 'foo';
- $foo ?: 'bar';
+ $foo ? 'bar' : $foo;

Coalesce mutator

Mutates:

$foo = 'foo';
$bar = 'bar';
- $foo ?? $bar;
+ $bar ?? $foo;

Or more complex case with nested values:

$foo = 'foo';
$bar = 'bar';
$baz = 'baz';

- $foo ?? $bar ?? $baz;
+ $foo ?? $baz ?? $bar;

- $foo ?? $bar ?? $baz;
+ $bar ?? $foo ?? $baz;

UnwrapSubstr mutator

- $x = substr('abcde', 0, -1);
+ $x = 'abcde';

UnwrapStrRev mutator

- $x = strrev('Hello!');
+ $x = 'Hello!';

UnwrapLtrim mutator

- $x = ltrim(' Hello!');
+ $x = ' Hello!';

UnwrapRtrim mutator

- $x = rtrim('Hello! ');
+ $x = 'Hello! ';

UnwrapStrIreplace mutator

- $x = str_ireplace('%body%', 'black', '<body text=%BODY%>');
+ $x = '<body text=%BODY%>';

UnwrapStrShuffle mutator

- $x = str_shuffle('Hello!');
+ $x = 'Hello!';

Enjoy!

Star