-
-
Notifications
You must be signed in to change notification settings - Fork 783
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
Added validation of constructors to LongParameterList #2410
Conversation
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.
Thanks for contributing to detekt. This is a good idea.
I'd put this in a separate rule, since users may want to have different configuration settings for the max parameter number.
Sure, will do.
Mar 5, 2020 20:37:07 M Schalk <notifications@github.com>:
…
@schalkms commented on this pull request.
Thanks for contributing to detekt.
I'd put this in a separate rule, since users may want to have different configuration settings for the max parameter number.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub [#2410] , or unsubscribe [https://github.com/notifications/unsubscribe-auth/ABC6WKTVZCPW5FT2VHBPRATRF7WM3ANCNFSM4LCM2BRA] . [https://github.com/notifications/beacon/ABC6WKUKRNVEJOFI7AT7MFDRF7WM3A5CNFSM4LCM2BRKYY3PNVWWK3TUL52HS4DFWFIHK3DMKJSXC5LFON2FEZLWNFSXPKTDN5WW2ZLOORPWSZGOCYFPEFY.gif]
|
96183ff
to
28726f9
Compare
I'm playing with it for some time and don't like both abstract parent for two rules (pushed above) and code duplication without it. Does adding new configuration parameter like I'm open to any other suggestions, of course. |
For me this PR looks good. Please fix the issues detected issues and regenerate the documentation for detekt's homepage. Then we'll proceed with merging this PR. |
...src/main/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongConstructorParameterList.kt
Outdated
Show resolved
Hide resolved
Both approaches have advantages:
I'm OK with both approaches but would prefer a property |
Why the -1 as default? It doesn't seem like a good default value. If the reason is to avoid "BC" I consider this a bug fix more than a new feature. If we go to |
Thanks for the reference! Proper deprecation and splitting property into two seems much nicer than -1. Hoping to get back to that in the nearest future. |
I don’t think it’s wise to use -1 as a default value. |
28726f9
to
9c69ab9
Compare
I've went with splitting configuration option in two as this was the most discussed option here. I've also added option to ignore data classes as these value bags are a kinda separate thing in my opinion. So one may want to both validate and not validate them to their liking. I guess CONTRIBUTING.md should note that update of both both documentation and Looking forward to your opinions on the PR. |
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.
Thanks! That PR looks awesome!
I have two found one minor improvement regarding the documentation.
Concerning the Contributing
guide, if you find something missing we would be really grateful for a PR that improves the docs. For experienced contributors it's hard to always know which documentation 1st time contributors need.
import org.jetbrains.kotlin.psi.KtNamedFunction | ||
import org.jetbrains.kotlin.psi.KtParameterList | ||
import org.jetbrains.kotlin.psi.KtPrimaryConstructor | ||
import org.jetbrains.kotlin.psi.KtSecondaryConstructor | ||
|
||
/** | ||
* Reports functions which have more parameters than a certain threshold (default: 6). |
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.
* Reports functions which have more parameters than a certain threshold (default: 6). | |
* Reports functions and constructors which have more parameters than a certain threshold. |
validateConstructor(constructor) | ||
} | ||
|
||
private fun <T : KtConstructor<T>> validateConstructor(constructor: KtConstructor<T>) { |
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.
I'm sorry, I don't understand why this function needs generics.
Does the following function declaration also suffice?
private fun <T : KtConstructor<T>> validateConstructor(constructor: KtConstructor<T>) { | |
private fun validateConstructor(constructor: KtFunction) { |
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.
I've just taken least common denominator of two types, which happens to have generic parameter. More generic KtFunction should work, will check.
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.
.getContainingClassOrObject()
is a method of KtConstructor
, so it's not available on KtFunction
, but if star-projected type is okay, that's more than enough for this.
...-rules/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterListSpec.kt
Show resolved
Hide resolved
...-rules/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterListSpec.kt
Outdated
Show resolved
Hide resolved
} | ||
|
||
it("does not report short parameter list for secondary constructors") { | ||
val code = "class LongCtor() { constructor(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) }" |
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.
same here
...-rules/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/complexity/LongParameterListSpec.kt
Outdated
Show resolved
Hide resolved
@r4zzz4k your implemented rule already found a code smell in detekt itself 😉
|
I'm not hundred percent sure about defaults, so it may need some tuning in future. Hoping we would be able to gather some valuable feedback on this in future. Anyway, great that my contribution is useful to someone besides me. Thank you :) And thanks for your hard work on the project, it really changes the way one works on their codebase in a very positive way! |
Please suppress the issue for now so we can move merging this awesome PR :) |
* Validated parameter count of primary and secondary constructors * Added flag to ignore data classes as parameter count of these arguably doesn't highlight violation of SRP * Deprecated threshold in favor of functionThreshold and constructorThreshold * Suppressed found issue in existing code
1f296f3
to
e9f90f7
Compare
Codecov Report
@@ Coverage Diff @@
## master #2410 +/- ##
============================================
- Coverage 83.26% 83.26% -0.01%
- Complexity 2198 2204 +6
============================================
Files 360 360
Lines 6240 6250 +10
Branches 1144 1146 +2
============================================
+ Hits 5196 5204 +8
Misses 466 466
- Partials 578 580 +2
Continue to review full report at Codecov.
|
Should I roll back rebasing or this is okay? |
@r4zzz4k this is okay. The failing AppVeyor build is not related to your change. Everything's fine. |
I've looked through existing rules, seems that nothing covers number of parameters in contructors. LongParameterList is the nearest thing, but it covers only methods.
Does this sound like a useful rule? In discussions over the Internet on argument count limit I mostly see costructors included. Checkstyle, for example, covers both methods and parameters, PMD seems to agree on that.
After adding this plugin
./gradlew detekt
starts failing on the project, as some classes (e.g. CorrectableCodeSmell, see below) have 6 or more parameters in constructors.https://github.com/arturbosch/detekt/blob/521e96856d841ec8c115a12f2db8230ce01eda86/detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/CodeSmell.kt#L44-L51
I'll try to collect proper report, as right now I'm having troubles with it (#2411).