Skip to content
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

Merged
merged 1 commit into from Jul 23, 2020

Conversation

morozov
Copy link
Member

@morozov morozov commented May 13, 2018

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.

Q A
Type improvement
BC Break no
Fixed issues none

Summary

Static class constructors are marked as @codeCoverageIgnore.

@morozov morozov requested a review from Majkl578 May 13, 2018 19:30
@morozov morozov added this to the 2.8.0 milestone May 15, 2018
@morozov morozov self-assigned this May 17, 2018
@Majkl578
Copy link
Contributor

Majkl578 commented May 21, 2018

I really don't like production code polluted with similar annotations to hack test suites, coverage etc. :/
How about we add a test to cover these? Simply trying to instantiate these classes and expecting Error? Possibly also asserting that message matches ^Call to private \S+::__construct\(\) from invalid context\z.

@morozov
Copy link
Member Author

morozov commented May 21, 2018

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 phpunit.xml but it would exclude entire files (not only methods) and would require copying this blacklist across all phpunit.xml replicas.

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).

@Ocramius
Copy link
Member

Ocramius commented May 21, 2018

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).

@morozov
Copy link
Member Author

morozov commented May 21, 2018

@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.

@Ocramius
Copy link
Member

I'm fine with merging this, I just don't see a strong reason to annotate un-coverable code, heh...

@morozov
Copy link
Member Author

morozov commented May 21, 2018

So there are no objections against writing non-executable code in the first place? Only about the annotations? 😄

@Ocramius
Copy link
Member

Correct, it's just about the annotations 🧐

@morozov morozov closed this Jun 12, 2018
@morozov morozov deleted the ignore-private-constructors branch June 12, 2018 05:19
@Majkl578 Majkl578 removed this from the 2.8.0 milestone Jun 14, 2018
@morozov morozov restored the ignore-private-constructors branch June 3, 2019 11:35
@morozov morozov deleted the ignore-private-constructors branch June 3, 2019 11:51
@morozov morozov restored the ignore-private-constructors branch July 23, 2020 16:33
@morozov morozov reopened this Jul 23, 2020
@morozov morozov changed the base branch from master to 2.10.x July 23, 2020 16:38
@morozov morozov force-pushed the ignore-private-constructors branch from 3ff56f3 to 088608d Compare July 23, 2020 16:38
@morozov morozov requested review from greg0ire and removed request for Majkl578 July 23, 2020 17:02
@morozov morozov force-pushed the ignore-private-constructors branch from 088608d to f399c02 Compare July 23, 2020 18:03
Copy link
Member

@greg0ire greg0ire left a 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?

@morozov
Copy link
Member Author

morozov commented Jul 23, 2020

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.

@morozov morozov merged commit 47b5000 into doctrine:2.10.x Jul 23, 2020
@morozov morozov deleted the ignore-private-constructors branch July 23, 2020 19:28
@morozov morozov added this to the 2.10.3 milestone Jul 23, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants