buttonmen-dev / buttonmen

Buttonmen - an online dice game
Other
16 stars 24 forks source link

jenkins has many complaints about PHP code #417

Closed cgolubi1 closed 10 years ago

cgolubi1 commented 10 years ago

Current output of a jenkins run on buttonmen-dev master includes:

Obviously, we can't really enforce coding standards using this tool under these conditions. Look at these and figure out which ones are real errors (and fix those) and which ones are a coding standard we like but Jenkins doesn't (and fix jenkins).

cgolubi1 commented 10 years ago

I decided that all the "duplicate code" warnings were legitimate --- three things that i'd looked at previously and said, "ugh, i should fix this", and one of James's which was similar. I fixed those all, and verified that the "duplicated code warnings" sidebar disappears from the page when there are no duplicated code warnings, which is nice from the point of view of making it easy to find things.

cgolubi1 commented 10 years ago

Okay, what about the things that PMD complains about?

Summarizing PMD complaints:

cgolubi1 commented 10 years ago

How about checkstyle? There are a lot of categories here, and we're clearly going to have to customize. Let's start with the right baseline. Here are the formats installed with Jenkins by default. Right now, these give me:

So, of all of those, i think i like the idea of starting with PSR2 and changing some stuff. So, step 1 is to figure out how to do that, which was pretty easy --- we now have a custom ruleset we're committing, which happens to follow the same rules as PSR2 right now. Cool. So, things where i can see that it's right, i'm fixing as i find them.

Here are the highest-hitcount things where i'm not sure what to do without talking to James:

Found: it expects "true", "false", and "null" to be lowercase. Well, that's what i do most of the time (though i'm not consistent), so i'm fine with it. James, what do you want to do?

CloseBracketLine: if you have a multi-line function call, it wants the closing paren to be on a line by itself, e.g. it's complaining about:

    setcookie(session_name(), '', time()-3600,
              $params["path"], $params["domain"],
              $params["secure"], $params["httponly"]);

and many others. I'm fine with this either way --- i'm happy to do what it wants here, or i'm happy to try to change it if we understand what we'd like better.

NotCamelCaps: it thinks all our class and method names should be camelcase. I think we're intentionally not doing that, but i can't necessarily articulate what our class/method name standard is.

Indent: it doesn't like

    setcookie(session_name(), '', time()-3600,
              $params["path"], $params["domain"],
              $params["secure"], $params["httponly"]);

It wants us to instead do:

    setcookie(session_name(), '', time()-3600,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]);

(i.e. four indents, not indenting to the open brace). I used the same example as i did for CloseBracketLine because i think these are in fact related, in that i'd be pretty happy with:

    setcookie(
        session_name(), '', time()-3600,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );

which i think would resolve both complaints.

Ah, that change would also resolve ContentAfterOpenBracket. No points for guessing what that one wants. :>)

Ah, one more --- it really wants us to do:

    setcookie(
        session_name(),
        '',
        time()-3600,
        $params["path"],
        $params["domain"],
        $params["secure"],
        $params["httponly"]
    );

and that one would resolve MultipleArguments. I'm fine with all that if you are, and can suppress any of them instead if you're not.

NotAllowed: it doesn't like "else if" (fine, we usually use "elseif", so i fixed the couple of errors), but more importantly, it thinks PHP files may not end in ?>. Hmm. What do we know?

ControlStructures,ContentBefore: it doesn't like a brace followed by more code on the same line, e.g.:

        if (count($helpers) == 0) { return array($helpMin, $helpMax); }

I don't really like it either, and think we should fix those. They look like old code anyway. But i want to double-check with you first.

TooLong: PSR2 complains at 120 characters. I like that --- i think it's a good idea not to have arbitrarily-long lines, and in fact very few of our lines are not in compliance with 120 characters. I fixed a bunch in dummy_responder that i'd just been too lazy to fix ealier, and will double-check with you before addressing the BMGame ones.

OneParamPerLine,ContentAfterComma,FirstParamSpacing: this is for multi-line function declarations (not invocations) and only affects BMInterface --- it wants us to put one parameter per line for a multi-line function declaration, and to put the first parameter on its own line, not with the function. What do you think?

FoundWithSymbols: You're going to have to translate these for me --- it's complaining about any file containing both class declarations and other code, and sees this in dummy_responder, responder, BMInterface, and BMDie. Let's discuss.

Whew. Okay, well, that was a long exercise, but at the end of it, we're down under 1000 checkstyle errors, and i think i understand the conversations we need to have to decide what to do about almost all of them.

cgolubi1 commented 10 years ago

Notes from discussion with James:

Stuff in PMD:

cgolubi1 commented 10 years ago

Okay, i did a number of fixes. In fact, i opted to go ahead and almost fully fix checkstyle. Six errors remain, which i think we can probably defer/ignore for some time:

Almost all of the PMD errors still remain --- James, could you take care of ShortVariable, UnusedFormalParameter, UnusedLocalVariable, and UnusedPrivateField? I'm still on the hook to spec out a cyclomatic complexity improvement.

I'm going to go ahead and put in a pull request with what i've got right now, because i think it's a significant improvement and also it touches a lot of code, so i'd like to get it in the queue before too many other things change.

blackshadowshade commented 10 years ago

UnusedPrivateField warnings cannot be removed. This is a problem that has arisen from PHP encapsulation and enforcement of getters and setters. Our private fields are actually almost all accessible as if they were public, only they have defined get and set behaviour.

blackshadowshade commented 10 years ago

@cgolubi1, here are the tweaks that I would like to make to the Jenkins PMD checking:

cgolubi1 commented 10 years ago

Anything magic about 13? That is, if something pops up with a cyclomatic complexity of 14, will we want to debug it, or raise the threshold again? (Either answer is fine, just curious.)

blackshadowshade commented 10 years ago

Actually, yes, there is something magic about 13.

The general guidelines for cyclomatic complexity suggest that 10 is a warning sign, and 15 is an absolute limit.

I tend to use two or three up on input parameter validation, and I don't really consider those to be part of the main function logic.

Thus, we have 10 + 2 allowed, and 13 should starts signalling again.

blackshadowshade commented 10 years ago

Add NumberOfChildren to the PMD warnings that we want to ignore.

blackshadowshade commented 10 years ago

I'll use my 'bug fix of the month' for April on this issue. I know I've mentioned it on IRC and it's already on Chaos' to-do list, but I figure that this is the one issue that is potentially affecting my ability to develop shinier code. :)

cgolubi1 commented 10 years ago

Sounds good. (Everyone else still has one more day of March to complete those games, since we're counting this in U.S. time. I'll send e-mail in the morning to the buttonmen-dev list about who is owed bugfixes.)

cgolubi1 commented 10 years ago

For future reference: http://phpmd.org/documentation/creating-a-ruleset.html explains how to create a PMD ruleset that imports and customizes existing rulesets.

cgolubi1 commented 10 years ago

Okay, i've almost got this (see http://jenkins.buttonweavers.com:8080/job/buttonmen-cgolubi1/208/), and i think my changes correctly addressed:

However, ExcessiveClassLength doesn't actually match any warnings (see http://jenkins.buttonweavers.com:8080/job/buttonmen-cgolubi1/208/pmdResult/fixed/ for the absence of that string). Did you mean to ask me to exclude ExcessiveClassComplexity instead?

blackshadowshade commented 10 years ago

Yeah, that's the one. ExcessiveClassComplexity.

blackshadowshade commented 10 years ago

Removed bugfix-of-the-month label on this because Chaos has done her part.

cgolubi1 commented 10 years ago

I'm going to use my bugfix-of-the-month for this, in particular for getting the PMD warning count down to zero.