0ad3bec5de1d7906a42dca7ec339f5ffe446a8eb
[web.mtrack] / inc / CommitCheck / BlankLines.php
1 <?php 
2 require_once 'Interface/CommitListener.php';
3
4 class MTrackCommitCheck_BlankLines implements IMTrackCommitListener 
5 {
6   
7     function vetoCommit($msg, $files, $actions, $checker)
8     {
9         // should only have a list of files which have been updated/added. (not deleted)
10         $ret = array();
11         foreach ($files as $filename) {
12             $pi = pathinfo($filename);
13             switch($pi['extension']) {
14               case 'php':
15               case 'html': 
16                     $fp = $checker->bridge->getFileStream($filename);
17                
18                     $res = $this->checkBlanks($filename, $fp);
19                     if ($res !== true) {
20                         $ret[] = $res;
21                     }
22                     $fp = null; // remove stream.
23           }
24         }
25          
26        return $ret ? implode("\n", $ret) : true;
27     }
28
29     function postCommit($msg, $files, $actions) {
30         return true;
31     }
32     function checkBlanks($filename, $fp)
33     {
34         $pipes = null;
35         $contents = stream_get_contents($fp);
36         $lines = explode("\n",$contents);
37         $seq = 0;
38         $total = 0;
39         foreach($lines as $l) {
40             if (strlen(trim($l))) {
41                 $seq =0;
42                 continue;
43             }
44             // got blannk line
45             $seq++;
46             $total++;
47             if ($seq > 3) {
48                 return "Remove all the extra blank lines from $filename before committing";
49             }
50         }
51         
52         if (($total / count($lines)) > 0.2) {
53             return "More than 20% of $filename is blank lines";
54         }
55          
56         
57         return true;
58     }
59 }