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
Ignore code coverage of private constructors of static classes #3148
Conversation
I really don't like production code polluted with similar annotations to hack test suites, coverage etc. :/ |
Calling this method will most likely not make its code covered. Besides that, it's even more hacky in the way that instead of telling PHPUnit "I don't want to test this code because we're relying on a PHP language feature", we're trying to test the language feature itself. We don't write tests for type hints or other non-public class members. A better solution would be to add these files to coverage blacklist in With that said, annotations seem the least of evils. I agree that we should avoid having such comments if possible (e.g. I didn't add any code style annotations in #3157 and let the build fail which is okay because of our incremental approach). |
I don't see a reason to push our coverage up artificially: what is pushing this kind of patch? What is the gain? Having 100% is surely a good aim, but I'd probably rather have a scenario that actually touches the code in question instead (not applicable here). |
@Ocramius it's not pushing coverage up artificially. As I said, it's an exclusion of the code from being subject to coverage. Until there's another way to define a static class in PHP, we'll have to use a private constructor which cannot be executed by design and this is the only line of code in this class which can be executed in those classes. It's not about 100% coverage, it's about having adequate metrics. |
I'm fine with merging this, I just don't see a strong reason to annotate un-coverable code, heh... |
So there are no objections against writing non-executable code in the first place? Only about the annotations? 😄 |
Correct, it's just about the annotations 🧐 |
3ff56f3
to
088608d
Compare
088608d
to
f399c02
Compare
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.
Should a feature request be done to PHPUnit?
I'd love to see this handled by PHPUnit so that we don't have to do this ourselves but I don't see how PHPUnit would do that. What are the criteria for a method to be automatically excluded from code coverage? Should it be any empty private constructor? Not sure if PHPUnit is capable of detecting an empty method. |
According to Scrutinizer, static classes have 0% code coverage since the only their executable code (constructor) is not executed. But it's not supposed to since it's just a way to prevent class instantiation.
Summary
Static class constructors are marked as
@codeCoverageIgnore
.