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
Support custom mutators #1686
base: master
Are you sure you want to change the base?
Support custom mutators #1686
Conversation
04f5f36
to
f49a2e7
Compare
Overall, I'm ok with a feature itself, however it is much more complex. Related to issues/comments discussed before
As it was already pointed out in those comments, we should consider the following cases (this list can be extended and should be taken into account/discussed during development of this feature here or in other PRs):
By the way, what is your custom Mutator? Can you please provide an example? I'm thinking about what are your case, probably we will need to create another packages like |
@vss414 This is great! Exactly what I was looking for. Can't wait to get my hands on it :) @maks-rafalko My custom mutator will be to remove some calls while using fluent interfaces - like: $query->select('*')->withTrashed(); so I'd add custom mutator to remove part of that statement: Mutations:
and:
Ofc if we force our developers to not use fluent interfaces then it will be not needed. I had another examples I wanted to use custom mutators, but I forgot now.... Thanks! |
I can maybe add some examples from our codebase that would require support for custom mutators to be more effective. Our codebase is using a custom framework in which we have a custom query language that I would like to be able to mutate. For example querying some database table on a field to be greater than 0 would look like:
It would be nice to be able to mutate the |
f49a2e7
to
442d08f
Compare
I decided to move forward with this one, as it turned out we are very close to make it possible, and there is a need for this from Ondrej (maintainer of PHPStan), so we will have a good first consumer of our API. This will be the first PR, in the following PRs I will extract our extension points to a separate package so that anyone who needs to implement custom mutator will require only small package But I have a problem here, seems like Box/Scoper have a bug or something like that. @theofidry when I add cd tests/e2e/Custom_Mutator
../../../build/infection.phar then I have an error:
which looks weird, as there is a non-scoped interface in the PHAR. UPD: probably, I misunderstood The bad thing that PHP-Parser's |
442d08f
to
727c20d
Compare
- exclude Mutator, Definition, MutatorCategory from being prefixes by Box/Scoper
c8a33e8
to
094ef17
Compare
…nfection` and `infection.phar`
ccbf248
to
484c5a8
Compare
What you likely want there is expose indeed, exclude is to mark a symbol as "native to PHP" (how it is handled is... depends, e.g. you can declare native PHP symbols, for example that is what polyfills are for, or for handling symbols coming from third-party that is not in your vendor for WP plugins).
I don't think that would be possible, as a consumer you want to be able to consume any node no, so it's more than just the node interface? I think for now it's best if you expose the whole |
Invalid FQCN are forbidden
…ig as per comments
Replacing
https://github.com/infection/infection/actions/runs/8953112456/job/24591330644?pr=1686#step:15:339 So for now I will revert it. |
…per config as per comments" This reverts commit 809d8f2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only a glance review; need to do a proper one still
], | ||
'expose-namespaces' => [ | ||
// we have to expose it cause Mutator depends on PhpParser/Node interface, and it's not in a separate package | ||
'/^PhpParser/', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be just the name:
'/^PhpParser/', | |
'PhpParser', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what I started with, but running infection.phar
then fails with:
PHP Fatal error: Uncaught Error: Class "Infection\PhpParser\FileParser" not found in phar:///home/maksrafalko/apps/infection/build/infection.phar/src/Container.php:185
It looks like it matches Infection\PhpParser\FileParser
in the middle
Infection supports only a set of Mutators which are based on AST and PHP-Parser project. In such situation library users can't use their own mutators (which may be useful in their projects but doesn't fit to be a part of the library).
This PR adds the ability to use custom mutators in projects that use the infection library.
Custom mutators should implement
Infection\Mutator\Mutator
interface.To use custom mutators, user must add them to the mutator list in the infection config. User has to use the class name with the namespace of the custom mutator as a name of mutator in the config. An example of using a custom mutator in the infection config: