Why does Scrutinizer say "duplicate code" when these two methods are totally different? Is this a false-positive or does Scrutinizer indeed want to see this in a more abstract kind of way?
Why does Scrutinizer say "duplicate code" when code is totally different?
390 views Asked by Sliq At
2
My guess is that they do what is called "normalization", i.e. the text is split into smaller parts (called tokens) and then some of these tokens are replaced with different text to make them all the same. For example, all numbers and strings are normalized to be the same number/string.
This makes sure that you can find clones that only differ in literals, which is helpful, because it usually means that you can extract a utility method that takes these differing literals as a parameter and thus reduce the redundancy in your code.
So to the clone detector, your code would then look something like this (all uppercase text is normalized):
Both functions would be normalized to this exact same representation and the clone detector will then pick this up as duplicated code.
The only sensible refactoring I can see for your code would be to extract a helper function that handles preparing and executing the query and returning the row count:
This might make sense if you have many different queries that are only interested in the row count.