4

I am looking for PHPCodeSniffer rule to limit the maximum number of code lines per function / method.

function something($b) {
    // some comment that should be ignored in the count
    $a = 12;

    $value = sqrt(
        $a * $b
    );

    return $value;
}

I would like the above function to be accounted as having 5 coding lines (not counting the comments an blank lines).

AsTeR
  • 7,247
  • 14
  • 60
  • 99

2 Answers2

4

I think PHPCodeSniffer (phpcs) has no standard rule for "line of code", all I can think of is to either use another metric like McCabe Cyclomatic Complexity (this is included as a Sniff called Generic.Metrics.CyclomaticComplexity). It's not the same, but a high value there is an indicator for messy code.

If you are willing to use another tool called PHP Mess Detector (phpmd), you could use the ExcessiveMethodLength rule to track the maximum length of methods.

Odi
  • 6,916
  • 3
  • 34
  • 52
1

You're looking for sniff in ObjectCalisthenics coding standard.

It can limit 4 metrics

  • line length of class (and traits, interfaces)
  • line length of method (function)
  • property count in class
  • method count in class
Tomas Votruba
  • 23,240
  • 9
  • 79
  • 115