@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return bool |
32 | 32 | */ |
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
34 | 34 | { |
35 | 35 | $filename = str_replace('\\', '\\\\', $report['filename']); |
36 | 36 | $filename = str_replace('"', '\"', $filename); |
@@ -96,10 +96,10 @@ discard block |
||
96 | 96 | $totalErrors, |
97 | 97 | $totalWarnings, |
98 | 98 | $totalFixable, |
99 | - $showSources=false, |
|
100 | - $width=80, |
|
101 | - $interactive=false, |
|
102 | - $toScreen=true |
|
99 | + $showSources = false, |
|
100 | + $width = 80, |
|
101 | + $interactive = false, |
|
102 | + $toScreen = true |
|
103 | 103 | ) { |
104 | 104 | echo '{"totals":{"errors":'.$totalErrors.',"warnings":'.$totalWarnings.',"fixable":'.$totalFixable.'},"files":{'; |
105 | 105 | echo rtrim($cachedData, ','); |
@@ -16,91 +16,91 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Generate a partial report for a single processed file. |
|
21 | - * |
|
22 | - * Function should return TRUE if it printed or stored data about the file |
|
23 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | - * its data should be counted in the grand totals. |
|
25 | - * |
|
26 | - * @param array $report Prepared report data. |
|
27 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | - * @param bool $showSources Show sources? |
|
29 | - * @param int $width Maximum allowed line width. |
|
30 | - * |
|
31 | - * @return bool |
|
32 | - */ |
|
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | - { |
|
35 | - $filename = str_replace('\\', '\\\\', $report['filename']); |
|
36 | - $filename = str_replace('"', '\"', $filename); |
|
37 | - $filename = str_replace('/', '\/', $filename); |
|
38 | - echo '"'.$filename.'":{'; |
|
39 | - echo '"errors":'.$report['errors'].',"warnings":'.$report['warnings'].',"messages":['; |
|
40 | - |
|
41 | - $messages = ''; |
|
42 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
43 | - foreach ($lineErrors as $column => $colErrors) { |
|
44 | - foreach ($colErrors as $error) { |
|
45 | - $error['message'] = str_replace("\n", '\n', $error['message']); |
|
46 | - $error['message'] = str_replace("\r", '\r', $error['message']); |
|
47 | - $error['message'] = str_replace("\t", '\t', $error['message']); |
|
48 | - |
|
49 | - $fixable = false; |
|
50 | - if ($error['fixable'] === true) { |
|
51 | - $fixable = true; |
|
52 | - } |
|
53 | - |
|
54 | - $messagesObject = (object) $error; |
|
55 | - $messagesObject->line = $line; |
|
56 | - $messagesObject->column = $column; |
|
57 | - $messagesObject->fixable = $fixable; |
|
58 | - |
|
59 | - $messages .= json_encode($messagesObject).","; |
|
60 | - } |
|
61 | - } |
|
62 | - }//end foreach |
|
63 | - |
|
64 | - echo rtrim($messages, ','); |
|
65 | - echo ']},'; |
|
66 | - |
|
67 | - return true; |
|
68 | - |
|
69 | - }//end generateFileReport() |
|
70 | - |
|
71 | - |
|
72 | - /** |
|
73 | - * Generates a JSON report. |
|
74 | - * |
|
75 | - * @param string $cachedData Any partial report data that was returned from |
|
76 | - * generateFileReport during the run. |
|
77 | - * @param int $totalFiles Total number of files processed during the run. |
|
78 | - * @param int $totalErrors Total number of errors found during the run. |
|
79 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
80 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
81 | - * @param bool $showSources Show sources? |
|
82 | - * @param int $width Maximum allowed line width. |
|
83 | - * @param bool $interactive Are we running in interactive mode? |
|
84 | - * @param bool $toScreen Is the report being printed to screen? |
|
85 | - * |
|
86 | - * @return void |
|
87 | - */ |
|
88 | - public function generate( |
|
89 | - $cachedData, |
|
90 | - $totalFiles, |
|
91 | - $totalErrors, |
|
92 | - $totalWarnings, |
|
93 | - $totalFixable, |
|
94 | - $showSources=false, |
|
95 | - $width=80, |
|
96 | - $interactive=false, |
|
97 | - $toScreen=true |
|
98 | - ) { |
|
99 | - echo '{"totals":{"errors":'.$totalErrors.',"warnings":'.$totalWarnings.',"fixable":'.$totalFixable.'},"files":{'; |
|
100 | - echo rtrim($cachedData, ','); |
|
101 | - echo "}}".PHP_EOL; |
|
102 | - |
|
103 | - }//end generate() |
|
19 | + /** |
|
20 | + * Generate a partial report for a single processed file. |
|
21 | + * |
|
22 | + * Function should return TRUE if it printed or stored data about the file |
|
23 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | + * its data should be counted in the grand totals. |
|
25 | + * |
|
26 | + * @param array $report Prepared report data. |
|
27 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | + * @param bool $showSources Show sources? |
|
29 | + * @param int $width Maximum allowed line width. |
|
30 | + * |
|
31 | + * @return bool |
|
32 | + */ |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | + { |
|
35 | + $filename = str_replace('\\', '\\\\', $report['filename']); |
|
36 | + $filename = str_replace('"', '\"', $filename); |
|
37 | + $filename = str_replace('/', '\/', $filename); |
|
38 | + echo '"'.$filename.'":{'; |
|
39 | + echo '"errors":'.$report['errors'].',"warnings":'.$report['warnings'].',"messages":['; |
|
40 | + |
|
41 | + $messages = ''; |
|
42 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
43 | + foreach ($lineErrors as $column => $colErrors) { |
|
44 | + foreach ($colErrors as $error) { |
|
45 | + $error['message'] = str_replace("\n", '\n', $error['message']); |
|
46 | + $error['message'] = str_replace("\r", '\r', $error['message']); |
|
47 | + $error['message'] = str_replace("\t", '\t', $error['message']); |
|
48 | + |
|
49 | + $fixable = false; |
|
50 | + if ($error['fixable'] === true) { |
|
51 | + $fixable = true; |
|
52 | + } |
|
53 | + |
|
54 | + $messagesObject = (object) $error; |
|
55 | + $messagesObject->line = $line; |
|
56 | + $messagesObject->column = $column; |
|
57 | + $messagesObject->fixable = $fixable; |
|
58 | + |
|
59 | + $messages .= json_encode($messagesObject).","; |
|
60 | + } |
|
61 | + } |
|
62 | + }//end foreach |
|
63 | + |
|
64 | + echo rtrim($messages, ','); |
|
65 | + echo ']},'; |
|
66 | + |
|
67 | + return true; |
|
68 | + |
|
69 | + }//end generateFileReport() |
|
70 | + |
|
71 | + |
|
72 | + /** |
|
73 | + * Generates a JSON report. |
|
74 | + * |
|
75 | + * @param string $cachedData Any partial report data that was returned from |
|
76 | + * generateFileReport during the run. |
|
77 | + * @param int $totalFiles Total number of files processed during the run. |
|
78 | + * @param int $totalErrors Total number of errors found during the run. |
|
79 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
80 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
81 | + * @param bool $showSources Show sources? |
|
82 | + * @param int $width Maximum allowed line width. |
|
83 | + * @param bool $interactive Are we running in interactive mode? |
|
84 | + * @param bool $toScreen Is the report being printed to screen? |
|
85 | + * |
|
86 | + * @return void |
|
87 | + */ |
|
88 | + public function generate( |
|
89 | + $cachedData, |
|
90 | + $totalFiles, |
|
91 | + $totalErrors, |
|
92 | + $totalWarnings, |
|
93 | + $totalFixable, |
|
94 | + $showSources=false, |
|
95 | + $width=80, |
|
96 | + $interactive=false, |
|
97 | + $toScreen=true |
|
98 | + ) { |
|
99 | + echo '{"totals":{"errors":'.$totalErrors.',"warnings":'.$totalWarnings.',"fixable":'.$totalFixable.'},"files":{'; |
|
100 | + echo rtrim($cachedData, ','); |
|
101 | + echo "}}".PHP_EOL; |
|
102 | + |
|
103 | + }//end generate() |
|
104 | 104 | |
105 | 105 | |
106 | 106 | }//end class |
@@ -15,77 +15,77 @@ |
||
15 | 15 | { |
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * Generate a partial report for a single processed file. |
|
20 | - * |
|
21 | - * Function should return TRUE if it printed or stored data about the file |
|
22 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
23 | - * its data should be counted in the grand totals. |
|
24 | - * |
|
25 | - * @param array $report Prepared report data. |
|
26 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
27 | - * @param bool $showSources Show sources? |
|
28 | - * @param int $width Maximum allowed line width. |
|
29 | - * |
|
30 | - * @return bool |
|
31 | - */ |
|
32 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | - { |
|
34 | - if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
35 | - // Nothing to print. |
|
36 | - return false; |
|
37 | - } |
|
18 | + /** |
|
19 | + * Generate a partial report for a single processed file. |
|
20 | + * |
|
21 | + * Function should return TRUE if it printed or stored data about the file |
|
22 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
23 | + * its data should be counted in the grand totals. |
|
24 | + * |
|
25 | + * @param array $report Prepared report data. |
|
26 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
27 | + * @param bool $showSources Show sources? |
|
28 | + * @param int $width Maximum allowed line width. |
|
29 | + * |
|
30 | + * @return bool |
|
31 | + */ |
|
32 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | + { |
|
34 | + if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
35 | + // Nothing to print. |
|
36 | + return false; |
|
37 | + } |
|
38 | 38 | |
39 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
40 | - foreach ($lineErrors as $column => $colErrors) { |
|
41 | - foreach ($colErrors as $error) { |
|
42 | - $filename = str_replace('"', '\"', $report['filename']); |
|
43 | - $message = str_replace('"', '\"', $error['message']); |
|
44 | - $type = strtolower($error['type']); |
|
45 | - $source = $error['source']; |
|
46 | - $severity = $error['severity']; |
|
47 | - $fixable = (int) $error['fixable']; |
|
48 | - echo "\"$filename\",$line,$column,$type,\"$message\",$source,$severity,$fixable".PHP_EOL; |
|
49 | - } |
|
50 | - } |
|
51 | - } |
|
39 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
40 | + foreach ($lineErrors as $column => $colErrors) { |
|
41 | + foreach ($colErrors as $error) { |
|
42 | + $filename = str_replace('"', '\"', $report['filename']); |
|
43 | + $message = str_replace('"', '\"', $error['message']); |
|
44 | + $type = strtolower($error['type']); |
|
45 | + $source = $error['source']; |
|
46 | + $severity = $error['severity']; |
|
47 | + $fixable = (int) $error['fixable']; |
|
48 | + echo "\"$filename\",$line,$column,$type,\"$message\",$source,$severity,$fixable".PHP_EOL; |
|
49 | + } |
|
50 | + } |
|
51 | + } |
|
52 | 52 | |
53 | - return true; |
|
53 | + return true; |
|
54 | 54 | |
55 | - }//end generateFileReport() |
|
55 | + }//end generateFileReport() |
|
56 | 56 | |
57 | 57 | |
58 | - /** |
|
59 | - * Generates a csv report. |
|
60 | - * |
|
61 | - * @param string $cachedData Any partial report data that was returned from |
|
62 | - * generateFileReport during the run. |
|
63 | - * @param int $totalFiles Total number of files processed during the run. |
|
64 | - * @param int $totalErrors Total number of errors found during the run. |
|
65 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
66 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
67 | - * @param bool $showSources Show sources? |
|
68 | - * @param int $width Maximum allowed line width. |
|
69 | - * @param bool $interactive Are we running in interactive mode? |
|
70 | - * @param bool $toScreen Is the report being printed to screen? |
|
71 | - * |
|
72 | - * @return void |
|
73 | - */ |
|
74 | - public function generate( |
|
75 | - $cachedData, |
|
76 | - $totalFiles, |
|
77 | - $totalErrors, |
|
78 | - $totalWarnings, |
|
79 | - $totalFixable, |
|
80 | - $showSources=false, |
|
81 | - $width=80, |
|
82 | - $interactive=false, |
|
83 | - $toScreen=true |
|
84 | - ) { |
|
85 | - echo 'File,Line,Column,Type,Message,Source,Severity,Fixable'.PHP_EOL; |
|
86 | - echo $cachedData; |
|
58 | + /** |
|
59 | + * Generates a csv report. |
|
60 | + * |
|
61 | + * @param string $cachedData Any partial report data that was returned from |
|
62 | + * generateFileReport during the run. |
|
63 | + * @param int $totalFiles Total number of files processed during the run. |
|
64 | + * @param int $totalErrors Total number of errors found during the run. |
|
65 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
66 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
67 | + * @param bool $showSources Show sources? |
|
68 | + * @param int $width Maximum allowed line width. |
|
69 | + * @param bool $interactive Are we running in interactive mode? |
|
70 | + * @param bool $toScreen Is the report being printed to screen? |
|
71 | + * |
|
72 | + * @return void |
|
73 | + */ |
|
74 | + public function generate( |
|
75 | + $cachedData, |
|
76 | + $totalFiles, |
|
77 | + $totalErrors, |
|
78 | + $totalWarnings, |
|
79 | + $totalFixable, |
|
80 | + $showSources=false, |
|
81 | + $width=80, |
|
82 | + $interactive=false, |
|
83 | + $toScreen=true |
|
84 | + ) { |
|
85 | + echo 'File,Line,Column,Type,Message,Source,Severity,Fixable'.PHP_EOL; |
|
86 | + echo $cachedData; |
|
87 | 87 | |
88 | - }//end generate() |
|
88 | + }//end generate() |
|
89 | 89 | |
90 | 90 | |
91 | 91 | }//end class |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | * |
30 | 30 | * @return bool |
31 | 31 | */ |
32 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
32 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
33 | 33 | { |
34 | 34 | if ($report['errors'] === 0 && $report['warnings'] === 0) { |
35 | 35 | // Nothing to print. |
@@ -77,10 +77,10 @@ discard block |
||
77 | 77 | $totalErrors, |
78 | 78 | $totalWarnings, |
79 | 79 | $totalFixable, |
80 | - $showSources=false, |
|
81 | - $width=80, |
|
82 | - $interactive=false, |
|
83 | - $toScreen=true |
|
80 | + $showSources = false, |
|
81 | + $width = 80, |
|
82 | + $interactive = false, |
|
83 | + $toScreen = true |
|
84 | 84 | ) { |
85 | 85 | echo 'File,Line,Column,Type,Message,Source,Severity,Fixable'.PHP_EOL; |
86 | 86 | echo $cachedData; |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return bool |
32 | 32 | */ |
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
34 | 34 | { |
35 | 35 | if ($report['errors'] === 0 && $report['warnings'] === 0) { |
36 | 36 | // Nothing to print. |
@@ -341,10 +341,10 @@ discard block |
||
341 | 341 | $totalErrors, |
342 | 342 | $totalWarnings, |
343 | 343 | $totalFixable, |
344 | - $showSources=false, |
|
345 | - $width=80, |
|
346 | - $interactive=false, |
|
347 | - $toScreen=true |
|
344 | + $showSources = false, |
|
345 | + $width = 80, |
|
346 | + $interactive = false, |
|
347 | + $toScreen = true |
|
348 | 348 | ) { |
349 | 349 | if ($cachedData === '') { |
350 | 350 | return; |
@@ -16,347 +16,347 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Generate a partial report for a single processed file. |
|
21 | - * |
|
22 | - * Function should return TRUE if it printed or stored data about the file |
|
23 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | - * its data should be counted in the grand totals. |
|
25 | - * |
|
26 | - * @param array $report Prepared report data. |
|
27 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | - * @param bool $showSources Show sources? |
|
29 | - * @param int $width Maximum allowed line width. |
|
30 | - * |
|
31 | - * @return bool |
|
32 | - */ |
|
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | - { |
|
35 | - if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
36 | - // Nothing to print. |
|
37 | - return false; |
|
38 | - } |
|
39 | - |
|
40 | - // How many lines to show about and below the error line. |
|
41 | - $surroundingLines = 2; |
|
42 | - |
|
43 | - $file = $report['filename']; |
|
44 | - $tokens = $phpcsFile->getTokens(); |
|
45 | - if (empty($tokens) === true) { |
|
46 | - if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
47 | - $startTime = microtime(true); |
|
48 | - echo 'CODE report is parsing '.basename($file).' '; |
|
49 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
50 | - echo "CODE report is forcing parse of $file".PHP_EOL; |
|
51 | - } |
|
52 | - |
|
53 | - try { |
|
54 | - $phpcsFile->parse(); |
|
55 | - } catch (\Exception $e) { |
|
56 | - // This is a second parse, so ignore exceptions. |
|
57 | - // They would have been added to the file's error list already. |
|
58 | - } |
|
59 | - |
|
60 | - if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
61 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
62 | - if ($timeTaken < 1000) { |
|
63 | - $timeTaken = round($timeTaken); |
|
64 | - echo "DONE in {$timeTaken}ms"; |
|
65 | - } else { |
|
66 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
67 | - echo "DONE in $timeTaken secs"; |
|
68 | - } |
|
69 | - |
|
70 | - echo PHP_EOL; |
|
71 | - } |
|
72 | - |
|
73 | - $tokens = $phpcsFile->getTokens(); |
|
74 | - }//end if |
|
75 | - |
|
76 | - // Create an array that maps lines to the first token on the line. |
|
77 | - $lineTokens = []; |
|
78 | - $lastLine = 0; |
|
79 | - $stackPtr = 0; |
|
80 | - foreach ($tokens as $stackPtr => $token) { |
|
81 | - if ($token['line'] !== $lastLine) { |
|
82 | - if ($lastLine > 0) { |
|
83 | - $lineTokens[$lastLine]['end'] = ($stackPtr - 1); |
|
84 | - } |
|
85 | - |
|
86 | - $lastLine++; |
|
87 | - $lineTokens[$lastLine] = [ |
|
88 | - 'start' => $stackPtr, |
|
89 | - 'end' => null, |
|
90 | - ]; |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - // Make sure the last token in the file sits on an imaginary |
|
95 | - // last line so it is easier to generate code snippets at the |
|
96 | - // end of the file. |
|
97 | - $lineTokens[$lastLine]['end'] = $stackPtr; |
|
98 | - |
|
99 | - // Determine the longest code line we will be showing. |
|
100 | - $maxSnippetLength = 0; |
|
101 | - $eolLen = strlen($phpcsFile->eolChar); |
|
102 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
103 | - $startLine = max(($line - $surroundingLines), 1); |
|
104 | - $endLine = min(($line + $surroundingLines), $lastLine); |
|
105 | - |
|
106 | - $maxLineNumLength = strlen($endLine); |
|
107 | - |
|
108 | - for ($i = $startLine; $i <= $endLine; $i++) { |
|
109 | - if ($i === 1) { |
|
110 | - continue; |
|
111 | - } |
|
112 | - |
|
113 | - $lineLength = ($tokens[($lineTokens[$i]['start'] - 1)]['column'] + $tokens[($lineTokens[$i]['start'] - 1)]['length'] - $eolLen); |
|
114 | - $maxSnippetLength = max($lineLength, $maxSnippetLength); |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - $maxSnippetLength += ($maxLineNumLength + 8); |
|
119 | - |
|
120 | - // Determine the longest error message we will be showing. |
|
121 | - $maxErrorLength = 0; |
|
122 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
123 | - foreach ($lineErrors as $column => $colErrors) { |
|
124 | - foreach ($colErrors as $error) { |
|
125 | - $length = strlen($error['message']); |
|
126 | - if ($showSources === true) { |
|
127 | - $length += (strlen($error['source']) + 3); |
|
128 | - } |
|
129 | - |
|
130 | - $maxErrorLength = max($maxErrorLength, ($length + 1)); |
|
131 | - } |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - // The padding that all lines will require that are printing an error message overflow. |
|
136 | - if ($report['warnings'] > 0) { |
|
137 | - $typeLength = 7; |
|
138 | - } else { |
|
139 | - $typeLength = 5; |
|
140 | - } |
|
141 | - |
|
142 | - $errorPadding = str_repeat(' ', ($maxLineNumLength + 7)); |
|
143 | - $errorPadding .= str_repeat(' ', $typeLength); |
|
144 | - $errorPadding .= ' '; |
|
145 | - if ($report['fixable'] > 0) { |
|
146 | - $errorPadding .= ' '; |
|
147 | - } |
|
148 | - |
|
149 | - $errorPaddingLength = strlen($errorPadding); |
|
150 | - |
|
151 | - // The maximum amount of space an error message can use. |
|
152 | - $maxErrorSpace = ($width - $errorPaddingLength); |
|
153 | - if ($showSources === true) { |
|
154 | - // Account for the chars used to print colors. |
|
155 | - $maxErrorSpace += 8; |
|
156 | - } |
|
157 | - |
|
158 | - // Figure out the max report width we need and can use. |
|
159 | - $fileLength = strlen($file); |
|
160 | - $maxWidth = max(($fileLength + 6), ($maxErrorLength + $errorPaddingLength)); |
|
161 | - $width = max(min($width, $maxWidth), $maxSnippetLength); |
|
162 | - if ($width < 70) { |
|
163 | - $width = 70; |
|
164 | - } |
|
165 | - |
|
166 | - // Print the file header. |
|
167 | - echo PHP_EOL."\033[1mFILE: "; |
|
168 | - if ($fileLength <= ($width - 6)) { |
|
169 | - echo $file; |
|
170 | - } else { |
|
171 | - echo '...'.substr($file, ($fileLength - ($width - 6))); |
|
172 | - } |
|
173 | - |
|
174 | - echo "\033[0m".PHP_EOL; |
|
175 | - echo str_repeat('-', $width).PHP_EOL; |
|
176 | - |
|
177 | - echo "\033[1m".'FOUND '.$report['errors'].' ERROR'; |
|
178 | - if ($report['errors'] !== 1) { |
|
179 | - echo 'S'; |
|
180 | - } |
|
181 | - |
|
182 | - if ($report['warnings'] > 0) { |
|
183 | - echo ' AND '.$report['warnings'].' WARNING'; |
|
184 | - if ($report['warnings'] !== 1) { |
|
185 | - echo 'S'; |
|
186 | - } |
|
187 | - } |
|
188 | - |
|
189 | - echo ' AFFECTING '.count($report['messages']).' LINE'; |
|
190 | - if (count($report['messages']) !== 1) { |
|
191 | - echo 'S'; |
|
192 | - } |
|
193 | - |
|
194 | - echo "\033[0m".PHP_EOL; |
|
195 | - |
|
196 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
197 | - $startLine = max(($line - $surroundingLines), 1); |
|
198 | - $endLine = min(($line + $surroundingLines), $lastLine); |
|
199 | - |
|
200 | - $snippet = ''; |
|
201 | - if (isset($lineTokens[$startLine]) === true) { |
|
202 | - for ($i = $lineTokens[$startLine]['start']; $i <= $lineTokens[$endLine]['end']; $i++) { |
|
203 | - $snippetLine = $tokens[$i]['line']; |
|
204 | - if ($lineTokens[$snippetLine]['start'] === $i) { |
|
205 | - // Starting a new line. |
|
206 | - if ($snippetLine === $line) { |
|
207 | - $snippet .= "\033[1m".'>> '; |
|
208 | - } else { |
|
209 | - $snippet .= ' '; |
|
210 | - } |
|
211 | - |
|
212 | - $snippet .= str_repeat(' ', ($maxLineNumLength - strlen($snippetLine))); |
|
213 | - $snippet .= $snippetLine.': '; |
|
214 | - if ($snippetLine === $line) { |
|
215 | - $snippet .= "\033[0m"; |
|
216 | - } |
|
217 | - } |
|
218 | - |
|
219 | - if (isset($tokens[$i]['orig_content']) === true) { |
|
220 | - $tokenContent = $tokens[$i]['orig_content']; |
|
221 | - } else { |
|
222 | - $tokenContent = $tokens[$i]['content']; |
|
223 | - } |
|
224 | - |
|
225 | - if (strpos($tokenContent, "\t") !== false) { |
|
226 | - $token = $tokens[$i]; |
|
227 | - $token['content'] = $tokenContent; |
|
228 | - if (stripos(PHP_OS, 'WIN') === 0) { |
|
229 | - $tab = "\000"; |
|
230 | - } else { |
|
231 | - $tab = "\033[30;1m»\033[0m"; |
|
232 | - } |
|
233 | - |
|
234 | - $phpcsFile->tokenizer->replaceTabsInToken($token, $tab, "\000"); |
|
235 | - $tokenContent = $token['content']; |
|
236 | - } |
|
237 | - |
|
238 | - $tokenContent = Util\Common::prepareForOutput($tokenContent, ["\r", "\n", "\t"]); |
|
239 | - $tokenContent = str_replace("\000", ' ', $tokenContent); |
|
240 | - |
|
241 | - $underline = false; |
|
242 | - if ($snippetLine === $line && isset($lineErrors[$tokens[$i]['column']]) === true) { |
|
243 | - $underline = true; |
|
244 | - } |
|
245 | - |
|
246 | - // Underline invisible characters as well. |
|
247 | - if ($underline === true && trim($tokenContent) === '') { |
|
248 | - $snippet .= "\033[4m".' '."\033[0m".$tokenContent; |
|
249 | - } else { |
|
250 | - if ($underline === true) { |
|
251 | - $snippet .= "\033[4m"; |
|
252 | - } |
|
253 | - |
|
254 | - $snippet .= $tokenContent; |
|
255 | - |
|
256 | - if ($underline === true) { |
|
257 | - $snippet .= "\033[0m"; |
|
258 | - } |
|
259 | - } |
|
260 | - }//end for |
|
261 | - }//end if |
|
262 | - |
|
263 | - echo str_repeat('-', $width).PHP_EOL; |
|
264 | - |
|
265 | - foreach ($lineErrors as $column => $colErrors) { |
|
266 | - foreach ($colErrors as $error) { |
|
267 | - $padding = ($maxLineNumLength - strlen($line)); |
|
268 | - echo 'LINE '.str_repeat(' ', $padding).$line.': '; |
|
269 | - |
|
270 | - if ($error['type'] === 'ERROR') { |
|
271 | - echo "\033[31mERROR\033[0m"; |
|
272 | - if ($report['warnings'] > 0) { |
|
273 | - echo ' '; |
|
274 | - } |
|
275 | - } else { |
|
276 | - echo "\033[33mWARNING\033[0m"; |
|
277 | - } |
|
278 | - |
|
279 | - echo ' '; |
|
280 | - if ($report['fixable'] > 0) { |
|
281 | - echo '['; |
|
282 | - if ($error['fixable'] === true) { |
|
283 | - echo 'x'; |
|
284 | - } else { |
|
285 | - echo ' '; |
|
286 | - } |
|
287 | - |
|
288 | - echo '] '; |
|
289 | - } |
|
290 | - |
|
291 | - $message = $error['message']; |
|
292 | - $message = str_replace("\n", "\n".$errorPadding, $message); |
|
293 | - if ($showSources === true) { |
|
294 | - $message = "\033[1m".$message."\033[0m".' ('.$error['source'].')'; |
|
295 | - } |
|
296 | - |
|
297 | - $errorMsg = wordwrap( |
|
298 | - $message, |
|
299 | - $maxErrorSpace, |
|
300 | - PHP_EOL.$errorPadding |
|
301 | - ); |
|
302 | - |
|
303 | - echo $errorMsg.PHP_EOL; |
|
304 | - }//end foreach |
|
305 | - }//end foreach |
|
306 | - |
|
307 | - echo str_repeat('-', $width).PHP_EOL; |
|
308 | - echo rtrim($snippet).PHP_EOL; |
|
309 | - }//end foreach |
|
310 | - |
|
311 | - echo str_repeat('-', $width).PHP_EOL; |
|
312 | - if ($report['fixable'] > 0) { |
|
313 | - echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL; |
|
314 | - echo str_repeat('-', $width).PHP_EOL; |
|
315 | - } |
|
316 | - |
|
317 | - return true; |
|
318 | - |
|
319 | - }//end generateFileReport() |
|
320 | - |
|
321 | - |
|
322 | - /** |
|
323 | - * Prints all errors and warnings for each file processed. |
|
324 | - * |
|
325 | - * @param string $cachedData Any partial report data that was returned from |
|
326 | - * generateFileReport during the run. |
|
327 | - * @param int $totalFiles Total number of files processed during the run. |
|
328 | - * @param int $totalErrors Total number of errors found during the run. |
|
329 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
330 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
331 | - * @param bool $showSources Show sources? |
|
332 | - * @param int $width Maximum allowed line width. |
|
333 | - * @param bool $interactive Are we running in interactive mode? |
|
334 | - * @param bool $toScreen Is the report being printed to screen? |
|
335 | - * |
|
336 | - * @return void |
|
337 | - */ |
|
338 | - public function generate( |
|
339 | - $cachedData, |
|
340 | - $totalFiles, |
|
341 | - $totalErrors, |
|
342 | - $totalWarnings, |
|
343 | - $totalFixable, |
|
344 | - $showSources=false, |
|
345 | - $width=80, |
|
346 | - $interactive=false, |
|
347 | - $toScreen=true |
|
348 | - ) { |
|
349 | - if ($cachedData === '') { |
|
350 | - return; |
|
351 | - } |
|
352 | - |
|
353 | - echo $cachedData; |
|
354 | - |
|
355 | - if ($toScreen === true && $interactive === false) { |
|
356 | - Util\Timing::printRunTime(); |
|
357 | - } |
|
358 | - |
|
359 | - }//end generate() |
|
19 | + /** |
|
20 | + * Generate a partial report for a single processed file. |
|
21 | + * |
|
22 | + * Function should return TRUE if it printed or stored data about the file |
|
23 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | + * its data should be counted in the grand totals. |
|
25 | + * |
|
26 | + * @param array $report Prepared report data. |
|
27 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | + * @param bool $showSources Show sources? |
|
29 | + * @param int $width Maximum allowed line width. |
|
30 | + * |
|
31 | + * @return bool |
|
32 | + */ |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | + { |
|
35 | + if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
36 | + // Nothing to print. |
|
37 | + return false; |
|
38 | + } |
|
39 | + |
|
40 | + // How many lines to show about and below the error line. |
|
41 | + $surroundingLines = 2; |
|
42 | + |
|
43 | + $file = $report['filename']; |
|
44 | + $tokens = $phpcsFile->getTokens(); |
|
45 | + if (empty($tokens) === true) { |
|
46 | + if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
47 | + $startTime = microtime(true); |
|
48 | + echo 'CODE report is parsing '.basename($file).' '; |
|
49 | + } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
50 | + echo "CODE report is forcing parse of $file".PHP_EOL; |
|
51 | + } |
|
52 | + |
|
53 | + try { |
|
54 | + $phpcsFile->parse(); |
|
55 | + } catch (\Exception $e) { |
|
56 | + // This is a second parse, so ignore exceptions. |
|
57 | + // They would have been added to the file's error list already. |
|
58 | + } |
|
59 | + |
|
60 | + if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
61 | + $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
62 | + if ($timeTaken < 1000) { |
|
63 | + $timeTaken = round($timeTaken); |
|
64 | + echo "DONE in {$timeTaken}ms"; |
|
65 | + } else { |
|
66 | + $timeTaken = round(($timeTaken / 1000), 2); |
|
67 | + echo "DONE in $timeTaken secs"; |
|
68 | + } |
|
69 | + |
|
70 | + echo PHP_EOL; |
|
71 | + } |
|
72 | + |
|
73 | + $tokens = $phpcsFile->getTokens(); |
|
74 | + }//end if |
|
75 | + |
|
76 | + // Create an array that maps lines to the first token on the line. |
|
77 | + $lineTokens = []; |
|
78 | + $lastLine = 0; |
|
79 | + $stackPtr = 0; |
|
80 | + foreach ($tokens as $stackPtr => $token) { |
|
81 | + if ($token['line'] !== $lastLine) { |
|
82 | + if ($lastLine > 0) { |
|
83 | + $lineTokens[$lastLine]['end'] = ($stackPtr - 1); |
|
84 | + } |
|
85 | + |
|
86 | + $lastLine++; |
|
87 | + $lineTokens[$lastLine] = [ |
|
88 | + 'start' => $stackPtr, |
|
89 | + 'end' => null, |
|
90 | + ]; |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + // Make sure the last token in the file sits on an imaginary |
|
95 | + // last line so it is easier to generate code snippets at the |
|
96 | + // end of the file. |
|
97 | + $lineTokens[$lastLine]['end'] = $stackPtr; |
|
98 | + |
|
99 | + // Determine the longest code line we will be showing. |
|
100 | + $maxSnippetLength = 0; |
|
101 | + $eolLen = strlen($phpcsFile->eolChar); |
|
102 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
103 | + $startLine = max(($line - $surroundingLines), 1); |
|
104 | + $endLine = min(($line + $surroundingLines), $lastLine); |
|
105 | + |
|
106 | + $maxLineNumLength = strlen($endLine); |
|
107 | + |
|
108 | + for ($i = $startLine; $i <= $endLine; $i++) { |
|
109 | + if ($i === 1) { |
|
110 | + continue; |
|
111 | + } |
|
112 | + |
|
113 | + $lineLength = ($tokens[($lineTokens[$i]['start'] - 1)]['column'] + $tokens[($lineTokens[$i]['start'] - 1)]['length'] - $eolLen); |
|
114 | + $maxSnippetLength = max($lineLength, $maxSnippetLength); |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + $maxSnippetLength += ($maxLineNumLength + 8); |
|
119 | + |
|
120 | + // Determine the longest error message we will be showing. |
|
121 | + $maxErrorLength = 0; |
|
122 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
123 | + foreach ($lineErrors as $column => $colErrors) { |
|
124 | + foreach ($colErrors as $error) { |
|
125 | + $length = strlen($error['message']); |
|
126 | + if ($showSources === true) { |
|
127 | + $length += (strlen($error['source']) + 3); |
|
128 | + } |
|
129 | + |
|
130 | + $maxErrorLength = max($maxErrorLength, ($length + 1)); |
|
131 | + } |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + // The padding that all lines will require that are printing an error message overflow. |
|
136 | + if ($report['warnings'] > 0) { |
|
137 | + $typeLength = 7; |
|
138 | + } else { |
|
139 | + $typeLength = 5; |
|
140 | + } |
|
141 | + |
|
142 | + $errorPadding = str_repeat(' ', ($maxLineNumLength + 7)); |
|
143 | + $errorPadding .= str_repeat(' ', $typeLength); |
|
144 | + $errorPadding .= ' '; |
|
145 | + if ($report['fixable'] > 0) { |
|
146 | + $errorPadding .= ' '; |
|
147 | + } |
|
148 | + |
|
149 | + $errorPaddingLength = strlen($errorPadding); |
|
150 | + |
|
151 | + // The maximum amount of space an error message can use. |
|
152 | + $maxErrorSpace = ($width - $errorPaddingLength); |
|
153 | + if ($showSources === true) { |
|
154 | + // Account for the chars used to print colors. |
|
155 | + $maxErrorSpace += 8; |
|
156 | + } |
|
157 | + |
|
158 | + // Figure out the max report width we need and can use. |
|
159 | + $fileLength = strlen($file); |
|
160 | + $maxWidth = max(($fileLength + 6), ($maxErrorLength + $errorPaddingLength)); |
|
161 | + $width = max(min($width, $maxWidth), $maxSnippetLength); |
|
162 | + if ($width < 70) { |
|
163 | + $width = 70; |
|
164 | + } |
|
165 | + |
|
166 | + // Print the file header. |
|
167 | + echo PHP_EOL."\033[1mFILE: "; |
|
168 | + if ($fileLength <= ($width - 6)) { |
|
169 | + echo $file; |
|
170 | + } else { |
|
171 | + echo '...'.substr($file, ($fileLength - ($width - 6))); |
|
172 | + } |
|
173 | + |
|
174 | + echo "\033[0m".PHP_EOL; |
|
175 | + echo str_repeat('-', $width).PHP_EOL; |
|
176 | + |
|
177 | + echo "\033[1m".'FOUND '.$report['errors'].' ERROR'; |
|
178 | + if ($report['errors'] !== 1) { |
|
179 | + echo 'S'; |
|
180 | + } |
|
181 | + |
|
182 | + if ($report['warnings'] > 0) { |
|
183 | + echo ' AND '.$report['warnings'].' WARNING'; |
|
184 | + if ($report['warnings'] !== 1) { |
|
185 | + echo 'S'; |
|
186 | + } |
|
187 | + } |
|
188 | + |
|
189 | + echo ' AFFECTING '.count($report['messages']).' LINE'; |
|
190 | + if (count($report['messages']) !== 1) { |
|
191 | + echo 'S'; |
|
192 | + } |
|
193 | + |
|
194 | + echo "\033[0m".PHP_EOL; |
|
195 | + |
|
196 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
197 | + $startLine = max(($line - $surroundingLines), 1); |
|
198 | + $endLine = min(($line + $surroundingLines), $lastLine); |
|
199 | + |
|
200 | + $snippet = ''; |
|
201 | + if (isset($lineTokens[$startLine]) === true) { |
|
202 | + for ($i = $lineTokens[$startLine]['start']; $i <= $lineTokens[$endLine]['end']; $i++) { |
|
203 | + $snippetLine = $tokens[$i]['line']; |
|
204 | + if ($lineTokens[$snippetLine]['start'] === $i) { |
|
205 | + // Starting a new line. |
|
206 | + if ($snippetLine === $line) { |
|
207 | + $snippet .= "\033[1m".'>> '; |
|
208 | + } else { |
|
209 | + $snippet .= ' '; |
|
210 | + } |
|
211 | + |
|
212 | + $snippet .= str_repeat(' ', ($maxLineNumLength - strlen($snippetLine))); |
|
213 | + $snippet .= $snippetLine.': '; |
|
214 | + if ($snippetLine === $line) { |
|
215 | + $snippet .= "\033[0m"; |
|
216 | + } |
|
217 | + } |
|
218 | + |
|
219 | + if (isset($tokens[$i]['orig_content']) === true) { |
|
220 | + $tokenContent = $tokens[$i]['orig_content']; |
|
221 | + } else { |
|
222 | + $tokenContent = $tokens[$i]['content']; |
|
223 | + } |
|
224 | + |
|
225 | + if (strpos($tokenContent, "\t") !== false) { |
|
226 | + $token = $tokens[$i]; |
|
227 | + $token['content'] = $tokenContent; |
|
228 | + if (stripos(PHP_OS, 'WIN') === 0) { |
|
229 | + $tab = "\000"; |
|
230 | + } else { |
|
231 | + $tab = "\033[30;1m»\033[0m"; |
|
232 | + } |
|
233 | + |
|
234 | + $phpcsFile->tokenizer->replaceTabsInToken($token, $tab, "\000"); |
|
235 | + $tokenContent = $token['content']; |
|
236 | + } |
|
237 | + |
|
238 | + $tokenContent = Util\Common::prepareForOutput($tokenContent, ["\r", "\n", "\t"]); |
|
239 | + $tokenContent = str_replace("\000", ' ', $tokenContent); |
|
240 | + |
|
241 | + $underline = false; |
|
242 | + if ($snippetLine === $line && isset($lineErrors[$tokens[$i]['column']]) === true) { |
|
243 | + $underline = true; |
|
244 | + } |
|
245 | + |
|
246 | + // Underline invisible characters as well. |
|
247 | + if ($underline === true && trim($tokenContent) === '') { |
|
248 | + $snippet .= "\033[4m".' '."\033[0m".$tokenContent; |
|
249 | + } else { |
|
250 | + if ($underline === true) { |
|
251 | + $snippet .= "\033[4m"; |
|
252 | + } |
|
253 | + |
|
254 | + $snippet .= $tokenContent; |
|
255 | + |
|
256 | + if ($underline === true) { |
|
257 | + $snippet .= "\033[0m"; |
|
258 | + } |
|
259 | + } |
|
260 | + }//end for |
|
261 | + }//end if |
|
262 | + |
|
263 | + echo str_repeat('-', $width).PHP_EOL; |
|
264 | + |
|
265 | + foreach ($lineErrors as $column => $colErrors) { |
|
266 | + foreach ($colErrors as $error) { |
|
267 | + $padding = ($maxLineNumLength - strlen($line)); |
|
268 | + echo 'LINE '.str_repeat(' ', $padding).$line.': '; |
|
269 | + |
|
270 | + if ($error['type'] === 'ERROR') { |
|
271 | + echo "\033[31mERROR\033[0m"; |
|
272 | + if ($report['warnings'] > 0) { |
|
273 | + echo ' '; |
|
274 | + } |
|
275 | + } else { |
|
276 | + echo "\033[33mWARNING\033[0m"; |
|
277 | + } |
|
278 | + |
|
279 | + echo ' '; |
|
280 | + if ($report['fixable'] > 0) { |
|
281 | + echo '['; |
|
282 | + if ($error['fixable'] === true) { |
|
283 | + echo 'x'; |
|
284 | + } else { |
|
285 | + echo ' '; |
|
286 | + } |
|
287 | + |
|
288 | + echo '] '; |
|
289 | + } |
|
290 | + |
|
291 | + $message = $error['message']; |
|
292 | + $message = str_replace("\n", "\n".$errorPadding, $message); |
|
293 | + if ($showSources === true) { |
|
294 | + $message = "\033[1m".$message."\033[0m".' ('.$error['source'].')'; |
|
295 | + } |
|
296 | + |
|
297 | + $errorMsg = wordwrap( |
|
298 | + $message, |
|
299 | + $maxErrorSpace, |
|
300 | + PHP_EOL.$errorPadding |
|
301 | + ); |
|
302 | + |
|
303 | + echo $errorMsg.PHP_EOL; |
|
304 | + }//end foreach |
|
305 | + }//end foreach |
|
306 | + |
|
307 | + echo str_repeat('-', $width).PHP_EOL; |
|
308 | + echo rtrim($snippet).PHP_EOL; |
|
309 | + }//end foreach |
|
310 | + |
|
311 | + echo str_repeat('-', $width).PHP_EOL; |
|
312 | + if ($report['fixable'] > 0) { |
|
313 | + echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL; |
|
314 | + echo str_repeat('-', $width).PHP_EOL; |
|
315 | + } |
|
316 | + |
|
317 | + return true; |
|
318 | + |
|
319 | + }//end generateFileReport() |
|
320 | + |
|
321 | + |
|
322 | + /** |
|
323 | + * Prints all errors and warnings for each file processed. |
|
324 | + * |
|
325 | + * @param string $cachedData Any partial report data that was returned from |
|
326 | + * generateFileReport during the run. |
|
327 | + * @param int $totalFiles Total number of files processed during the run. |
|
328 | + * @param int $totalErrors Total number of errors found during the run. |
|
329 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
330 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
331 | + * @param bool $showSources Show sources? |
|
332 | + * @param int $width Maximum allowed line width. |
|
333 | + * @param bool $interactive Are we running in interactive mode? |
|
334 | + * @param bool $toScreen Is the report being printed to screen? |
|
335 | + * |
|
336 | + * @return void |
|
337 | + */ |
|
338 | + public function generate( |
|
339 | + $cachedData, |
|
340 | + $totalFiles, |
|
341 | + $totalErrors, |
|
342 | + $totalWarnings, |
|
343 | + $totalFixable, |
|
344 | + $showSources=false, |
|
345 | + $width=80, |
|
346 | + $interactive=false, |
|
347 | + $toScreen=true |
|
348 | + ) { |
|
349 | + if ($cachedData === '') { |
|
350 | + return; |
|
351 | + } |
|
352 | + |
|
353 | + echo $cachedData; |
|
354 | + |
|
355 | + if ($toScreen === true && $interactive === false) { |
|
356 | + Util\Timing::printRunTime(); |
|
357 | + } |
|
358 | + |
|
359 | + }//end generate() |
|
360 | 360 | |
361 | 361 | |
362 | 362 | }//end class |
@@ -16,94 +16,94 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Generate a partial report for a single processed file. |
|
21 | - * |
|
22 | - * Function should return TRUE if it printed or stored data about the file |
|
23 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | - * its data should be counted in the grand totals. |
|
25 | - * |
|
26 | - * @param array $report Prepared report data. |
|
27 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | - * @param bool $showSources Show sources? |
|
29 | - * @param int $width Maximum allowed line width. |
|
30 | - * |
|
31 | - * @return bool |
|
32 | - */ |
|
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | - { |
|
35 | - $out = new \XMLWriter; |
|
36 | - $out->openMemory(); |
|
37 | - $out->setIndent(true); |
|
38 | - |
|
39 | - if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
40 | - // Nothing to print. |
|
41 | - return false; |
|
42 | - } |
|
43 | - |
|
44 | - $out->startElement('file'); |
|
45 | - $out->writeAttribute('name', $report['filename']); |
|
46 | - |
|
47 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
48 | - foreach ($lineErrors as $column => $colErrors) { |
|
49 | - foreach ($colErrors as $error) { |
|
50 | - $error['type'] = strtolower($error['type']); |
|
51 | - if ($phpcsFile->config->encoding !== 'utf-8') { |
|
52 | - $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']); |
|
53 | - } |
|
54 | - |
|
55 | - $out->startElement('error'); |
|
56 | - $out->writeAttribute('line', $line); |
|
57 | - $out->writeAttribute('column', $column); |
|
58 | - $out->writeAttribute('severity', $error['type']); |
|
59 | - $out->writeAttribute('message', $error['message']); |
|
60 | - $out->writeAttribute('source', $error['source']); |
|
61 | - $out->endElement(); |
|
62 | - } |
|
63 | - } |
|
64 | - }//end foreach |
|
65 | - |
|
66 | - $out->endElement(); |
|
67 | - echo $out->flush(); |
|
68 | - |
|
69 | - return true; |
|
70 | - |
|
71 | - }//end generateFileReport() |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * Prints all violations for processed files, in a Checkstyle format. |
|
76 | - * |
|
77 | - * @param string $cachedData Any partial report data that was returned from |
|
78 | - * generateFileReport during the run. |
|
79 | - * @param int $totalFiles Total number of files processed during the run. |
|
80 | - * @param int $totalErrors Total number of errors found during the run. |
|
81 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
82 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
83 | - * @param bool $showSources Show sources? |
|
84 | - * @param int $width Maximum allowed line width. |
|
85 | - * @param bool $interactive Are we running in interactive mode? |
|
86 | - * @param bool $toScreen Is the report being printed to screen? |
|
87 | - * |
|
88 | - * @return void |
|
89 | - */ |
|
90 | - public function generate( |
|
91 | - $cachedData, |
|
92 | - $totalFiles, |
|
93 | - $totalErrors, |
|
94 | - $totalWarnings, |
|
95 | - $totalFixable, |
|
96 | - $showSources=false, |
|
97 | - $width=80, |
|
98 | - $interactive=false, |
|
99 | - $toScreen=true |
|
100 | - ) { |
|
101 | - echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
|
102 | - echo '<checkstyle version="'.Config::VERSION.'">'.PHP_EOL; |
|
103 | - echo $cachedData; |
|
104 | - echo '</checkstyle>'.PHP_EOL; |
|
105 | - |
|
106 | - }//end generate() |
|
19 | + /** |
|
20 | + * Generate a partial report for a single processed file. |
|
21 | + * |
|
22 | + * Function should return TRUE if it printed or stored data about the file |
|
23 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | + * its data should be counted in the grand totals. |
|
25 | + * |
|
26 | + * @param array $report Prepared report data. |
|
27 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | + * @param bool $showSources Show sources? |
|
29 | + * @param int $width Maximum allowed line width. |
|
30 | + * |
|
31 | + * @return bool |
|
32 | + */ |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | + { |
|
35 | + $out = new \XMLWriter; |
|
36 | + $out->openMemory(); |
|
37 | + $out->setIndent(true); |
|
38 | + |
|
39 | + if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
40 | + // Nothing to print. |
|
41 | + return false; |
|
42 | + } |
|
43 | + |
|
44 | + $out->startElement('file'); |
|
45 | + $out->writeAttribute('name', $report['filename']); |
|
46 | + |
|
47 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
48 | + foreach ($lineErrors as $column => $colErrors) { |
|
49 | + foreach ($colErrors as $error) { |
|
50 | + $error['type'] = strtolower($error['type']); |
|
51 | + if ($phpcsFile->config->encoding !== 'utf-8') { |
|
52 | + $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']); |
|
53 | + } |
|
54 | + |
|
55 | + $out->startElement('error'); |
|
56 | + $out->writeAttribute('line', $line); |
|
57 | + $out->writeAttribute('column', $column); |
|
58 | + $out->writeAttribute('severity', $error['type']); |
|
59 | + $out->writeAttribute('message', $error['message']); |
|
60 | + $out->writeAttribute('source', $error['source']); |
|
61 | + $out->endElement(); |
|
62 | + } |
|
63 | + } |
|
64 | + }//end foreach |
|
65 | + |
|
66 | + $out->endElement(); |
|
67 | + echo $out->flush(); |
|
68 | + |
|
69 | + return true; |
|
70 | + |
|
71 | + }//end generateFileReport() |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * Prints all violations for processed files, in a Checkstyle format. |
|
76 | + * |
|
77 | + * @param string $cachedData Any partial report data that was returned from |
|
78 | + * generateFileReport during the run. |
|
79 | + * @param int $totalFiles Total number of files processed during the run. |
|
80 | + * @param int $totalErrors Total number of errors found during the run. |
|
81 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
82 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
83 | + * @param bool $showSources Show sources? |
|
84 | + * @param int $width Maximum allowed line width. |
|
85 | + * @param bool $interactive Are we running in interactive mode? |
|
86 | + * @param bool $toScreen Is the report being printed to screen? |
|
87 | + * |
|
88 | + * @return void |
|
89 | + */ |
|
90 | + public function generate( |
|
91 | + $cachedData, |
|
92 | + $totalFiles, |
|
93 | + $totalErrors, |
|
94 | + $totalWarnings, |
|
95 | + $totalFixable, |
|
96 | + $showSources=false, |
|
97 | + $width=80, |
|
98 | + $interactive=false, |
|
99 | + $toScreen=true |
|
100 | + ) { |
|
101 | + echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
|
102 | + echo '<checkstyle version="'.Config::VERSION.'">'.PHP_EOL; |
|
103 | + echo $cachedData; |
|
104 | + echo '</checkstyle>'.PHP_EOL; |
|
105 | + |
|
106 | + }//end generate() |
|
107 | 107 | |
108 | 108 | |
109 | 109 | }//end class |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return bool |
32 | 32 | */ |
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
34 | 34 | { |
35 | 35 | $out = new \XMLWriter; |
36 | 36 | $out->openMemory(); |
@@ -93,10 +93,10 @@ discard block |
||
93 | 93 | $totalErrors, |
94 | 94 | $totalWarnings, |
95 | 95 | $totalFixable, |
96 | - $showSources=false, |
|
97 | - $width=80, |
|
98 | - $interactive=false, |
|
99 | - $toScreen=true |
|
96 | + $showSources = false, |
|
97 | + $width = 80, |
|
98 | + $interactive = false, |
|
99 | + $toScreen = true |
|
100 | 100 | ) { |
101 | 101 | echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
102 | 102 | echo '<checkstyle version="'.Config::VERSION.'">'.PHP_EOL; |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return bool |
32 | 32 | */ |
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
34 | 34 | { |
35 | 35 | $metrics = $phpcsFile->getMetrics(); |
36 | 36 | foreach ($metrics as $metric => $data) { |
@@ -66,10 +66,10 @@ discard block |
||
66 | 66 | $totalErrors, |
67 | 67 | $totalWarnings, |
68 | 68 | $totalFixable, |
69 | - $showSources=false, |
|
70 | - $width=80, |
|
71 | - $interactive=false, |
|
72 | - $toScreen=true |
|
69 | + $showSources = false, |
|
70 | + $width = 80, |
|
71 | + $interactive = false, |
|
72 | + $toScreen = true |
|
73 | 73 | ) { |
74 | 74 | $lines = explode(PHP_EOL, $cachedData); |
75 | 75 | array_pop($lines); |
@@ -16,157 +16,157 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Generate a partial report for a single processed file. |
|
21 | - * |
|
22 | - * Function should return TRUE if it printed or stored data about the file |
|
23 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | - * its data should be counted in the grand totals. |
|
25 | - * |
|
26 | - * @param array $report Prepared report data. |
|
27 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | - * @param bool $showSources Show sources? |
|
29 | - * @param int $width Maximum allowed line width. |
|
30 | - * |
|
31 | - * @return bool |
|
32 | - */ |
|
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | - { |
|
35 | - $metrics = $phpcsFile->getMetrics(); |
|
36 | - foreach ($metrics as $metric => $data) { |
|
37 | - foreach ($data['values'] as $value => $count) { |
|
38 | - echo "$metric>>$value>>$count".PHP_EOL; |
|
39 | - } |
|
40 | - } |
|
41 | - |
|
42 | - return true; |
|
43 | - |
|
44 | - }//end generateFileReport() |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * Prints the source of all errors and warnings. |
|
49 | - * |
|
50 | - * @param string $cachedData Any partial report data that was returned from |
|
51 | - * generateFileReport during the run. |
|
52 | - * @param int $totalFiles Total number of files processed during the run. |
|
53 | - * @param int $totalErrors Total number of errors found during the run. |
|
54 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
55 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
56 | - * @param bool $showSources Show sources? |
|
57 | - * @param int $width Maximum allowed line width. |
|
58 | - * @param bool $interactive Are we running in interactive mode? |
|
59 | - * @param bool $toScreen Is the report being printed to screen? |
|
60 | - * |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - public function generate( |
|
64 | - $cachedData, |
|
65 | - $totalFiles, |
|
66 | - $totalErrors, |
|
67 | - $totalWarnings, |
|
68 | - $totalFixable, |
|
69 | - $showSources=false, |
|
70 | - $width=80, |
|
71 | - $interactive=false, |
|
72 | - $toScreen=true |
|
73 | - ) { |
|
74 | - $lines = explode(PHP_EOL, $cachedData); |
|
75 | - array_pop($lines); |
|
76 | - |
|
77 | - if (empty($lines) === true) { |
|
78 | - return; |
|
79 | - } |
|
80 | - |
|
81 | - $metrics = []; |
|
82 | - foreach ($lines as $line) { |
|
83 | - $parts = explode('>>', $line); |
|
84 | - $metric = $parts[0]; |
|
85 | - $value = $parts[1]; |
|
86 | - $count = $parts[2]; |
|
87 | - if (isset($metrics[$metric]) === false) { |
|
88 | - $metrics[$metric] = []; |
|
89 | - } |
|
90 | - |
|
91 | - if (isset($metrics[$metric][$value]) === false) { |
|
92 | - $metrics[$metric][$value] = $count; |
|
93 | - } else { |
|
94 | - $metrics[$metric][$value] += $count; |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - ksort($metrics); |
|
99 | - |
|
100 | - echo PHP_EOL."\033[1m".'PHP CODE SNIFFER INFORMATION REPORT'."\033[0m".PHP_EOL; |
|
101 | - echo str_repeat('-', 70).PHP_EOL; |
|
102 | - |
|
103 | - foreach ($metrics as $metric => $values) { |
|
104 | - if (count($values) === 1) { |
|
105 | - $count = reset($values); |
|
106 | - $value = key($values); |
|
107 | - |
|
108 | - echo "$metric: \033[4m$value\033[0m [$count/$count, 100%]".PHP_EOL; |
|
109 | - } else { |
|
110 | - $totalCount = 0; |
|
111 | - $valueWidth = 0; |
|
112 | - foreach ($values as $value => $count) { |
|
113 | - $totalCount += $count; |
|
114 | - $valueWidth = max($valueWidth, strlen($value)); |
|
115 | - } |
|
116 | - |
|
117 | - // Length of the total string, plus however many |
|
118 | - // thousands separators there are. |
|
119 | - $countWidth = strlen($totalCount); |
|
120 | - $thousandSeparatorCount = floor($countWidth / 3); |
|
121 | - $countWidth += $thousandSeparatorCount; |
|
122 | - |
|
123 | - // Account for 'total' line. |
|
124 | - $valueWidth = max(5, $valueWidth); |
|
125 | - |
|
126 | - echo "$metric:".PHP_EOL; |
|
127 | - |
|
128 | - ksort($values, SORT_NATURAL); |
|
129 | - arsort($values); |
|
130 | - |
|
131 | - $percentPrefixWidth = 0; |
|
132 | - $percentWidth = 6; |
|
133 | - foreach ($values as $value => $count) { |
|
134 | - $percent = round(($count / $totalCount * 100), 2); |
|
135 | - $percentPrefix = ''; |
|
136 | - if ($percent === 0.00) { |
|
137 | - $percent = 0.01; |
|
138 | - $percentPrefix = '<'; |
|
139 | - $percentPrefixWidth = 2; |
|
140 | - $percentWidth = 4; |
|
141 | - } |
|
142 | - |
|
143 | - printf( |
|
144 | - "\t%-{$valueWidth}s => %{$countWidth}s (%{$percentPrefixWidth}s%{$percentWidth}.2f%%)".PHP_EOL, |
|
145 | - $value, |
|
146 | - number_format($count), |
|
147 | - $percentPrefix, |
|
148 | - $percent |
|
149 | - ); |
|
150 | - } |
|
151 | - |
|
152 | - echo "\t".str_repeat('-', ($valueWidth + $countWidth + 15)).PHP_EOL; |
|
153 | - printf( |
|
154 | - "\t%-{$valueWidth}s => %{$countWidth}s (100.00%%)".PHP_EOL, |
|
155 | - 'total', |
|
156 | - number_format($totalCount) |
|
157 | - ); |
|
158 | - }//end if |
|
159 | - |
|
160 | - echo PHP_EOL; |
|
161 | - }//end foreach |
|
162 | - |
|
163 | - echo str_repeat('-', 70).PHP_EOL; |
|
164 | - |
|
165 | - if ($toScreen === true && $interactive === false) { |
|
166 | - Timing::printRunTime(); |
|
167 | - } |
|
168 | - |
|
169 | - }//end generate() |
|
19 | + /** |
|
20 | + * Generate a partial report for a single processed file. |
|
21 | + * |
|
22 | + * Function should return TRUE if it printed or stored data about the file |
|
23 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | + * its data should be counted in the grand totals. |
|
25 | + * |
|
26 | + * @param array $report Prepared report data. |
|
27 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | + * @param bool $showSources Show sources? |
|
29 | + * @param int $width Maximum allowed line width. |
|
30 | + * |
|
31 | + * @return bool |
|
32 | + */ |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | + { |
|
35 | + $metrics = $phpcsFile->getMetrics(); |
|
36 | + foreach ($metrics as $metric => $data) { |
|
37 | + foreach ($data['values'] as $value => $count) { |
|
38 | + echo "$metric>>$value>>$count".PHP_EOL; |
|
39 | + } |
|
40 | + } |
|
41 | + |
|
42 | + return true; |
|
43 | + |
|
44 | + }//end generateFileReport() |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * Prints the source of all errors and warnings. |
|
49 | + * |
|
50 | + * @param string $cachedData Any partial report data that was returned from |
|
51 | + * generateFileReport during the run. |
|
52 | + * @param int $totalFiles Total number of files processed during the run. |
|
53 | + * @param int $totalErrors Total number of errors found during the run. |
|
54 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
55 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
56 | + * @param bool $showSources Show sources? |
|
57 | + * @param int $width Maximum allowed line width. |
|
58 | + * @param bool $interactive Are we running in interactive mode? |
|
59 | + * @param bool $toScreen Is the report being printed to screen? |
|
60 | + * |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + public function generate( |
|
64 | + $cachedData, |
|
65 | + $totalFiles, |
|
66 | + $totalErrors, |
|
67 | + $totalWarnings, |
|
68 | + $totalFixable, |
|
69 | + $showSources=false, |
|
70 | + $width=80, |
|
71 | + $interactive=false, |
|
72 | + $toScreen=true |
|
73 | + ) { |
|
74 | + $lines = explode(PHP_EOL, $cachedData); |
|
75 | + array_pop($lines); |
|
76 | + |
|
77 | + if (empty($lines) === true) { |
|
78 | + return; |
|
79 | + } |
|
80 | + |
|
81 | + $metrics = []; |
|
82 | + foreach ($lines as $line) { |
|
83 | + $parts = explode('>>', $line); |
|
84 | + $metric = $parts[0]; |
|
85 | + $value = $parts[1]; |
|
86 | + $count = $parts[2]; |
|
87 | + if (isset($metrics[$metric]) === false) { |
|
88 | + $metrics[$metric] = []; |
|
89 | + } |
|
90 | + |
|
91 | + if (isset($metrics[$metric][$value]) === false) { |
|
92 | + $metrics[$metric][$value] = $count; |
|
93 | + } else { |
|
94 | + $metrics[$metric][$value] += $count; |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + ksort($metrics); |
|
99 | + |
|
100 | + echo PHP_EOL."\033[1m".'PHP CODE SNIFFER INFORMATION REPORT'."\033[0m".PHP_EOL; |
|
101 | + echo str_repeat('-', 70).PHP_EOL; |
|
102 | + |
|
103 | + foreach ($metrics as $metric => $values) { |
|
104 | + if (count($values) === 1) { |
|
105 | + $count = reset($values); |
|
106 | + $value = key($values); |
|
107 | + |
|
108 | + echo "$metric: \033[4m$value\033[0m [$count/$count, 100%]".PHP_EOL; |
|
109 | + } else { |
|
110 | + $totalCount = 0; |
|
111 | + $valueWidth = 0; |
|
112 | + foreach ($values as $value => $count) { |
|
113 | + $totalCount += $count; |
|
114 | + $valueWidth = max($valueWidth, strlen($value)); |
|
115 | + } |
|
116 | + |
|
117 | + // Length of the total string, plus however many |
|
118 | + // thousands separators there are. |
|
119 | + $countWidth = strlen($totalCount); |
|
120 | + $thousandSeparatorCount = floor($countWidth / 3); |
|
121 | + $countWidth += $thousandSeparatorCount; |
|
122 | + |
|
123 | + // Account for 'total' line. |
|
124 | + $valueWidth = max(5, $valueWidth); |
|
125 | + |
|
126 | + echo "$metric:".PHP_EOL; |
|
127 | + |
|
128 | + ksort($values, SORT_NATURAL); |
|
129 | + arsort($values); |
|
130 | + |
|
131 | + $percentPrefixWidth = 0; |
|
132 | + $percentWidth = 6; |
|
133 | + foreach ($values as $value => $count) { |
|
134 | + $percent = round(($count / $totalCount * 100), 2); |
|
135 | + $percentPrefix = ''; |
|
136 | + if ($percent === 0.00) { |
|
137 | + $percent = 0.01; |
|
138 | + $percentPrefix = '<'; |
|
139 | + $percentPrefixWidth = 2; |
|
140 | + $percentWidth = 4; |
|
141 | + } |
|
142 | + |
|
143 | + printf( |
|
144 | + "\t%-{$valueWidth}s => %{$countWidth}s (%{$percentPrefixWidth}s%{$percentWidth}.2f%%)".PHP_EOL, |
|
145 | + $value, |
|
146 | + number_format($count), |
|
147 | + $percentPrefix, |
|
148 | + $percent |
|
149 | + ); |
|
150 | + } |
|
151 | + |
|
152 | + echo "\t".str_repeat('-', ($valueWidth + $countWidth + 15)).PHP_EOL; |
|
153 | + printf( |
|
154 | + "\t%-{$valueWidth}s => %{$countWidth}s (100.00%%)".PHP_EOL, |
|
155 | + 'total', |
|
156 | + number_format($totalCount) |
|
157 | + ); |
|
158 | + }//end if |
|
159 | + |
|
160 | + echo PHP_EOL; |
|
161 | + }//end foreach |
|
162 | + |
|
163 | + echo str_repeat('-', 70).PHP_EOL; |
|
164 | + |
|
165 | + if ($toScreen === true && $interactive === false) { |
|
166 | + Timing::printRunTime(); |
|
167 | + } |
|
168 | + |
|
169 | + }//end generate() |
|
170 | 170 | |
171 | 171 | |
172 | 172 | }//end class |
@@ -16,116 +16,116 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Generate a partial report for a single processed file. |
|
21 | - * |
|
22 | - * Function should return TRUE if it printed or stored data about the file |
|
23 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | - * its data should be counted in the grand totals. |
|
25 | - * |
|
26 | - * @param array $report Prepared report data. |
|
27 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | - * @param bool $showSources Show sources? |
|
29 | - * @param int $width Maximum allowed line width. |
|
30 | - * |
|
31 | - * @return bool |
|
32 | - */ |
|
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | - { |
|
35 | - $errors = $phpcsFile->getFixableCount(); |
|
36 | - if ($errors === 0) { |
|
37 | - return false; |
|
38 | - } |
|
39 | - |
|
40 | - $phpcsFile->disableCaching(); |
|
41 | - $tokens = $phpcsFile->getTokens(); |
|
42 | - if (empty($tokens) === true) { |
|
43 | - if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
44 | - $startTime = microtime(true); |
|
45 | - echo 'DIFF report is parsing '.basename($report['filename']).' '; |
|
46 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
47 | - echo 'DIFF report is forcing parse of '.$report['filename'].PHP_EOL; |
|
48 | - } |
|
49 | - |
|
50 | - $phpcsFile->parse(); |
|
51 | - |
|
52 | - if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
53 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
54 | - if ($timeTaken < 1000) { |
|
55 | - $timeTaken = round($timeTaken); |
|
56 | - echo "DONE in {$timeTaken}ms"; |
|
57 | - } else { |
|
58 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
59 | - echo "DONE in $timeTaken secs"; |
|
60 | - } |
|
61 | - |
|
62 | - echo PHP_EOL; |
|
63 | - } |
|
64 | - |
|
65 | - $phpcsFile->fixer->startFile($phpcsFile); |
|
66 | - }//end if |
|
67 | - |
|
68 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
69 | - ob_end_clean(); |
|
70 | - echo "\t*** START FILE FIXING ***".PHP_EOL; |
|
71 | - } |
|
72 | - |
|
73 | - $fixed = $phpcsFile->fixer->fixFile(); |
|
74 | - |
|
75 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
76 | - echo "\t*** END FILE FIXING ***".PHP_EOL; |
|
77 | - ob_start(); |
|
78 | - } |
|
79 | - |
|
80 | - if ($fixed === false) { |
|
81 | - return false; |
|
82 | - } |
|
83 | - |
|
84 | - $diff = $phpcsFile->fixer->generateDiff(); |
|
85 | - if ($diff === '') { |
|
86 | - // Nothing to print. |
|
87 | - return false; |
|
88 | - } |
|
89 | - |
|
90 | - echo $diff.PHP_EOL; |
|
91 | - return true; |
|
92 | - |
|
93 | - }//end generateFileReport() |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * Prints all errors and warnings for each file processed. |
|
98 | - * |
|
99 | - * @param string $cachedData Any partial report data that was returned from |
|
100 | - * generateFileReport during the run. |
|
101 | - * @param int $totalFiles Total number of files processed during the run. |
|
102 | - * @param int $totalErrors Total number of errors found during the run. |
|
103 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
104 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
105 | - * @param bool $showSources Show sources? |
|
106 | - * @param int $width Maximum allowed line width. |
|
107 | - * @param bool $interactive Are we running in interactive mode? |
|
108 | - * @param bool $toScreen Is the report being printed to screen? |
|
109 | - * |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - public function generate( |
|
113 | - $cachedData, |
|
114 | - $totalFiles, |
|
115 | - $totalErrors, |
|
116 | - $totalWarnings, |
|
117 | - $totalFixable, |
|
118 | - $showSources=false, |
|
119 | - $width=80, |
|
120 | - $interactive=false, |
|
121 | - $toScreen=true |
|
122 | - ) { |
|
123 | - echo $cachedData; |
|
124 | - if ($toScreen === true && $cachedData !== '') { |
|
125 | - echo PHP_EOL; |
|
126 | - } |
|
127 | - |
|
128 | - }//end generate() |
|
19 | + /** |
|
20 | + * Generate a partial report for a single processed file. |
|
21 | + * |
|
22 | + * Function should return TRUE if it printed or stored data about the file |
|
23 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | + * its data should be counted in the grand totals. |
|
25 | + * |
|
26 | + * @param array $report Prepared report data. |
|
27 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | + * @param bool $showSources Show sources? |
|
29 | + * @param int $width Maximum allowed line width. |
|
30 | + * |
|
31 | + * @return bool |
|
32 | + */ |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | + { |
|
35 | + $errors = $phpcsFile->getFixableCount(); |
|
36 | + if ($errors === 0) { |
|
37 | + return false; |
|
38 | + } |
|
39 | + |
|
40 | + $phpcsFile->disableCaching(); |
|
41 | + $tokens = $phpcsFile->getTokens(); |
|
42 | + if (empty($tokens) === true) { |
|
43 | + if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
44 | + $startTime = microtime(true); |
|
45 | + echo 'DIFF report is parsing '.basename($report['filename']).' '; |
|
46 | + } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
47 | + echo 'DIFF report is forcing parse of '.$report['filename'].PHP_EOL; |
|
48 | + } |
|
49 | + |
|
50 | + $phpcsFile->parse(); |
|
51 | + |
|
52 | + if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
53 | + $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
54 | + if ($timeTaken < 1000) { |
|
55 | + $timeTaken = round($timeTaken); |
|
56 | + echo "DONE in {$timeTaken}ms"; |
|
57 | + } else { |
|
58 | + $timeTaken = round(($timeTaken / 1000), 2); |
|
59 | + echo "DONE in $timeTaken secs"; |
|
60 | + } |
|
61 | + |
|
62 | + echo PHP_EOL; |
|
63 | + } |
|
64 | + |
|
65 | + $phpcsFile->fixer->startFile($phpcsFile); |
|
66 | + }//end if |
|
67 | + |
|
68 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
69 | + ob_end_clean(); |
|
70 | + echo "\t*** START FILE FIXING ***".PHP_EOL; |
|
71 | + } |
|
72 | + |
|
73 | + $fixed = $phpcsFile->fixer->fixFile(); |
|
74 | + |
|
75 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
76 | + echo "\t*** END FILE FIXING ***".PHP_EOL; |
|
77 | + ob_start(); |
|
78 | + } |
|
79 | + |
|
80 | + if ($fixed === false) { |
|
81 | + return false; |
|
82 | + } |
|
83 | + |
|
84 | + $diff = $phpcsFile->fixer->generateDiff(); |
|
85 | + if ($diff === '') { |
|
86 | + // Nothing to print. |
|
87 | + return false; |
|
88 | + } |
|
89 | + |
|
90 | + echo $diff.PHP_EOL; |
|
91 | + return true; |
|
92 | + |
|
93 | + }//end generateFileReport() |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * Prints all errors and warnings for each file processed. |
|
98 | + * |
|
99 | + * @param string $cachedData Any partial report data that was returned from |
|
100 | + * generateFileReport during the run. |
|
101 | + * @param int $totalFiles Total number of files processed during the run. |
|
102 | + * @param int $totalErrors Total number of errors found during the run. |
|
103 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
104 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
105 | + * @param bool $showSources Show sources? |
|
106 | + * @param int $width Maximum allowed line width. |
|
107 | + * @param bool $interactive Are we running in interactive mode? |
|
108 | + * @param bool $toScreen Is the report being printed to screen? |
|
109 | + * |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + public function generate( |
|
113 | + $cachedData, |
|
114 | + $totalFiles, |
|
115 | + $totalErrors, |
|
116 | + $totalWarnings, |
|
117 | + $totalFixable, |
|
118 | + $showSources=false, |
|
119 | + $width=80, |
|
120 | + $interactive=false, |
|
121 | + $toScreen=true |
|
122 | + ) { |
|
123 | + echo $cachedData; |
|
124 | + if ($toScreen === true && $cachedData !== '') { |
|
125 | + echo PHP_EOL; |
|
126 | + } |
|
127 | + |
|
128 | + }//end generate() |
|
129 | 129 | |
130 | 130 | |
131 | 131 | }//end class |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return bool |
32 | 32 | */ |
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
34 | 34 | { |
35 | 35 | $errors = $phpcsFile->getFixableCount(); |
36 | 36 | if ($errors === 0) { |
@@ -115,10 +115,10 @@ discard block |
||
115 | 115 | $totalErrors, |
116 | 116 | $totalWarnings, |
117 | 117 | $totalFixable, |
118 | - $showSources=false, |
|
119 | - $width=80, |
|
120 | - $interactive=false, |
|
121 | - $toScreen=true |
|
118 | + $showSources = false, |
|
119 | + $width = 80, |
|
120 | + $interactive = false, |
|
121 | + $toScreen = true |
|
122 | 122 | ) { |
123 | 123 | echo $cachedData; |
124 | 124 | if ($toScreen === true && $cachedData !== '') { |
@@ -16,361 +16,361 @@ |
||
16 | 16 | abstract class VersionControl implements Report |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * The name of the report we want in the output. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $reportName = 'VERSION CONTROL'; |
|
25 | - |
|
26 | - |
|
27 | - /** |
|
28 | - * Generate a partial report for a single processed file. |
|
29 | - * |
|
30 | - * Function should return TRUE if it printed or stored data about the file |
|
31 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
32 | - * its data should be counted in the grand totals. |
|
33 | - * |
|
34 | - * @param array $report Prepared report data. |
|
35 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
36 | - * @param bool $showSources Show sources? |
|
37 | - * @param int $width Maximum allowed line width. |
|
38 | - * |
|
39 | - * @return bool |
|
40 | - */ |
|
41 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
42 | - { |
|
43 | - $blames = $this->getBlameContent($report['filename']); |
|
44 | - |
|
45 | - $authorCache = []; |
|
46 | - $praiseCache = []; |
|
47 | - $sourceCache = []; |
|
48 | - |
|
49 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
50 | - $author = 'Unknown'; |
|
51 | - if (isset($blames[($line - 1)]) === true) { |
|
52 | - $blameAuthor = $this->getAuthor($blames[($line - 1)]); |
|
53 | - if ($blameAuthor !== false) { |
|
54 | - $author = $blameAuthor; |
|
55 | - } |
|
56 | - } |
|
57 | - |
|
58 | - if (isset($authorCache[$author]) === false) { |
|
59 | - $authorCache[$author] = 0; |
|
60 | - $praiseCache[$author] = [ |
|
61 | - 'good' => 0, |
|
62 | - 'bad' => 0, |
|
63 | - ]; |
|
64 | - } |
|
65 | - |
|
66 | - $praiseCache[$author]['bad']++; |
|
67 | - |
|
68 | - foreach ($lineErrors as $column => $colErrors) { |
|
69 | - foreach ($colErrors as $error) { |
|
70 | - $authorCache[$author]++; |
|
71 | - |
|
72 | - if ($showSources === true) { |
|
73 | - $source = $error['source']; |
|
74 | - if (isset($sourceCache[$author][$source]) === false) { |
|
75 | - $sourceCache[$author][$source] = [ |
|
76 | - 'count' => 1, |
|
77 | - 'fixable' => $error['fixable'], |
|
78 | - ]; |
|
79 | - } else { |
|
80 | - $sourceCache[$author][$source]['count']++; |
|
81 | - } |
|
82 | - } |
|
83 | - } |
|
84 | - } |
|
85 | - |
|
86 | - unset($blames[($line - 1)]); |
|
87 | - }//end foreach |
|
88 | - |
|
89 | - // Now go through and give the authors some credit for |
|
90 | - // all the lines that do not have errors. |
|
91 | - foreach ($blames as $line) { |
|
92 | - $author = $this->getAuthor($line); |
|
93 | - if ($author === false) { |
|
94 | - $author = 'Unknown'; |
|
95 | - } |
|
96 | - |
|
97 | - if (isset($authorCache[$author]) === false) { |
|
98 | - // This author doesn't have any errors. |
|
99 | - if (PHP_CODESNIFFER_VERBOSITY === 0) { |
|
100 | - continue; |
|
101 | - } |
|
102 | - |
|
103 | - $authorCache[$author] = 0; |
|
104 | - $praiseCache[$author] = [ |
|
105 | - 'good' => 0, |
|
106 | - 'bad' => 0, |
|
107 | - ]; |
|
108 | - } |
|
109 | - |
|
110 | - $praiseCache[$author]['good']++; |
|
111 | - }//end foreach |
|
112 | - |
|
113 | - foreach ($authorCache as $author => $errors) { |
|
114 | - echo "AUTHOR>>$author>>$errors".PHP_EOL; |
|
115 | - } |
|
116 | - |
|
117 | - foreach ($praiseCache as $author => $praise) { |
|
118 | - echo "PRAISE>>$author>>".$praise['good'].'>>'.$praise['bad'].PHP_EOL; |
|
119 | - } |
|
120 | - |
|
121 | - foreach ($sourceCache as $author => $sources) { |
|
122 | - foreach ($sources as $source => $sourceData) { |
|
123 | - $count = $sourceData['count']; |
|
124 | - $fixable = (int) $sourceData['fixable']; |
|
125 | - echo "SOURCE>>$author>>$source>>$count>>$fixable".PHP_EOL; |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - return true; |
|
130 | - |
|
131 | - }//end generateFileReport() |
|
132 | - |
|
133 | - |
|
134 | - /** |
|
135 | - * Prints the author of all errors and warnings, as given by "version control blame". |
|
136 | - * |
|
137 | - * @param string $cachedData Any partial report data that was returned from |
|
138 | - * generateFileReport during the run. |
|
139 | - * @param int $totalFiles Total number of files processed during the run. |
|
140 | - * @param int $totalErrors Total number of errors found during the run. |
|
141 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
142 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
143 | - * @param bool $showSources Show sources? |
|
144 | - * @param int $width Maximum allowed line width. |
|
145 | - * @param bool $interactive Are we running in interactive mode? |
|
146 | - * @param bool $toScreen Is the report being printed to screen? |
|
147 | - * |
|
148 | - * @return void |
|
149 | - */ |
|
150 | - public function generate( |
|
151 | - $cachedData, |
|
152 | - $totalFiles, |
|
153 | - $totalErrors, |
|
154 | - $totalWarnings, |
|
155 | - $totalFixable, |
|
156 | - $showSources=false, |
|
157 | - $width=80, |
|
158 | - $interactive=false, |
|
159 | - $toScreen=true |
|
160 | - ) { |
|
161 | - $errorsShown = ($totalErrors + $totalWarnings); |
|
162 | - if ($errorsShown === 0) { |
|
163 | - // Nothing to show. |
|
164 | - return; |
|
165 | - } |
|
166 | - |
|
167 | - $lines = explode(PHP_EOL, $cachedData); |
|
168 | - array_pop($lines); |
|
169 | - |
|
170 | - if (empty($lines) === true) { |
|
171 | - return; |
|
172 | - } |
|
173 | - |
|
174 | - $authorCache = []; |
|
175 | - $praiseCache = []; |
|
176 | - $sourceCache = []; |
|
177 | - |
|
178 | - foreach ($lines as $line) { |
|
179 | - $parts = explode('>>', $line); |
|
180 | - switch ($parts[0]) { |
|
181 | - case 'AUTHOR': |
|
182 | - if (isset($authorCache[$parts[1]]) === false) { |
|
183 | - $authorCache[$parts[1]] = $parts[2]; |
|
184 | - } else { |
|
185 | - $authorCache[$parts[1]] += $parts[2]; |
|
186 | - } |
|
187 | - break; |
|
188 | - case 'PRAISE': |
|
189 | - if (isset($praiseCache[$parts[1]]) === false) { |
|
190 | - $praiseCache[$parts[1]] = [ |
|
191 | - 'good' => $parts[2], |
|
192 | - 'bad' => $parts[3], |
|
193 | - ]; |
|
194 | - } else { |
|
195 | - $praiseCache[$parts[1]]['good'] += $parts[2]; |
|
196 | - $praiseCache[$parts[1]]['bad'] += $parts[3]; |
|
197 | - } |
|
198 | - break; |
|
199 | - case 'SOURCE': |
|
200 | - if (isset($praiseCache[$parts[1]]) === false) { |
|
201 | - $praiseCache[$parts[1]] = []; |
|
202 | - } |
|
203 | - |
|
204 | - if (isset($sourceCache[$parts[1]][$parts[2]]) === false) { |
|
205 | - $sourceCache[$parts[1]][$parts[2]] = [ |
|
206 | - 'count' => $parts[3], |
|
207 | - 'fixable' => (bool) $parts[4], |
|
208 | - ]; |
|
209 | - } else { |
|
210 | - $sourceCache[$parts[1]][$parts[2]]['count'] += $parts[3]; |
|
211 | - } |
|
212 | - break; |
|
213 | - default: |
|
214 | - break; |
|
215 | - }//end switch |
|
216 | - }//end foreach |
|
217 | - |
|
218 | - // Make sure the report width isn't too big. |
|
219 | - $maxLength = 0; |
|
220 | - foreach ($authorCache as $author => $count) { |
|
221 | - $maxLength = max($maxLength, strlen($author)); |
|
222 | - if ($showSources === true && isset($sourceCache[$author]) === true) { |
|
223 | - foreach ($sourceCache[$author] as $source => $sourceData) { |
|
224 | - if ($source === 'count') { |
|
225 | - continue; |
|
226 | - } |
|
227 | - |
|
228 | - $maxLength = max($maxLength, (strlen($source) + 9)); |
|
229 | - } |
|
230 | - } |
|
231 | - } |
|
232 | - |
|
233 | - $width = min($width, ($maxLength + 30)); |
|
234 | - $width = max($width, 70); |
|
235 | - arsort($authorCache); |
|
236 | - |
|
237 | - echo PHP_EOL."\033[1m".'PHP CODE SNIFFER '.$this->reportName.' BLAME SUMMARY'."\033[0m".PHP_EOL; |
|
238 | - echo str_repeat('-', $width).PHP_EOL."\033[1m"; |
|
239 | - if ($showSources === true) { |
|
240 | - echo 'AUTHOR SOURCE'.str_repeat(' ', ($width - 43)).'(Author %) (Overall %) COUNT'.PHP_EOL; |
|
241 | - echo str_repeat('-', $width).PHP_EOL; |
|
242 | - } else { |
|
243 | - echo 'AUTHOR'.str_repeat(' ', ($width - 34)).'(Author %) (Overall %) COUNT'.PHP_EOL; |
|
244 | - echo str_repeat('-', $width).PHP_EOL; |
|
245 | - } |
|
246 | - |
|
247 | - echo "\033[0m"; |
|
248 | - |
|
249 | - if ($showSources === true) { |
|
250 | - $maxSniffWidth = ($width - 15); |
|
251 | - |
|
252 | - if ($totalFixable > 0) { |
|
253 | - $maxSniffWidth -= 4; |
|
254 | - } |
|
255 | - } |
|
256 | - |
|
257 | - $fixableSources = 0; |
|
258 | - |
|
259 | - foreach ($authorCache as $author => $count) { |
|
260 | - if ($praiseCache[$author]['good'] === 0) { |
|
261 | - $percent = 0; |
|
262 | - } else { |
|
263 | - $total = ($praiseCache[$author]['bad'] + $praiseCache[$author]['good']); |
|
264 | - $percent = round(($praiseCache[$author]['bad'] / $total * 100), 2); |
|
265 | - } |
|
266 | - |
|
267 | - $overallPercent = '('.round((($count / $errorsShown) * 100), 2).')'; |
|
268 | - $authorPercent = '('.$percent.')'; |
|
269 | - $line = str_repeat(' ', (6 - strlen($count))).$count; |
|
270 | - $line = str_repeat(' ', (12 - strlen($overallPercent))).$overallPercent.$line; |
|
271 | - $line = str_repeat(' ', (11 - strlen($authorPercent))).$authorPercent.$line; |
|
272 | - $line = $author.str_repeat(' ', ($width - strlen($author) - strlen($line))).$line; |
|
273 | - |
|
274 | - if ($showSources === true) { |
|
275 | - $line = "\033[1m$line\033[0m"; |
|
276 | - } |
|
277 | - |
|
278 | - echo $line.PHP_EOL; |
|
279 | - |
|
280 | - if ($showSources === true && isset($sourceCache[$author]) === true) { |
|
281 | - $errors = $sourceCache[$author]; |
|
282 | - asort($errors); |
|
283 | - $errors = array_reverse($errors); |
|
284 | - |
|
285 | - foreach ($errors as $source => $sourceData) { |
|
286 | - if ($source === 'count') { |
|
287 | - continue; |
|
288 | - } |
|
289 | - |
|
290 | - $count = $sourceData['count']; |
|
291 | - |
|
292 | - $srcLength = strlen($source); |
|
293 | - if ($srcLength > $maxSniffWidth) { |
|
294 | - $source = substr($source, 0, $maxSniffWidth); |
|
295 | - } |
|
296 | - |
|
297 | - $line = str_repeat(' ', (5 - strlen($count))).$count; |
|
298 | - |
|
299 | - echo ' '; |
|
300 | - if ($totalFixable > 0) { |
|
301 | - echo '['; |
|
302 | - if ($sourceData['fixable'] === true) { |
|
303 | - echo 'x'; |
|
304 | - $fixableSources++; |
|
305 | - } else { |
|
306 | - echo ' '; |
|
307 | - } |
|
308 | - |
|
309 | - echo '] '; |
|
310 | - } |
|
311 | - |
|
312 | - echo $source; |
|
313 | - if ($totalFixable > 0) { |
|
314 | - echo str_repeat(' ', ($width - 18 - strlen($source))); |
|
315 | - } else { |
|
316 | - echo str_repeat(' ', ($width - 14 - strlen($source))); |
|
317 | - } |
|
318 | - |
|
319 | - echo $line.PHP_EOL; |
|
320 | - }//end foreach |
|
321 | - }//end if |
|
322 | - }//end foreach |
|
323 | - |
|
324 | - echo str_repeat('-', $width).PHP_EOL; |
|
325 | - echo "\033[1m".'A TOTAL OF '.$errorsShown.' SNIFF VIOLATION'; |
|
326 | - if ($errorsShown !== 1) { |
|
327 | - echo 'S'; |
|
328 | - } |
|
329 | - |
|
330 | - echo ' WERE COMMITTED BY '.count($authorCache).' AUTHOR'; |
|
331 | - if (count($authorCache) !== 1) { |
|
332 | - echo 'S'; |
|
333 | - } |
|
334 | - |
|
335 | - echo "\033[0m"; |
|
336 | - |
|
337 | - if ($totalFixable > 0) { |
|
338 | - if ($showSources === true) { |
|
339 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
340 | - echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m"; |
|
341 | - } else { |
|
342 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
343 | - echo "\033[1mPHPCBF CAN FIX $totalFixable OF THESE SNIFF VIOLATIONS AUTOMATICALLY\033[0m"; |
|
344 | - } |
|
345 | - } |
|
346 | - |
|
347 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
|
348 | - |
|
349 | - if ($toScreen === true && $interactive === false) { |
|
350 | - Timing::printRunTime(); |
|
351 | - } |
|
352 | - |
|
353 | - }//end generate() |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * Extract the author from a blame line. |
|
358 | - * |
|
359 | - * @param string $line Line to parse. |
|
360 | - * |
|
361 | - * @return mixed string or false if impossible to recover. |
|
362 | - */ |
|
363 | - abstract protected function getAuthor($line); |
|
364 | - |
|
365 | - |
|
366 | - /** |
|
367 | - * Gets the blame output. |
|
368 | - * |
|
369 | - * @param string $filename File to blame. |
|
370 | - * |
|
371 | - * @return array |
|
372 | - */ |
|
373 | - abstract protected function getBlameContent($filename); |
|
19 | + /** |
|
20 | + * The name of the report we want in the output. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $reportName = 'VERSION CONTROL'; |
|
25 | + |
|
26 | + |
|
27 | + /** |
|
28 | + * Generate a partial report for a single processed file. |
|
29 | + * |
|
30 | + * Function should return TRUE if it printed or stored data about the file |
|
31 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
32 | + * its data should be counted in the grand totals. |
|
33 | + * |
|
34 | + * @param array $report Prepared report data. |
|
35 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
36 | + * @param bool $showSources Show sources? |
|
37 | + * @param int $width Maximum allowed line width. |
|
38 | + * |
|
39 | + * @return bool |
|
40 | + */ |
|
41 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
42 | + { |
|
43 | + $blames = $this->getBlameContent($report['filename']); |
|
44 | + |
|
45 | + $authorCache = []; |
|
46 | + $praiseCache = []; |
|
47 | + $sourceCache = []; |
|
48 | + |
|
49 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
50 | + $author = 'Unknown'; |
|
51 | + if (isset($blames[($line - 1)]) === true) { |
|
52 | + $blameAuthor = $this->getAuthor($blames[($line - 1)]); |
|
53 | + if ($blameAuthor !== false) { |
|
54 | + $author = $blameAuthor; |
|
55 | + } |
|
56 | + } |
|
57 | + |
|
58 | + if (isset($authorCache[$author]) === false) { |
|
59 | + $authorCache[$author] = 0; |
|
60 | + $praiseCache[$author] = [ |
|
61 | + 'good' => 0, |
|
62 | + 'bad' => 0, |
|
63 | + ]; |
|
64 | + } |
|
65 | + |
|
66 | + $praiseCache[$author]['bad']++; |
|
67 | + |
|
68 | + foreach ($lineErrors as $column => $colErrors) { |
|
69 | + foreach ($colErrors as $error) { |
|
70 | + $authorCache[$author]++; |
|
71 | + |
|
72 | + if ($showSources === true) { |
|
73 | + $source = $error['source']; |
|
74 | + if (isset($sourceCache[$author][$source]) === false) { |
|
75 | + $sourceCache[$author][$source] = [ |
|
76 | + 'count' => 1, |
|
77 | + 'fixable' => $error['fixable'], |
|
78 | + ]; |
|
79 | + } else { |
|
80 | + $sourceCache[$author][$source]['count']++; |
|
81 | + } |
|
82 | + } |
|
83 | + } |
|
84 | + } |
|
85 | + |
|
86 | + unset($blames[($line - 1)]); |
|
87 | + }//end foreach |
|
88 | + |
|
89 | + // Now go through and give the authors some credit for |
|
90 | + // all the lines that do not have errors. |
|
91 | + foreach ($blames as $line) { |
|
92 | + $author = $this->getAuthor($line); |
|
93 | + if ($author === false) { |
|
94 | + $author = 'Unknown'; |
|
95 | + } |
|
96 | + |
|
97 | + if (isset($authorCache[$author]) === false) { |
|
98 | + // This author doesn't have any errors. |
|
99 | + if (PHP_CODESNIFFER_VERBOSITY === 0) { |
|
100 | + continue; |
|
101 | + } |
|
102 | + |
|
103 | + $authorCache[$author] = 0; |
|
104 | + $praiseCache[$author] = [ |
|
105 | + 'good' => 0, |
|
106 | + 'bad' => 0, |
|
107 | + ]; |
|
108 | + } |
|
109 | + |
|
110 | + $praiseCache[$author]['good']++; |
|
111 | + }//end foreach |
|
112 | + |
|
113 | + foreach ($authorCache as $author => $errors) { |
|
114 | + echo "AUTHOR>>$author>>$errors".PHP_EOL; |
|
115 | + } |
|
116 | + |
|
117 | + foreach ($praiseCache as $author => $praise) { |
|
118 | + echo "PRAISE>>$author>>".$praise['good'].'>>'.$praise['bad'].PHP_EOL; |
|
119 | + } |
|
120 | + |
|
121 | + foreach ($sourceCache as $author => $sources) { |
|
122 | + foreach ($sources as $source => $sourceData) { |
|
123 | + $count = $sourceData['count']; |
|
124 | + $fixable = (int) $sourceData['fixable']; |
|
125 | + echo "SOURCE>>$author>>$source>>$count>>$fixable".PHP_EOL; |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + return true; |
|
130 | + |
|
131 | + }//end generateFileReport() |
|
132 | + |
|
133 | + |
|
134 | + /** |
|
135 | + * Prints the author of all errors and warnings, as given by "version control blame". |
|
136 | + * |
|
137 | + * @param string $cachedData Any partial report data that was returned from |
|
138 | + * generateFileReport during the run. |
|
139 | + * @param int $totalFiles Total number of files processed during the run. |
|
140 | + * @param int $totalErrors Total number of errors found during the run. |
|
141 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
142 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
143 | + * @param bool $showSources Show sources? |
|
144 | + * @param int $width Maximum allowed line width. |
|
145 | + * @param bool $interactive Are we running in interactive mode? |
|
146 | + * @param bool $toScreen Is the report being printed to screen? |
|
147 | + * |
|
148 | + * @return void |
|
149 | + */ |
|
150 | + public function generate( |
|
151 | + $cachedData, |
|
152 | + $totalFiles, |
|
153 | + $totalErrors, |
|
154 | + $totalWarnings, |
|
155 | + $totalFixable, |
|
156 | + $showSources=false, |
|
157 | + $width=80, |
|
158 | + $interactive=false, |
|
159 | + $toScreen=true |
|
160 | + ) { |
|
161 | + $errorsShown = ($totalErrors + $totalWarnings); |
|
162 | + if ($errorsShown === 0) { |
|
163 | + // Nothing to show. |
|
164 | + return; |
|
165 | + } |
|
166 | + |
|
167 | + $lines = explode(PHP_EOL, $cachedData); |
|
168 | + array_pop($lines); |
|
169 | + |
|
170 | + if (empty($lines) === true) { |
|
171 | + return; |
|
172 | + } |
|
173 | + |
|
174 | + $authorCache = []; |
|
175 | + $praiseCache = []; |
|
176 | + $sourceCache = []; |
|
177 | + |
|
178 | + foreach ($lines as $line) { |
|
179 | + $parts = explode('>>', $line); |
|
180 | + switch ($parts[0]) { |
|
181 | + case 'AUTHOR': |
|
182 | + if (isset($authorCache[$parts[1]]) === false) { |
|
183 | + $authorCache[$parts[1]] = $parts[2]; |
|
184 | + } else { |
|
185 | + $authorCache[$parts[1]] += $parts[2]; |
|
186 | + } |
|
187 | + break; |
|
188 | + case 'PRAISE': |
|
189 | + if (isset($praiseCache[$parts[1]]) === false) { |
|
190 | + $praiseCache[$parts[1]] = [ |
|
191 | + 'good' => $parts[2], |
|
192 | + 'bad' => $parts[3], |
|
193 | + ]; |
|
194 | + } else { |
|
195 | + $praiseCache[$parts[1]]['good'] += $parts[2]; |
|
196 | + $praiseCache[$parts[1]]['bad'] += $parts[3]; |
|
197 | + } |
|
198 | + break; |
|
199 | + case 'SOURCE': |
|
200 | + if (isset($praiseCache[$parts[1]]) === false) { |
|
201 | + $praiseCache[$parts[1]] = []; |
|
202 | + } |
|
203 | + |
|
204 | + if (isset($sourceCache[$parts[1]][$parts[2]]) === false) { |
|
205 | + $sourceCache[$parts[1]][$parts[2]] = [ |
|
206 | + 'count' => $parts[3], |
|
207 | + 'fixable' => (bool) $parts[4], |
|
208 | + ]; |
|
209 | + } else { |
|
210 | + $sourceCache[$parts[1]][$parts[2]]['count'] += $parts[3]; |
|
211 | + } |
|
212 | + break; |
|
213 | + default: |
|
214 | + break; |
|
215 | + }//end switch |
|
216 | + }//end foreach |
|
217 | + |
|
218 | + // Make sure the report width isn't too big. |
|
219 | + $maxLength = 0; |
|
220 | + foreach ($authorCache as $author => $count) { |
|
221 | + $maxLength = max($maxLength, strlen($author)); |
|
222 | + if ($showSources === true && isset($sourceCache[$author]) === true) { |
|
223 | + foreach ($sourceCache[$author] as $source => $sourceData) { |
|
224 | + if ($source === 'count') { |
|
225 | + continue; |
|
226 | + } |
|
227 | + |
|
228 | + $maxLength = max($maxLength, (strlen($source) + 9)); |
|
229 | + } |
|
230 | + } |
|
231 | + } |
|
232 | + |
|
233 | + $width = min($width, ($maxLength + 30)); |
|
234 | + $width = max($width, 70); |
|
235 | + arsort($authorCache); |
|
236 | + |
|
237 | + echo PHP_EOL."\033[1m".'PHP CODE SNIFFER '.$this->reportName.' BLAME SUMMARY'."\033[0m".PHP_EOL; |
|
238 | + echo str_repeat('-', $width).PHP_EOL."\033[1m"; |
|
239 | + if ($showSources === true) { |
|
240 | + echo 'AUTHOR SOURCE'.str_repeat(' ', ($width - 43)).'(Author %) (Overall %) COUNT'.PHP_EOL; |
|
241 | + echo str_repeat('-', $width).PHP_EOL; |
|
242 | + } else { |
|
243 | + echo 'AUTHOR'.str_repeat(' ', ($width - 34)).'(Author %) (Overall %) COUNT'.PHP_EOL; |
|
244 | + echo str_repeat('-', $width).PHP_EOL; |
|
245 | + } |
|
246 | + |
|
247 | + echo "\033[0m"; |
|
248 | + |
|
249 | + if ($showSources === true) { |
|
250 | + $maxSniffWidth = ($width - 15); |
|
251 | + |
|
252 | + if ($totalFixable > 0) { |
|
253 | + $maxSniffWidth -= 4; |
|
254 | + } |
|
255 | + } |
|
256 | + |
|
257 | + $fixableSources = 0; |
|
258 | + |
|
259 | + foreach ($authorCache as $author => $count) { |
|
260 | + if ($praiseCache[$author]['good'] === 0) { |
|
261 | + $percent = 0; |
|
262 | + } else { |
|
263 | + $total = ($praiseCache[$author]['bad'] + $praiseCache[$author]['good']); |
|
264 | + $percent = round(($praiseCache[$author]['bad'] / $total * 100), 2); |
|
265 | + } |
|
266 | + |
|
267 | + $overallPercent = '('.round((($count / $errorsShown) * 100), 2).')'; |
|
268 | + $authorPercent = '('.$percent.')'; |
|
269 | + $line = str_repeat(' ', (6 - strlen($count))).$count; |
|
270 | + $line = str_repeat(' ', (12 - strlen($overallPercent))).$overallPercent.$line; |
|
271 | + $line = str_repeat(' ', (11 - strlen($authorPercent))).$authorPercent.$line; |
|
272 | + $line = $author.str_repeat(' ', ($width - strlen($author) - strlen($line))).$line; |
|
273 | + |
|
274 | + if ($showSources === true) { |
|
275 | + $line = "\033[1m$line\033[0m"; |
|
276 | + } |
|
277 | + |
|
278 | + echo $line.PHP_EOL; |
|
279 | + |
|
280 | + if ($showSources === true && isset($sourceCache[$author]) === true) { |
|
281 | + $errors = $sourceCache[$author]; |
|
282 | + asort($errors); |
|
283 | + $errors = array_reverse($errors); |
|
284 | + |
|
285 | + foreach ($errors as $source => $sourceData) { |
|
286 | + if ($source === 'count') { |
|
287 | + continue; |
|
288 | + } |
|
289 | + |
|
290 | + $count = $sourceData['count']; |
|
291 | + |
|
292 | + $srcLength = strlen($source); |
|
293 | + if ($srcLength > $maxSniffWidth) { |
|
294 | + $source = substr($source, 0, $maxSniffWidth); |
|
295 | + } |
|
296 | + |
|
297 | + $line = str_repeat(' ', (5 - strlen($count))).$count; |
|
298 | + |
|
299 | + echo ' '; |
|
300 | + if ($totalFixable > 0) { |
|
301 | + echo '['; |
|
302 | + if ($sourceData['fixable'] === true) { |
|
303 | + echo 'x'; |
|
304 | + $fixableSources++; |
|
305 | + } else { |
|
306 | + echo ' '; |
|
307 | + } |
|
308 | + |
|
309 | + echo '] '; |
|
310 | + } |
|
311 | + |
|
312 | + echo $source; |
|
313 | + if ($totalFixable > 0) { |
|
314 | + echo str_repeat(' ', ($width - 18 - strlen($source))); |
|
315 | + } else { |
|
316 | + echo str_repeat(' ', ($width - 14 - strlen($source))); |
|
317 | + } |
|
318 | + |
|
319 | + echo $line.PHP_EOL; |
|
320 | + }//end foreach |
|
321 | + }//end if |
|
322 | + }//end foreach |
|
323 | + |
|
324 | + echo str_repeat('-', $width).PHP_EOL; |
|
325 | + echo "\033[1m".'A TOTAL OF '.$errorsShown.' SNIFF VIOLATION'; |
|
326 | + if ($errorsShown !== 1) { |
|
327 | + echo 'S'; |
|
328 | + } |
|
329 | + |
|
330 | + echo ' WERE COMMITTED BY '.count($authorCache).' AUTHOR'; |
|
331 | + if (count($authorCache) !== 1) { |
|
332 | + echo 'S'; |
|
333 | + } |
|
334 | + |
|
335 | + echo "\033[0m"; |
|
336 | + |
|
337 | + if ($totalFixable > 0) { |
|
338 | + if ($showSources === true) { |
|
339 | + echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
340 | + echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m"; |
|
341 | + } else { |
|
342 | + echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
343 | + echo "\033[1mPHPCBF CAN FIX $totalFixable OF THESE SNIFF VIOLATIONS AUTOMATICALLY\033[0m"; |
|
344 | + } |
|
345 | + } |
|
346 | + |
|
347 | + echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
|
348 | + |
|
349 | + if ($toScreen === true && $interactive === false) { |
|
350 | + Timing::printRunTime(); |
|
351 | + } |
|
352 | + |
|
353 | + }//end generate() |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * Extract the author from a blame line. |
|
358 | + * |
|
359 | + * @param string $line Line to parse. |
|
360 | + * |
|
361 | + * @return mixed string or false if impossible to recover. |
|
362 | + */ |
|
363 | + abstract protected function getAuthor($line); |
|
364 | + |
|
365 | + |
|
366 | + /** |
|
367 | + * Gets the blame output. |
|
368 | + * |
|
369 | + * @param string $filename File to blame. |
|
370 | + * |
|
371 | + * @return array |
|
372 | + */ |
|
373 | + abstract protected function getBlameContent($filename); |
|
374 | 374 | |
375 | 375 | |
376 | 376 | }//end class |
@@ -178,40 +178,40 @@ |
||
178 | 178 | foreach ($lines as $line) { |
179 | 179 | $parts = explode('>>', $line); |
180 | 180 | switch ($parts[0]) { |
181 | - case 'AUTHOR': |
|
182 | - if (isset($authorCache[$parts[1]]) === false) { |
|
183 | - $authorCache[$parts[1]] = $parts[2]; |
|
184 | - } else { |
|
185 | - $authorCache[$parts[1]] += $parts[2]; |
|
186 | - } |
|
187 | - break; |
|
188 | - case 'PRAISE': |
|
189 | - if (isset($praiseCache[$parts[1]]) === false) { |
|
190 | - $praiseCache[$parts[1]] = [ |
|
191 | - 'good' => $parts[2], |
|
192 | - 'bad' => $parts[3], |
|
193 | - ]; |
|
194 | - } else { |
|
195 | - $praiseCache[$parts[1]]['good'] += $parts[2]; |
|
196 | - $praiseCache[$parts[1]]['bad'] += $parts[3]; |
|
197 | - } |
|
198 | - break; |
|
199 | - case 'SOURCE': |
|
200 | - if (isset($praiseCache[$parts[1]]) === false) { |
|
201 | - $praiseCache[$parts[1]] = []; |
|
202 | - } |
|
203 | - |
|
204 | - if (isset($sourceCache[$parts[1]][$parts[2]]) === false) { |
|
205 | - $sourceCache[$parts[1]][$parts[2]] = [ |
|
206 | - 'count' => $parts[3], |
|
207 | - 'fixable' => (bool) $parts[4], |
|
208 | - ]; |
|
209 | - } else { |
|
210 | - $sourceCache[$parts[1]][$parts[2]]['count'] += $parts[3]; |
|
211 | - } |
|
212 | - break; |
|
213 | - default: |
|
214 | - break; |
|
181 | + case 'AUTHOR': |
|
182 | + if (isset($authorCache[$parts[1]]) === false) { |
|
183 | + $authorCache[$parts[1]] = $parts[2]; |
|
184 | + } else { |
|
185 | + $authorCache[$parts[1]] += $parts[2]; |
|
186 | + } |
|
187 | + break; |
|
188 | + case 'PRAISE': |
|
189 | + if (isset($praiseCache[$parts[1]]) === false) { |
|
190 | + $praiseCache[$parts[1]] = [ |
|
191 | + 'good' => $parts[2], |
|
192 | + 'bad' => $parts[3], |
|
193 | + ]; |
|
194 | + } else { |
|
195 | + $praiseCache[$parts[1]]['good'] += $parts[2]; |
|
196 | + $praiseCache[$parts[1]]['bad'] += $parts[3]; |
|
197 | + } |
|
198 | + break; |
|
199 | + case 'SOURCE': |
|
200 | + if (isset($praiseCache[$parts[1]]) === false) { |
|
201 | + $praiseCache[$parts[1]] = []; |
|
202 | + } |
|
203 | + |
|
204 | + if (isset($sourceCache[$parts[1]][$parts[2]]) === false) { |
|
205 | + $sourceCache[$parts[1]][$parts[2]] = [ |
|
206 | + 'count' => $parts[3], |
|
207 | + 'fixable' => (bool) $parts[4], |
|
208 | + ]; |
|
209 | + } else { |
|
210 | + $sourceCache[$parts[1]][$parts[2]]['count'] += $parts[3]; |
|
211 | + } |
|
212 | + break; |
|
213 | + default: |
|
214 | + break; |
|
215 | 215 | }//end switch |
216 | 216 | }//end foreach |
217 | 217 |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | * |
39 | 39 | * @return bool |
40 | 40 | */ |
41 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
41 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
42 | 42 | { |
43 | 43 | $blames = $this->getBlameContent($report['filename']); |
44 | 44 | |
@@ -153,10 +153,10 @@ discard block |
||
153 | 153 | $totalErrors, |
154 | 154 | $totalWarnings, |
155 | 155 | $totalFixable, |
156 | - $showSources=false, |
|
157 | - $width=80, |
|
158 | - $interactive=false, |
|
159 | - $toScreen=true |
|
156 | + $showSources = false, |
|
157 | + $width = 80, |
|
158 | + $interactive = false, |
|
159 | + $toScreen = true |
|
160 | 160 | ) { |
161 | 161 | $errorsShown = ($totalErrors + $totalWarnings); |
162 | 162 | if ($errorsShown === 0) { |
@@ -269,14 +269,14 @@ |
||
269 | 269 | foreach ($diffLines as $line) { |
270 | 270 | if (isset($line[0]) === true) { |
271 | 271 | switch ($line[0]) { |
272 | - case '-': |
|
273 | - $diff[] = "\033[31m$line\033[0m"; |
|
274 | - break; |
|
275 | - case '+': |
|
276 | - $diff[] = "\033[32m$line\033[0m"; |
|
277 | - break; |
|
278 | - default: |
|
279 | - $diff[] = $line; |
|
272 | + case '-': |
|
273 | + $diff[] = "\033[31m$line\033[0m"; |
|
274 | + break; |
|
275 | + case '+': |
|
276 | + $diff[] = "\033[32m$line\033[0m"; |
|
277 | + break; |
|
278 | + default: |
|
279 | + $diff[] = $line; |
|
280 | 280 | } |
281 | 281 | } |
282 | 282 | } |
@@ -224,7 +224,7 @@ discard block |
||
224 | 224 | * |
225 | 225 | * @return string |
226 | 226 | */ |
227 | - public function generateDiff($filePath=null, $colors=true) |
|
227 | + public function generateDiff($filePath = null, $colors = true) |
|
228 | 228 | { |
229 | 229 | if ($filePath === null) { |
230 | 230 | $filePath = $this->currentFile->getFilename(); |
@@ -643,7 +643,7 @@ discard block |
||
643 | 643 | * |
644 | 644 | * @return bool If the change was accepted. |
645 | 645 | */ |
646 | - public function substrToken($stackPtr, $start, $length=null) |
|
646 | + public function substrToken($stackPtr, $start, $length = null) |
|
647 | 647 | { |
648 | 648 | $current = $this->getTokenContent($stackPtr); |
649 | 649 |
@@ -18,789 +18,789 @@ |
||
18 | 18 | class Fixer |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * Is the fixer enabled and fixing a file? |
|
23 | - * |
|
24 | - * Sniffs should check this value to ensure they are not |
|
25 | - * doing extra processing to prepare for a fix when fixing is |
|
26 | - * not required. |
|
27 | - * |
|
28 | - * @var boolean |
|
29 | - */ |
|
30 | - public $enabled = false; |
|
31 | - |
|
32 | - /** |
|
33 | - * The number of times we have looped over a file. |
|
34 | - * |
|
35 | - * @var integer |
|
36 | - */ |
|
37 | - public $loops = 0; |
|
38 | - |
|
39 | - /** |
|
40 | - * The file being fixed. |
|
41 | - * |
|
42 | - * @var \PHP_CodeSniffer\Files\File |
|
43 | - */ |
|
44 | - private $currentFile = null; |
|
45 | - |
|
46 | - /** |
|
47 | - * The list of tokens that make up the file contents. |
|
48 | - * |
|
49 | - * This is a simplified list which just contains the token content and nothing |
|
50 | - * else. This is the array that is updated as fixes are made, not the file's |
|
51 | - * token array. Imploding this array will give you the file content back. |
|
52 | - * |
|
53 | - * @var array<int, string> |
|
54 | - */ |
|
55 | - private $tokens = []; |
|
56 | - |
|
57 | - /** |
|
58 | - * A list of tokens that have already been fixed. |
|
59 | - * |
|
60 | - * We don't allow the same token to be fixed more than once each time |
|
61 | - * through a file as this can easily cause conflicts between sniffs. |
|
62 | - * |
|
63 | - * @var int[] |
|
64 | - */ |
|
65 | - private $fixedTokens = []; |
|
66 | - |
|
67 | - /** |
|
68 | - * The last value of each fixed token. |
|
69 | - * |
|
70 | - * If a token is being "fixed" back to its last value, the fix is |
|
71 | - * probably conflicting with another. |
|
72 | - * |
|
73 | - * @var array<int, string> |
|
74 | - */ |
|
75 | - private $oldTokenValues = []; |
|
76 | - |
|
77 | - /** |
|
78 | - * A list of tokens that have been fixed during a changeset. |
|
79 | - * |
|
80 | - * All changes in changeset must be able to be applied, or else |
|
81 | - * the entire changeset is rejected. |
|
82 | - * |
|
83 | - * @var array |
|
84 | - */ |
|
85 | - private $changeset = []; |
|
86 | - |
|
87 | - /** |
|
88 | - * Is there an open changeset. |
|
89 | - * |
|
90 | - * @var boolean |
|
91 | - */ |
|
92 | - private $inChangeset = false; |
|
93 | - |
|
94 | - /** |
|
95 | - * Is the current fixing loop in conflict? |
|
96 | - * |
|
97 | - * @var boolean |
|
98 | - */ |
|
99 | - private $inConflict = false; |
|
100 | - |
|
101 | - /** |
|
102 | - * The number of fixes that have been performed. |
|
103 | - * |
|
104 | - * @var integer |
|
105 | - */ |
|
106 | - private $numFixes = 0; |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * Starts fixing a new file. |
|
111 | - * |
|
112 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being fixed. |
|
113 | - * |
|
114 | - * @return void |
|
115 | - */ |
|
116 | - public function startFile(File $phpcsFile) |
|
117 | - { |
|
118 | - $this->currentFile = $phpcsFile; |
|
119 | - $this->numFixes = 0; |
|
120 | - $this->fixedTokens = []; |
|
121 | - |
|
122 | - $tokens = $phpcsFile->getTokens(); |
|
123 | - $this->tokens = []; |
|
124 | - foreach ($tokens as $index => $token) { |
|
125 | - if (isset($token['orig_content']) === true) { |
|
126 | - $this->tokens[$index] = $token['orig_content']; |
|
127 | - } else { |
|
128 | - $this->tokens[$index] = $token['content']; |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - }//end startFile() |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * Attempt to fix the file by processing it until no fixes are made. |
|
137 | - * |
|
138 | - * @return boolean |
|
139 | - */ |
|
140 | - public function fixFile() |
|
141 | - { |
|
142 | - $fixable = $this->currentFile->getFixableCount(); |
|
143 | - if ($fixable === 0) { |
|
144 | - // Nothing to fix. |
|
145 | - return false; |
|
146 | - } |
|
147 | - |
|
148 | - $this->enabled = true; |
|
149 | - |
|
150 | - $this->loops = 0; |
|
151 | - while ($this->loops < 50) { |
|
152 | - ob_start(); |
|
153 | - |
|
154 | - // Only needed once file content has changed. |
|
155 | - $contents = $this->getContents(); |
|
156 | - |
|
157 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
158 | - @ob_end_clean(); |
|
159 | - echo '---START FILE CONTENT---'.PHP_EOL; |
|
160 | - $lines = explode($this->currentFile->eolChar, $contents); |
|
161 | - $max = strlen(count($lines)); |
|
162 | - foreach ($lines as $lineNum => $line) { |
|
163 | - $lineNum++; |
|
164 | - echo str_pad($lineNum, $max, ' ', STR_PAD_LEFT).'|'.$line.PHP_EOL; |
|
165 | - } |
|
166 | - |
|
167 | - echo '--- END FILE CONTENT ---'.PHP_EOL; |
|
168 | - ob_start(); |
|
169 | - } |
|
170 | - |
|
171 | - $this->inConflict = false; |
|
172 | - $this->currentFile->ruleset->populateTokenListeners(); |
|
173 | - $this->currentFile->setContent($contents); |
|
174 | - $this->currentFile->process(); |
|
175 | - ob_end_clean(); |
|
176 | - |
|
177 | - $this->loops++; |
|
178 | - |
|
179 | - if (PHP_CODESNIFFER_CBF === true && PHP_CODESNIFFER_VERBOSITY > 0) { |
|
180 | - echo "\r".str_repeat(' ', 80)."\r"; |
|
181 | - echo "\t=> Fixing file: $this->numFixes/$fixable violations remaining [made $this->loops pass"; |
|
182 | - if ($this->loops > 1) { |
|
183 | - echo 'es'; |
|
184 | - } |
|
185 | - |
|
186 | - echo ']... '; |
|
187 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
188 | - echo PHP_EOL; |
|
189 | - } |
|
190 | - } |
|
191 | - |
|
192 | - if ($this->numFixes === 0 && $this->inConflict === false) { |
|
193 | - // Nothing left to do. |
|
194 | - break; |
|
195 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
196 | - echo "\t* fixed $this->numFixes violations, starting loop ".($this->loops + 1).' *'.PHP_EOL; |
|
197 | - } |
|
198 | - }//end while |
|
199 | - |
|
200 | - $this->enabled = false; |
|
201 | - |
|
202 | - if ($this->numFixes > 0) { |
|
203 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
204 | - if (ob_get_level() > 0) { |
|
205 | - ob_end_clean(); |
|
206 | - } |
|
207 | - |
|
208 | - echo "\t*** Reached maximum number of loops with $this->numFixes violations left unfixed ***".PHP_EOL; |
|
209 | - ob_start(); |
|
210 | - } |
|
211 | - |
|
212 | - return false; |
|
213 | - } |
|
214 | - |
|
215 | - return true; |
|
216 | - |
|
217 | - }//end fixFile() |
|
218 | - |
|
219 | - |
|
220 | - /** |
|
221 | - * Generates a text diff of the original file and the new content. |
|
222 | - * |
|
223 | - * @param string $filePath Optional file path to diff the file against. |
|
224 | - * If not specified, the original version of the |
|
225 | - * file will be used. |
|
226 | - * @param boolean $colors Print coloured output or not. |
|
227 | - * |
|
228 | - * @return string |
|
229 | - */ |
|
230 | - public function generateDiff($filePath=null, $colors=true) |
|
231 | - { |
|
232 | - if ($filePath === null) { |
|
233 | - $filePath = $this->currentFile->getFilename(); |
|
234 | - } |
|
235 | - |
|
236 | - $cwd = getcwd().DIRECTORY_SEPARATOR; |
|
237 | - if (strpos($filePath, $cwd) === 0) { |
|
238 | - $filename = substr($filePath, strlen($cwd)); |
|
239 | - } else { |
|
240 | - $filename = $filePath; |
|
241 | - } |
|
242 | - |
|
243 | - $contents = $this->getContents(); |
|
244 | - |
|
245 | - $tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer'); |
|
246 | - $fixedFile = fopen($tempName, 'w'); |
|
247 | - fwrite($fixedFile, $contents); |
|
248 | - |
|
249 | - // We must use something like shell_exec() because whitespace at the end |
|
250 | - // of lines is critical to diff files. |
|
251 | - $filename = escapeshellarg($filename); |
|
252 | - $cmd = "diff -u -L$filename -LPHP_CodeSniffer $filename \"$tempName\""; |
|
253 | - |
|
254 | - $diff = shell_exec($cmd); |
|
255 | - |
|
256 | - fclose($fixedFile); |
|
257 | - if (is_file($tempName) === true) { |
|
258 | - unlink($tempName); |
|
259 | - } |
|
260 | - |
|
261 | - if ($diff === null) { |
|
262 | - return ''; |
|
263 | - } |
|
264 | - |
|
265 | - if ($colors === false) { |
|
266 | - return $diff; |
|
267 | - } |
|
268 | - |
|
269 | - $diffLines = explode(PHP_EOL, $diff); |
|
270 | - if (count($diffLines) === 1) { |
|
271 | - // Seems to be required for cygwin. |
|
272 | - $diffLines = explode("\n", $diff); |
|
273 | - } |
|
274 | - |
|
275 | - $diff = []; |
|
276 | - foreach ($diffLines as $line) { |
|
277 | - if (isset($line[0]) === true) { |
|
278 | - switch ($line[0]) { |
|
279 | - case '-': |
|
280 | - $diff[] = "\033[31m$line\033[0m"; |
|
281 | - break; |
|
282 | - case '+': |
|
283 | - $diff[] = "\033[32m$line\033[0m"; |
|
284 | - break; |
|
285 | - default: |
|
286 | - $diff[] = $line; |
|
287 | - } |
|
288 | - } |
|
289 | - } |
|
290 | - |
|
291 | - $diff = implode(PHP_EOL, $diff); |
|
292 | - |
|
293 | - return $diff; |
|
294 | - |
|
295 | - }//end generateDiff() |
|
296 | - |
|
297 | - |
|
298 | - /** |
|
299 | - * Get a count of fixes that have been performed on the file. |
|
300 | - * |
|
301 | - * This value is reset every time a new file is started, or an existing |
|
302 | - * file is restarted. |
|
303 | - * |
|
304 | - * @return int |
|
305 | - */ |
|
306 | - public function getFixCount() |
|
307 | - { |
|
308 | - return $this->numFixes; |
|
309 | - |
|
310 | - }//end getFixCount() |
|
311 | - |
|
312 | - |
|
313 | - /** |
|
314 | - * Get the current content of the file, as a string. |
|
315 | - * |
|
316 | - * @return string |
|
317 | - */ |
|
318 | - public function getContents() |
|
319 | - { |
|
320 | - $contents = implode($this->tokens); |
|
321 | - return $contents; |
|
322 | - |
|
323 | - }//end getContents() |
|
324 | - |
|
325 | - |
|
326 | - /** |
|
327 | - * Get the current fixed content of a token. |
|
328 | - * |
|
329 | - * This function takes changesets into account so should be used |
|
330 | - * instead of directly accessing the token array. |
|
331 | - * |
|
332 | - * @param int $stackPtr The position of the token in the token stack. |
|
333 | - * |
|
334 | - * @return string |
|
335 | - */ |
|
336 | - public function getTokenContent($stackPtr) |
|
337 | - { |
|
338 | - if ($this->inChangeset === true |
|
339 | - && isset($this->changeset[$stackPtr]) === true |
|
340 | - ) { |
|
341 | - return $this->changeset[$stackPtr]; |
|
342 | - } else { |
|
343 | - return $this->tokens[$stackPtr]; |
|
344 | - } |
|
345 | - |
|
346 | - }//end getTokenContent() |
|
347 | - |
|
348 | - |
|
349 | - /** |
|
350 | - * Start recording actions for a changeset. |
|
351 | - * |
|
352 | - * @return void |
|
353 | - */ |
|
354 | - public function beginChangeset() |
|
355 | - { |
|
356 | - if ($this->inConflict === true) { |
|
357 | - return false; |
|
358 | - } |
|
359 | - |
|
360 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
361 | - $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
362 | - if ($bt[1]['class'] === __CLASS__) { |
|
363 | - $sniff = 'Fixer'; |
|
364 | - } else { |
|
365 | - $sniff = Util\Common::getSniffCode($bt[1]['class']); |
|
366 | - } |
|
367 | - |
|
368 | - $line = $bt[0]['line']; |
|
369 | - |
|
370 | - @ob_end_clean(); |
|
371 | - echo "\t=> Changeset started by $sniff:$line".PHP_EOL; |
|
372 | - ob_start(); |
|
373 | - } |
|
374 | - |
|
375 | - $this->changeset = []; |
|
376 | - $this->inChangeset = true; |
|
377 | - |
|
378 | - }//end beginChangeset() |
|
379 | - |
|
380 | - |
|
381 | - /** |
|
382 | - * Stop recording actions for a changeset, and apply logged changes. |
|
383 | - * |
|
384 | - * @return boolean |
|
385 | - */ |
|
386 | - public function endChangeset() |
|
387 | - { |
|
388 | - if ($this->inConflict === true) { |
|
389 | - return false; |
|
390 | - } |
|
391 | - |
|
392 | - $this->inChangeset = false; |
|
393 | - |
|
394 | - $success = true; |
|
395 | - $applied = []; |
|
396 | - foreach ($this->changeset as $stackPtr => $content) { |
|
397 | - $success = $this->replaceToken($stackPtr, $content); |
|
398 | - if ($success === false) { |
|
399 | - break; |
|
400 | - } else { |
|
401 | - $applied[] = $stackPtr; |
|
402 | - } |
|
403 | - } |
|
404 | - |
|
405 | - if ($success === false) { |
|
406 | - // Rolling back all changes. |
|
407 | - foreach ($applied as $stackPtr) { |
|
408 | - $this->revertToken($stackPtr); |
|
409 | - } |
|
410 | - |
|
411 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
412 | - @ob_end_clean(); |
|
413 | - echo "\t=> Changeset failed to apply".PHP_EOL; |
|
414 | - ob_start(); |
|
415 | - } |
|
416 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
417 | - $fixes = count($this->changeset); |
|
418 | - @ob_end_clean(); |
|
419 | - echo "\t=> Changeset ended: $fixes changes applied".PHP_EOL; |
|
420 | - ob_start(); |
|
421 | - } |
|
422 | - |
|
423 | - $this->changeset = []; |
|
424 | - return true; |
|
425 | - |
|
426 | - }//end endChangeset() |
|
427 | - |
|
428 | - |
|
429 | - /** |
|
430 | - * Stop recording actions for a changeset, and discard logged changes. |
|
431 | - * |
|
432 | - * @return void |
|
433 | - */ |
|
434 | - public function rollbackChangeset() |
|
435 | - { |
|
436 | - $this->inChangeset = false; |
|
437 | - $this->inConflict = false; |
|
438 | - |
|
439 | - if (empty($this->changeset) === false) { |
|
440 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
441 | - $bt = debug_backtrace(); |
|
442 | - if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
443 | - $sniff = $bt[2]['class']; |
|
444 | - $line = $bt[1]['line']; |
|
445 | - } else { |
|
446 | - $sniff = $bt[1]['class']; |
|
447 | - $line = $bt[0]['line']; |
|
448 | - } |
|
449 | - |
|
450 | - $sniff = Util\Common::getSniffCode($sniff); |
|
451 | - |
|
452 | - $numChanges = count($this->changeset); |
|
453 | - |
|
454 | - @ob_end_clean(); |
|
455 | - echo "\t\tR: $sniff:$line rolled back the changeset ($numChanges changes)".PHP_EOL; |
|
456 | - echo "\t=> Changeset rolled back".PHP_EOL; |
|
457 | - ob_start(); |
|
458 | - } |
|
459 | - |
|
460 | - $this->changeset = []; |
|
461 | - }//end if |
|
462 | - |
|
463 | - }//end rollbackChangeset() |
|
464 | - |
|
465 | - |
|
466 | - /** |
|
467 | - * Replace the entire contents of a token. |
|
468 | - * |
|
469 | - * @param int $stackPtr The position of the token in the token stack. |
|
470 | - * @param string $content The new content of the token. |
|
471 | - * |
|
472 | - * @return bool If the change was accepted. |
|
473 | - */ |
|
474 | - public function replaceToken($stackPtr, $content) |
|
475 | - { |
|
476 | - if ($this->inConflict === true) { |
|
477 | - return false; |
|
478 | - } |
|
479 | - |
|
480 | - if ($this->inChangeset === false |
|
481 | - && isset($this->fixedTokens[$stackPtr]) === true |
|
482 | - ) { |
|
483 | - $indent = "\t"; |
|
484 | - if (empty($this->changeset) === false) { |
|
485 | - $indent .= "\t"; |
|
486 | - } |
|
487 | - |
|
488 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
489 | - @ob_end_clean(); |
|
490 | - echo "$indent* token $stackPtr has already been modified, skipping *".PHP_EOL; |
|
491 | - ob_start(); |
|
492 | - } |
|
493 | - |
|
494 | - return false; |
|
495 | - } |
|
496 | - |
|
497 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
498 | - $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
499 | - if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
500 | - $sniff = $bt[2]['class']; |
|
501 | - $line = $bt[1]['line']; |
|
502 | - } else { |
|
503 | - $sniff = $bt[1]['class']; |
|
504 | - $line = $bt[0]['line']; |
|
505 | - } |
|
506 | - |
|
507 | - $sniff = Util\Common::getSniffCode($sniff); |
|
508 | - |
|
509 | - $tokens = $this->currentFile->getTokens(); |
|
510 | - $type = $tokens[$stackPtr]['type']; |
|
511 | - $tokenLine = $tokens[$stackPtr]['line']; |
|
512 | - $oldContent = Common::prepareForOutput($this->tokens[$stackPtr]); |
|
513 | - $newContent = Common::prepareForOutput($content); |
|
514 | - if (trim($this->tokens[$stackPtr]) === '' && isset($this->tokens[($stackPtr + 1)]) === true) { |
|
515 | - // Add some context for whitespace only changes. |
|
516 | - $append = Common::prepareForOutput($this->tokens[($stackPtr + 1)]); |
|
517 | - $oldContent .= $append; |
|
518 | - $newContent .= $append; |
|
519 | - } |
|
520 | - }//end if |
|
521 | - |
|
522 | - if ($this->inChangeset === true) { |
|
523 | - $this->changeset[$stackPtr] = $content; |
|
524 | - |
|
525 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
526 | - @ob_end_clean(); |
|
527 | - echo "\t\tQ: $sniff:$line replaced token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
528 | - ob_start(); |
|
529 | - } |
|
530 | - |
|
531 | - return true; |
|
532 | - } |
|
533 | - |
|
534 | - if (isset($this->oldTokenValues[$stackPtr]) === false) { |
|
535 | - $this->oldTokenValues[$stackPtr] = [ |
|
536 | - 'curr' => $content, |
|
537 | - 'prev' => $this->tokens[$stackPtr], |
|
538 | - 'loop' => $this->loops, |
|
539 | - ]; |
|
540 | - } else { |
|
541 | - if ($this->oldTokenValues[$stackPtr]['prev'] === $content |
|
542 | - && $this->oldTokenValues[$stackPtr]['loop'] === ($this->loops - 1) |
|
543 | - ) { |
|
544 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
545 | - $indent = "\t"; |
|
546 | - if (empty($this->changeset) === false) { |
|
547 | - $indent .= "\t"; |
|
548 | - } |
|
549 | - |
|
550 | - $loop = $this->oldTokenValues[$stackPtr]['loop']; |
|
551 | - |
|
552 | - @ob_end_clean(); |
|
553 | - echo "$indent**** $sniff:$line has possible conflict with another sniff on loop $loop; caused by the following change ****".PHP_EOL; |
|
554 | - echo "$indent**** replaced token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\" ****".PHP_EOL; |
|
555 | - } |
|
556 | - |
|
557 | - if ($this->oldTokenValues[$stackPtr]['loop'] >= ($this->loops - 1)) { |
|
558 | - $this->inConflict = true; |
|
559 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
560 | - echo "$indent**** ignoring all changes until next loop ****".PHP_EOL; |
|
561 | - } |
|
562 | - } |
|
563 | - |
|
564 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
565 | - ob_start(); |
|
566 | - } |
|
567 | - |
|
568 | - return false; |
|
569 | - }//end if |
|
570 | - |
|
571 | - $this->oldTokenValues[$stackPtr]['prev'] = $this->oldTokenValues[$stackPtr]['curr']; |
|
572 | - $this->oldTokenValues[$stackPtr]['curr'] = $content; |
|
573 | - $this->oldTokenValues[$stackPtr]['loop'] = $this->loops; |
|
574 | - }//end if |
|
575 | - |
|
576 | - $this->fixedTokens[$stackPtr] = $this->tokens[$stackPtr]; |
|
577 | - $this->tokens[$stackPtr] = $content; |
|
578 | - $this->numFixes++; |
|
579 | - |
|
580 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
581 | - $indent = "\t"; |
|
582 | - if (empty($this->changeset) === false) { |
|
583 | - $indent .= "\tA: "; |
|
584 | - } |
|
585 | - |
|
586 | - if (ob_get_level() > 0) { |
|
587 | - ob_end_clean(); |
|
588 | - } |
|
589 | - |
|
590 | - echo "$indent$sniff:$line replaced token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
591 | - ob_start(); |
|
592 | - } |
|
593 | - |
|
594 | - return true; |
|
595 | - |
|
596 | - }//end replaceToken() |
|
597 | - |
|
598 | - |
|
599 | - /** |
|
600 | - * Reverts the previous fix made to a token. |
|
601 | - * |
|
602 | - * @param int $stackPtr The position of the token in the token stack. |
|
603 | - * |
|
604 | - * @return bool If a change was reverted. |
|
605 | - */ |
|
606 | - public function revertToken($stackPtr) |
|
607 | - { |
|
608 | - if (isset($this->fixedTokens[$stackPtr]) === false) { |
|
609 | - return false; |
|
610 | - } |
|
611 | - |
|
612 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
613 | - $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
614 | - if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
615 | - $sniff = $bt[2]['class']; |
|
616 | - $line = $bt[1]['line']; |
|
617 | - } else { |
|
618 | - $sniff = $bt[1]['class']; |
|
619 | - $line = $bt[0]['line']; |
|
620 | - } |
|
621 | - |
|
622 | - $sniff = Util\Common::getSniffCode($sniff); |
|
623 | - |
|
624 | - $tokens = $this->currentFile->getTokens(); |
|
625 | - $type = $tokens[$stackPtr]['type']; |
|
626 | - $tokenLine = $tokens[$stackPtr]['line']; |
|
627 | - $oldContent = Common::prepareForOutput($this->tokens[$stackPtr]); |
|
628 | - $newContent = Common::prepareForOutput($this->fixedTokens[$stackPtr]); |
|
629 | - if (trim($this->tokens[$stackPtr]) === '' && isset($tokens[($stackPtr + 1)]) === true) { |
|
630 | - // Add some context for whitespace only changes. |
|
631 | - $append = Common::prepareForOutput($this->tokens[($stackPtr + 1)]); |
|
632 | - $oldContent .= $append; |
|
633 | - $newContent .= $append; |
|
634 | - } |
|
635 | - }//end if |
|
636 | - |
|
637 | - $this->tokens[$stackPtr] = $this->fixedTokens[$stackPtr]; |
|
638 | - unset($this->fixedTokens[$stackPtr]); |
|
639 | - $this->numFixes--; |
|
640 | - |
|
641 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
642 | - $indent = "\t"; |
|
643 | - if (empty($this->changeset) === false) { |
|
644 | - $indent .= "\tR: "; |
|
645 | - } |
|
646 | - |
|
647 | - @ob_end_clean(); |
|
648 | - echo "$indent$sniff:$line reverted token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
649 | - ob_start(); |
|
650 | - } |
|
651 | - |
|
652 | - return true; |
|
653 | - |
|
654 | - }//end revertToken() |
|
655 | - |
|
656 | - |
|
657 | - /** |
|
658 | - * Replace the content of a token with a part of its current content. |
|
659 | - * |
|
660 | - * @param int $stackPtr The position of the token in the token stack. |
|
661 | - * @param int $start The first character to keep. |
|
662 | - * @param int $length The number of characters to keep. If NULL, the content of |
|
663 | - * the token from $start to the end of the content is kept. |
|
664 | - * |
|
665 | - * @return bool If the change was accepted. |
|
666 | - */ |
|
667 | - public function substrToken($stackPtr, $start, $length=null) |
|
668 | - { |
|
669 | - $current = $this->getTokenContent($stackPtr); |
|
670 | - |
|
671 | - if ($length === null) { |
|
672 | - $newContent = substr($current, $start); |
|
673 | - } else { |
|
674 | - $newContent = substr($current, $start, $length); |
|
675 | - } |
|
676 | - |
|
677 | - return $this->replaceToken($stackPtr, $newContent); |
|
678 | - |
|
679 | - }//end substrToken() |
|
680 | - |
|
681 | - |
|
682 | - /** |
|
683 | - * Adds a newline to end of a token's content. |
|
684 | - * |
|
685 | - * @param int $stackPtr The position of the token in the token stack. |
|
686 | - * |
|
687 | - * @return bool If the change was accepted. |
|
688 | - */ |
|
689 | - public function addNewline($stackPtr) |
|
690 | - { |
|
691 | - $current = $this->getTokenContent($stackPtr); |
|
692 | - return $this->replaceToken($stackPtr, $current.$this->currentFile->eolChar); |
|
693 | - |
|
694 | - }//end addNewline() |
|
695 | - |
|
696 | - |
|
697 | - /** |
|
698 | - * Adds a newline to the start of a token's content. |
|
699 | - * |
|
700 | - * @param int $stackPtr The position of the token in the token stack. |
|
701 | - * |
|
702 | - * @return bool If the change was accepted. |
|
703 | - */ |
|
704 | - public function addNewlineBefore($stackPtr) |
|
705 | - { |
|
706 | - $current = $this->getTokenContent($stackPtr); |
|
707 | - return $this->replaceToken($stackPtr, $this->currentFile->eolChar.$current); |
|
708 | - |
|
709 | - }//end addNewlineBefore() |
|
710 | - |
|
711 | - |
|
712 | - /** |
|
713 | - * Adds content to the end of a token's current content. |
|
714 | - * |
|
715 | - * @param int $stackPtr The position of the token in the token stack. |
|
716 | - * @param string $content The content to add. |
|
717 | - * |
|
718 | - * @return bool If the change was accepted. |
|
719 | - */ |
|
720 | - public function addContent($stackPtr, $content) |
|
721 | - { |
|
722 | - $current = $this->getTokenContent($stackPtr); |
|
723 | - return $this->replaceToken($stackPtr, $current.$content); |
|
724 | - |
|
725 | - }//end addContent() |
|
726 | - |
|
727 | - |
|
728 | - /** |
|
729 | - * Adds content to the start of a token's current content. |
|
730 | - * |
|
731 | - * @param int $stackPtr The position of the token in the token stack. |
|
732 | - * @param string $content The content to add. |
|
733 | - * |
|
734 | - * @return bool If the change was accepted. |
|
735 | - */ |
|
736 | - public function addContentBefore($stackPtr, $content) |
|
737 | - { |
|
738 | - $current = $this->getTokenContent($stackPtr); |
|
739 | - return $this->replaceToken($stackPtr, $content.$current); |
|
740 | - |
|
741 | - }//end addContentBefore() |
|
742 | - |
|
743 | - |
|
744 | - /** |
|
745 | - * Adjust the indent of a code block. |
|
746 | - * |
|
747 | - * @param int $start The position of the token in the token stack |
|
748 | - * to start adjusting the indent from. |
|
749 | - * @param int $end The position of the token in the token stack |
|
750 | - * to end adjusting the indent. |
|
751 | - * @param int $change The number of spaces to adjust the indent by |
|
752 | - * (positive or negative). |
|
753 | - * |
|
754 | - * @return void |
|
755 | - */ |
|
756 | - public function changeCodeBlockIndent($start, $end, $change) |
|
757 | - { |
|
758 | - $tokens = $this->currentFile->getTokens(); |
|
759 | - |
|
760 | - $baseIndent = ''; |
|
761 | - if ($change > 0) { |
|
762 | - $baseIndent = str_repeat(' ', $change); |
|
763 | - } |
|
764 | - |
|
765 | - $useChangeset = false; |
|
766 | - if ($this->inChangeset === false) { |
|
767 | - $this->beginChangeset(); |
|
768 | - $useChangeset = true; |
|
769 | - } |
|
770 | - |
|
771 | - for ($i = $start; $i <= $end; $i++) { |
|
772 | - if ($tokens[$i]['column'] !== 1 |
|
773 | - || $tokens[($i + 1)]['line'] !== $tokens[$i]['line'] |
|
774 | - ) { |
|
775 | - continue; |
|
776 | - } |
|
777 | - |
|
778 | - $length = 0; |
|
779 | - if ($tokens[$i]['code'] === T_WHITESPACE |
|
780 | - || $tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE |
|
781 | - ) { |
|
782 | - $length = $tokens[$i]['length']; |
|
783 | - |
|
784 | - $padding = ($length + $change); |
|
785 | - if ($padding > 0) { |
|
786 | - $padding = str_repeat(' ', $padding); |
|
787 | - } else { |
|
788 | - $padding = ''; |
|
789 | - } |
|
790 | - |
|
791 | - $newContent = $padding.ltrim($tokens[$i]['content']); |
|
792 | - } else { |
|
793 | - $newContent = $baseIndent.$tokens[$i]['content']; |
|
794 | - } |
|
795 | - |
|
796 | - $this->replaceToken($i, $newContent); |
|
797 | - }//end for |
|
798 | - |
|
799 | - if ($useChangeset === true) { |
|
800 | - $this->endChangeset(); |
|
801 | - } |
|
802 | - |
|
803 | - }//end changeCodeBlockIndent() |
|
21 | + /** |
|
22 | + * Is the fixer enabled and fixing a file? |
|
23 | + * |
|
24 | + * Sniffs should check this value to ensure they are not |
|
25 | + * doing extra processing to prepare for a fix when fixing is |
|
26 | + * not required. |
|
27 | + * |
|
28 | + * @var boolean |
|
29 | + */ |
|
30 | + public $enabled = false; |
|
31 | + |
|
32 | + /** |
|
33 | + * The number of times we have looped over a file. |
|
34 | + * |
|
35 | + * @var integer |
|
36 | + */ |
|
37 | + public $loops = 0; |
|
38 | + |
|
39 | + /** |
|
40 | + * The file being fixed. |
|
41 | + * |
|
42 | + * @var \PHP_CodeSniffer\Files\File |
|
43 | + */ |
|
44 | + private $currentFile = null; |
|
45 | + |
|
46 | + /** |
|
47 | + * The list of tokens that make up the file contents. |
|
48 | + * |
|
49 | + * This is a simplified list which just contains the token content and nothing |
|
50 | + * else. This is the array that is updated as fixes are made, not the file's |
|
51 | + * token array. Imploding this array will give you the file content back. |
|
52 | + * |
|
53 | + * @var array<int, string> |
|
54 | + */ |
|
55 | + private $tokens = []; |
|
56 | + |
|
57 | + /** |
|
58 | + * A list of tokens that have already been fixed. |
|
59 | + * |
|
60 | + * We don't allow the same token to be fixed more than once each time |
|
61 | + * through a file as this can easily cause conflicts between sniffs. |
|
62 | + * |
|
63 | + * @var int[] |
|
64 | + */ |
|
65 | + private $fixedTokens = []; |
|
66 | + |
|
67 | + /** |
|
68 | + * The last value of each fixed token. |
|
69 | + * |
|
70 | + * If a token is being "fixed" back to its last value, the fix is |
|
71 | + * probably conflicting with another. |
|
72 | + * |
|
73 | + * @var array<int, string> |
|
74 | + */ |
|
75 | + private $oldTokenValues = []; |
|
76 | + |
|
77 | + /** |
|
78 | + * A list of tokens that have been fixed during a changeset. |
|
79 | + * |
|
80 | + * All changes in changeset must be able to be applied, or else |
|
81 | + * the entire changeset is rejected. |
|
82 | + * |
|
83 | + * @var array |
|
84 | + */ |
|
85 | + private $changeset = []; |
|
86 | + |
|
87 | + /** |
|
88 | + * Is there an open changeset. |
|
89 | + * |
|
90 | + * @var boolean |
|
91 | + */ |
|
92 | + private $inChangeset = false; |
|
93 | + |
|
94 | + /** |
|
95 | + * Is the current fixing loop in conflict? |
|
96 | + * |
|
97 | + * @var boolean |
|
98 | + */ |
|
99 | + private $inConflict = false; |
|
100 | + |
|
101 | + /** |
|
102 | + * The number of fixes that have been performed. |
|
103 | + * |
|
104 | + * @var integer |
|
105 | + */ |
|
106 | + private $numFixes = 0; |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * Starts fixing a new file. |
|
111 | + * |
|
112 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being fixed. |
|
113 | + * |
|
114 | + * @return void |
|
115 | + */ |
|
116 | + public function startFile(File $phpcsFile) |
|
117 | + { |
|
118 | + $this->currentFile = $phpcsFile; |
|
119 | + $this->numFixes = 0; |
|
120 | + $this->fixedTokens = []; |
|
121 | + |
|
122 | + $tokens = $phpcsFile->getTokens(); |
|
123 | + $this->tokens = []; |
|
124 | + foreach ($tokens as $index => $token) { |
|
125 | + if (isset($token['orig_content']) === true) { |
|
126 | + $this->tokens[$index] = $token['orig_content']; |
|
127 | + } else { |
|
128 | + $this->tokens[$index] = $token['content']; |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + }//end startFile() |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * Attempt to fix the file by processing it until no fixes are made. |
|
137 | + * |
|
138 | + * @return boolean |
|
139 | + */ |
|
140 | + public function fixFile() |
|
141 | + { |
|
142 | + $fixable = $this->currentFile->getFixableCount(); |
|
143 | + if ($fixable === 0) { |
|
144 | + // Nothing to fix. |
|
145 | + return false; |
|
146 | + } |
|
147 | + |
|
148 | + $this->enabled = true; |
|
149 | + |
|
150 | + $this->loops = 0; |
|
151 | + while ($this->loops < 50) { |
|
152 | + ob_start(); |
|
153 | + |
|
154 | + // Only needed once file content has changed. |
|
155 | + $contents = $this->getContents(); |
|
156 | + |
|
157 | + if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
158 | + @ob_end_clean(); |
|
159 | + echo '---START FILE CONTENT---'.PHP_EOL; |
|
160 | + $lines = explode($this->currentFile->eolChar, $contents); |
|
161 | + $max = strlen(count($lines)); |
|
162 | + foreach ($lines as $lineNum => $line) { |
|
163 | + $lineNum++; |
|
164 | + echo str_pad($lineNum, $max, ' ', STR_PAD_LEFT).'|'.$line.PHP_EOL; |
|
165 | + } |
|
166 | + |
|
167 | + echo '--- END FILE CONTENT ---'.PHP_EOL; |
|
168 | + ob_start(); |
|
169 | + } |
|
170 | + |
|
171 | + $this->inConflict = false; |
|
172 | + $this->currentFile->ruleset->populateTokenListeners(); |
|
173 | + $this->currentFile->setContent($contents); |
|
174 | + $this->currentFile->process(); |
|
175 | + ob_end_clean(); |
|
176 | + |
|
177 | + $this->loops++; |
|
178 | + |
|
179 | + if (PHP_CODESNIFFER_CBF === true && PHP_CODESNIFFER_VERBOSITY > 0) { |
|
180 | + echo "\r".str_repeat(' ', 80)."\r"; |
|
181 | + echo "\t=> Fixing file: $this->numFixes/$fixable violations remaining [made $this->loops pass"; |
|
182 | + if ($this->loops > 1) { |
|
183 | + echo 'es'; |
|
184 | + } |
|
185 | + |
|
186 | + echo ']... '; |
|
187 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
188 | + echo PHP_EOL; |
|
189 | + } |
|
190 | + } |
|
191 | + |
|
192 | + if ($this->numFixes === 0 && $this->inConflict === false) { |
|
193 | + // Nothing left to do. |
|
194 | + break; |
|
195 | + } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
196 | + echo "\t* fixed $this->numFixes violations, starting loop ".($this->loops + 1).' *'.PHP_EOL; |
|
197 | + } |
|
198 | + }//end while |
|
199 | + |
|
200 | + $this->enabled = false; |
|
201 | + |
|
202 | + if ($this->numFixes > 0) { |
|
203 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
204 | + if (ob_get_level() > 0) { |
|
205 | + ob_end_clean(); |
|
206 | + } |
|
207 | + |
|
208 | + echo "\t*** Reached maximum number of loops with $this->numFixes violations left unfixed ***".PHP_EOL; |
|
209 | + ob_start(); |
|
210 | + } |
|
211 | + |
|
212 | + return false; |
|
213 | + } |
|
214 | + |
|
215 | + return true; |
|
216 | + |
|
217 | + }//end fixFile() |
|
218 | + |
|
219 | + |
|
220 | + /** |
|
221 | + * Generates a text diff of the original file and the new content. |
|
222 | + * |
|
223 | + * @param string $filePath Optional file path to diff the file against. |
|
224 | + * If not specified, the original version of the |
|
225 | + * file will be used. |
|
226 | + * @param boolean $colors Print coloured output or not. |
|
227 | + * |
|
228 | + * @return string |
|
229 | + */ |
|
230 | + public function generateDiff($filePath=null, $colors=true) |
|
231 | + { |
|
232 | + if ($filePath === null) { |
|
233 | + $filePath = $this->currentFile->getFilename(); |
|
234 | + } |
|
235 | + |
|
236 | + $cwd = getcwd().DIRECTORY_SEPARATOR; |
|
237 | + if (strpos($filePath, $cwd) === 0) { |
|
238 | + $filename = substr($filePath, strlen($cwd)); |
|
239 | + } else { |
|
240 | + $filename = $filePath; |
|
241 | + } |
|
242 | + |
|
243 | + $contents = $this->getContents(); |
|
244 | + |
|
245 | + $tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer'); |
|
246 | + $fixedFile = fopen($tempName, 'w'); |
|
247 | + fwrite($fixedFile, $contents); |
|
248 | + |
|
249 | + // We must use something like shell_exec() because whitespace at the end |
|
250 | + // of lines is critical to diff files. |
|
251 | + $filename = escapeshellarg($filename); |
|
252 | + $cmd = "diff -u -L$filename -LPHP_CodeSniffer $filename \"$tempName\""; |
|
253 | + |
|
254 | + $diff = shell_exec($cmd); |
|
255 | + |
|
256 | + fclose($fixedFile); |
|
257 | + if (is_file($tempName) === true) { |
|
258 | + unlink($tempName); |
|
259 | + } |
|
260 | + |
|
261 | + if ($diff === null) { |
|
262 | + return ''; |
|
263 | + } |
|
264 | + |
|
265 | + if ($colors === false) { |
|
266 | + return $diff; |
|
267 | + } |
|
268 | + |
|
269 | + $diffLines = explode(PHP_EOL, $diff); |
|
270 | + if (count($diffLines) === 1) { |
|
271 | + // Seems to be required for cygwin. |
|
272 | + $diffLines = explode("\n", $diff); |
|
273 | + } |
|
274 | + |
|
275 | + $diff = []; |
|
276 | + foreach ($diffLines as $line) { |
|
277 | + if (isset($line[0]) === true) { |
|
278 | + switch ($line[0]) { |
|
279 | + case '-': |
|
280 | + $diff[] = "\033[31m$line\033[0m"; |
|
281 | + break; |
|
282 | + case '+': |
|
283 | + $diff[] = "\033[32m$line\033[0m"; |
|
284 | + break; |
|
285 | + default: |
|
286 | + $diff[] = $line; |
|
287 | + } |
|
288 | + } |
|
289 | + } |
|
290 | + |
|
291 | + $diff = implode(PHP_EOL, $diff); |
|
292 | + |
|
293 | + return $diff; |
|
294 | + |
|
295 | + }//end generateDiff() |
|
296 | + |
|
297 | + |
|
298 | + /** |
|
299 | + * Get a count of fixes that have been performed on the file. |
|
300 | + * |
|
301 | + * This value is reset every time a new file is started, or an existing |
|
302 | + * file is restarted. |
|
303 | + * |
|
304 | + * @return int |
|
305 | + */ |
|
306 | + public function getFixCount() |
|
307 | + { |
|
308 | + return $this->numFixes; |
|
309 | + |
|
310 | + }//end getFixCount() |
|
311 | + |
|
312 | + |
|
313 | + /** |
|
314 | + * Get the current content of the file, as a string. |
|
315 | + * |
|
316 | + * @return string |
|
317 | + */ |
|
318 | + public function getContents() |
|
319 | + { |
|
320 | + $contents = implode($this->tokens); |
|
321 | + return $contents; |
|
322 | + |
|
323 | + }//end getContents() |
|
324 | + |
|
325 | + |
|
326 | + /** |
|
327 | + * Get the current fixed content of a token. |
|
328 | + * |
|
329 | + * This function takes changesets into account so should be used |
|
330 | + * instead of directly accessing the token array. |
|
331 | + * |
|
332 | + * @param int $stackPtr The position of the token in the token stack. |
|
333 | + * |
|
334 | + * @return string |
|
335 | + */ |
|
336 | + public function getTokenContent($stackPtr) |
|
337 | + { |
|
338 | + if ($this->inChangeset === true |
|
339 | + && isset($this->changeset[$stackPtr]) === true |
|
340 | + ) { |
|
341 | + return $this->changeset[$stackPtr]; |
|
342 | + } else { |
|
343 | + return $this->tokens[$stackPtr]; |
|
344 | + } |
|
345 | + |
|
346 | + }//end getTokenContent() |
|
347 | + |
|
348 | + |
|
349 | + /** |
|
350 | + * Start recording actions for a changeset. |
|
351 | + * |
|
352 | + * @return void |
|
353 | + */ |
|
354 | + public function beginChangeset() |
|
355 | + { |
|
356 | + if ($this->inConflict === true) { |
|
357 | + return false; |
|
358 | + } |
|
359 | + |
|
360 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
361 | + $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
362 | + if ($bt[1]['class'] === __CLASS__) { |
|
363 | + $sniff = 'Fixer'; |
|
364 | + } else { |
|
365 | + $sniff = Util\Common::getSniffCode($bt[1]['class']); |
|
366 | + } |
|
367 | + |
|
368 | + $line = $bt[0]['line']; |
|
369 | + |
|
370 | + @ob_end_clean(); |
|
371 | + echo "\t=> Changeset started by $sniff:$line".PHP_EOL; |
|
372 | + ob_start(); |
|
373 | + } |
|
374 | + |
|
375 | + $this->changeset = []; |
|
376 | + $this->inChangeset = true; |
|
377 | + |
|
378 | + }//end beginChangeset() |
|
379 | + |
|
380 | + |
|
381 | + /** |
|
382 | + * Stop recording actions for a changeset, and apply logged changes. |
|
383 | + * |
|
384 | + * @return boolean |
|
385 | + */ |
|
386 | + public function endChangeset() |
|
387 | + { |
|
388 | + if ($this->inConflict === true) { |
|
389 | + return false; |
|
390 | + } |
|
391 | + |
|
392 | + $this->inChangeset = false; |
|
393 | + |
|
394 | + $success = true; |
|
395 | + $applied = []; |
|
396 | + foreach ($this->changeset as $stackPtr => $content) { |
|
397 | + $success = $this->replaceToken($stackPtr, $content); |
|
398 | + if ($success === false) { |
|
399 | + break; |
|
400 | + } else { |
|
401 | + $applied[] = $stackPtr; |
|
402 | + } |
|
403 | + } |
|
404 | + |
|
405 | + if ($success === false) { |
|
406 | + // Rolling back all changes. |
|
407 | + foreach ($applied as $stackPtr) { |
|
408 | + $this->revertToken($stackPtr); |
|
409 | + } |
|
410 | + |
|
411 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
412 | + @ob_end_clean(); |
|
413 | + echo "\t=> Changeset failed to apply".PHP_EOL; |
|
414 | + ob_start(); |
|
415 | + } |
|
416 | + } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
417 | + $fixes = count($this->changeset); |
|
418 | + @ob_end_clean(); |
|
419 | + echo "\t=> Changeset ended: $fixes changes applied".PHP_EOL; |
|
420 | + ob_start(); |
|
421 | + } |
|
422 | + |
|
423 | + $this->changeset = []; |
|
424 | + return true; |
|
425 | + |
|
426 | + }//end endChangeset() |
|
427 | + |
|
428 | + |
|
429 | + /** |
|
430 | + * Stop recording actions for a changeset, and discard logged changes. |
|
431 | + * |
|
432 | + * @return void |
|
433 | + */ |
|
434 | + public function rollbackChangeset() |
|
435 | + { |
|
436 | + $this->inChangeset = false; |
|
437 | + $this->inConflict = false; |
|
438 | + |
|
439 | + if (empty($this->changeset) === false) { |
|
440 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
441 | + $bt = debug_backtrace(); |
|
442 | + if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
443 | + $sniff = $bt[2]['class']; |
|
444 | + $line = $bt[1]['line']; |
|
445 | + } else { |
|
446 | + $sniff = $bt[1]['class']; |
|
447 | + $line = $bt[0]['line']; |
|
448 | + } |
|
449 | + |
|
450 | + $sniff = Util\Common::getSniffCode($sniff); |
|
451 | + |
|
452 | + $numChanges = count($this->changeset); |
|
453 | + |
|
454 | + @ob_end_clean(); |
|
455 | + echo "\t\tR: $sniff:$line rolled back the changeset ($numChanges changes)".PHP_EOL; |
|
456 | + echo "\t=> Changeset rolled back".PHP_EOL; |
|
457 | + ob_start(); |
|
458 | + } |
|
459 | + |
|
460 | + $this->changeset = []; |
|
461 | + }//end if |
|
462 | + |
|
463 | + }//end rollbackChangeset() |
|
464 | + |
|
465 | + |
|
466 | + /** |
|
467 | + * Replace the entire contents of a token. |
|
468 | + * |
|
469 | + * @param int $stackPtr The position of the token in the token stack. |
|
470 | + * @param string $content The new content of the token. |
|
471 | + * |
|
472 | + * @return bool If the change was accepted. |
|
473 | + */ |
|
474 | + public function replaceToken($stackPtr, $content) |
|
475 | + { |
|
476 | + if ($this->inConflict === true) { |
|
477 | + return false; |
|
478 | + } |
|
479 | + |
|
480 | + if ($this->inChangeset === false |
|
481 | + && isset($this->fixedTokens[$stackPtr]) === true |
|
482 | + ) { |
|
483 | + $indent = "\t"; |
|
484 | + if (empty($this->changeset) === false) { |
|
485 | + $indent .= "\t"; |
|
486 | + } |
|
487 | + |
|
488 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
489 | + @ob_end_clean(); |
|
490 | + echo "$indent* token $stackPtr has already been modified, skipping *".PHP_EOL; |
|
491 | + ob_start(); |
|
492 | + } |
|
493 | + |
|
494 | + return false; |
|
495 | + } |
|
496 | + |
|
497 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
498 | + $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
499 | + if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
500 | + $sniff = $bt[2]['class']; |
|
501 | + $line = $bt[1]['line']; |
|
502 | + } else { |
|
503 | + $sniff = $bt[1]['class']; |
|
504 | + $line = $bt[0]['line']; |
|
505 | + } |
|
506 | + |
|
507 | + $sniff = Util\Common::getSniffCode($sniff); |
|
508 | + |
|
509 | + $tokens = $this->currentFile->getTokens(); |
|
510 | + $type = $tokens[$stackPtr]['type']; |
|
511 | + $tokenLine = $tokens[$stackPtr]['line']; |
|
512 | + $oldContent = Common::prepareForOutput($this->tokens[$stackPtr]); |
|
513 | + $newContent = Common::prepareForOutput($content); |
|
514 | + if (trim($this->tokens[$stackPtr]) === '' && isset($this->tokens[($stackPtr + 1)]) === true) { |
|
515 | + // Add some context for whitespace only changes. |
|
516 | + $append = Common::prepareForOutput($this->tokens[($stackPtr + 1)]); |
|
517 | + $oldContent .= $append; |
|
518 | + $newContent .= $append; |
|
519 | + } |
|
520 | + }//end if |
|
521 | + |
|
522 | + if ($this->inChangeset === true) { |
|
523 | + $this->changeset[$stackPtr] = $content; |
|
524 | + |
|
525 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
526 | + @ob_end_clean(); |
|
527 | + echo "\t\tQ: $sniff:$line replaced token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
528 | + ob_start(); |
|
529 | + } |
|
530 | + |
|
531 | + return true; |
|
532 | + } |
|
533 | + |
|
534 | + if (isset($this->oldTokenValues[$stackPtr]) === false) { |
|
535 | + $this->oldTokenValues[$stackPtr] = [ |
|
536 | + 'curr' => $content, |
|
537 | + 'prev' => $this->tokens[$stackPtr], |
|
538 | + 'loop' => $this->loops, |
|
539 | + ]; |
|
540 | + } else { |
|
541 | + if ($this->oldTokenValues[$stackPtr]['prev'] === $content |
|
542 | + && $this->oldTokenValues[$stackPtr]['loop'] === ($this->loops - 1) |
|
543 | + ) { |
|
544 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
545 | + $indent = "\t"; |
|
546 | + if (empty($this->changeset) === false) { |
|
547 | + $indent .= "\t"; |
|
548 | + } |
|
549 | + |
|
550 | + $loop = $this->oldTokenValues[$stackPtr]['loop']; |
|
551 | + |
|
552 | + @ob_end_clean(); |
|
553 | + echo "$indent**** $sniff:$line has possible conflict with another sniff on loop $loop; caused by the following change ****".PHP_EOL; |
|
554 | + echo "$indent**** replaced token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\" ****".PHP_EOL; |
|
555 | + } |
|
556 | + |
|
557 | + if ($this->oldTokenValues[$stackPtr]['loop'] >= ($this->loops - 1)) { |
|
558 | + $this->inConflict = true; |
|
559 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
560 | + echo "$indent**** ignoring all changes until next loop ****".PHP_EOL; |
|
561 | + } |
|
562 | + } |
|
563 | + |
|
564 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
565 | + ob_start(); |
|
566 | + } |
|
567 | + |
|
568 | + return false; |
|
569 | + }//end if |
|
570 | + |
|
571 | + $this->oldTokenValues[$stackPtr]['prev'] = $this->oldTokenValues[$stackPtr]['curr']; |
|
572 | + $this->oldTokenValues[$stackPtr]['curr'] = $content; |
|
573 | + $this->oldTokenValues[$stackPtr]['loop'] = $this->loops; |
|
574 | + }//end if |
|
575 | + |
|
576 | + $this->fixedTokens[$stackPtr] = $this->tokens[$stackPtr]; |
|
577 | + $this->tokens[$stackPtr] = $content; |
|
578 | + $this->numFixes++; |
|
579 | + |
|
580 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
581 | + $indent = "\t"; |
|
582 | + if (empty($this->changeset) === false) { |
|
583 | + $indent .= "\tA: "; |
|
584 | + } |
|
585 | + |
|
586 | + if (ob_get_level() > 0) { |
|
587 | + ob_end_clean(); |
|
588 | + } |
|
589 | + |
|
590 | + echo "$indent$sniff:$line replaced token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
591 | + ob_start(); |
|
592 | + } |
|
593 | + |
|
594 | + return true; |
|
595 | + |
|
596 | + }//end replaceToken() |
|
597 | + |
|
598 | + |
|
599 | + /** |
|
600 | + * Reverts the previous fix made to a token. |
|
601 | + * |
|
602 | + * @param int $stackPtr The position of the token in the token stack. |
|
603 | + * |
|
604 | + * @return bool If a change was reverted. |
|
605 | + */ |
|
606 | + public function revertToken($stackPtr) |
|
607 | + { |
|
608 | + if (isset($this->fixedTokens[$stackPtr]) === false) { |
|
609 | + return false; |
|
610 | + } |
|
611 | + |
|
612 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
613 | + $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
614 | + if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { |
|
615 | + $sniff = $bt[2]['class']; |
|
616 | + $line = $bt[1]['line']; |
|
617 | + } else { |
|
618 | + $sniff = $bt[1]['class']; |
|
619 | + $line = $bt[0]['line']; |
|
620 | + } |
|
621 | + |
|
622 | + $sniff = Util\Common::getSniffCode($sniff); |
|
623 | + |
|
624 | + $tokens = $this->currentFile->getTokens(); |
|
625 | + $type = $tokens[$stackPtr]['type']; |
|
626 | + $tokenLine = $tokens[$stackPtr]['line']; |
|
627 | + $oldContent = Common::prepareForOutput($this->tokens[$stackPtr]); |
|
628 | + $newContent = Common::prepareForOutput($this->fixedTokens[$stackPtr]); |
|
629 | + if (trim($this->tokens[$stackPtr]) === '' && isset($tokens[($stackPtr + 1)]) === true) { |
|
630 | + // Add some context for whitespace only changes. |
|
631 | + $append = Common::prepareForOutput($this->tokens[($stackPtr + 1)]); |
|
632 | + $oldContent .= $append; |
|
633 | + $newContent .= $append; |
|
634 | + } |
|
635 | + }//end if |
|
636 | + |
|
637 | + $this->tokens[$stackPtr] = $this->fixedTokens[$stackPtr]; |
|
638 | + unset($this->fixedTokens[$stackPtr]); |
|
639 | + $this->numFixes--; |
|
640 | + |
|
641 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
642 | + $indent = "\t"; |
|
643 | + if (empty($this->changeset) === false) { |
|
644 | + $indent .= "\tR: "; |
|
645 | + } |
|
646 | + |
|
647 | + @ob_end_clean(); |
|
648 | + echo "$indent$sniff:$line reverted token $stackPtr ($type on line $tokenLine) \"$oldContent\" => \"$newContent\"".PHP_EOL; |
|
649 | + ob_start(); |
|
650 | + } |
|
651 | + |
|
652 | + return true; |
|
653 | + |
|
654 | + }//end revertToken() |
|
655 | + |
|
656 | + |
|
657 | + /** |
|
658 | + * Replace the content of a token with a part of its current content. |
|
659 | + * |
|
660 | + * @param int $stackPtr The position of the token in the token stack. |
|
661 | + * @param int $start The first character to keep. |
|
662 | + * @param int $length The number of characters to keep. If NULL, the content of |
|
663 | + * the token from $start to the end of the content is kept. |
|
664 | + * |
|
665 | + * @return bool If the change was accepted. |
|
666 | + */ |
|
667 | + public function substrToken($stackPtr, $start, $length=null) |
|
668 | + { |
|
669 | + $current = $this->getTokenContent($stackPtr); |
|
670 | + |
|
671 | + if ($length === null) { |
|
672 | + $newContent = substr($current, $start); |
|
673 | + } else { |
|
674 | + $newContent = substr($current, $start, $length); |
|
675 | + } |
|
676 | + |
|
677 | + return $this->replaceToken($stackPtr, $newContent); |
|
678 | + |
|
679 | + }//end substrToken() |
|
680 | + |
|
681 | + |
|
682 | + /** |
|
683 | + * Adds a newline to end of a token's content. |
|
684 | + * |
|
685 | + * @param int $stackPtr The position of the token in the token stack. |
|
686 | + * |
|
687 | + * @return bool If the change was accepted. |
|
688 | + */ |
|
689 | + public function addNewline($stackPtr) |
|
690 | + { |
|
691 | + $current = $this->getTokenContent($stackPtr); |
|
692 | + return $this->replaceToken($stackPtr, $current.$this->currentFile->eolChar); |
|
693 | + |
|
694 | + }//end addNewline() |
|
695 | + |
|
696 | + |
|
697 | + /** |
|
698 | + * Adds a newline to the start of a token's content. |
|
699 | + * |
|
700 | + * @param int $stackPtr The position of the token in the token stack. |
|
701 | + * |
|
702 | + * @return bool If the change was accepted. |
|
703 | + */ |
|
704 | + public function addNewlineBefore($stackPtr) |
|
705 | + { |
|
706 | + $current = $this->getTokenContent($stackPtr); |
|
707 | + return $this->replaceToken($stackPtr, $this->currentFile->eolChar.$current); |
|
708 | + |
|
709 | + }//end addNewlineBefore() |
|
710 | + |
|
711 | + |
|
712 | + /** |
|
713 | + * Adds content to the end of a token's current content. |
|
714 | + * |
|
715 | + * @param int $stackPtr The position of the token in the token stack. |
|
716 | + * @param string $content The content to add. |
|
717 | + * |
|
718 | + * @return bool If the change was accepted. |
|
719 | + */ |
|
720 | + public function addContent($stackPtr, $content) |
|
721 | + { |
|
722 | + $current = $this->getTokenContent($stackPtr); |
|
723 | + return $this->replaceToken($stackPtr, $current.$content); |
|
724 | + |
|
725 | + }//end addContent() |
|
726 | + |
|
727 | + |
|
728 | + /** |
|
729 | + * Adds content to the start of a token's current content. |
|
730 | + * |
|
731 | + * @param int $stackPtr The position of the token in the token stack. |
|
732 | + * @param string $content The content to add. |
|
733 | + * |
|
734 | + * @return bool If the change was accepted. |
|
735 | + */ |
|
736 | + public function addContentBefore($stackPtr, $content) |
|
737 | + { |
|
738 | + $current = $this->getTokenContent($stackPtr); |
|
739 | + return $this->replaceToken($stackPtr, $content.$current); |
|
740 | + |
|
741 | + }//end addContentBefore() |
|
742 | + |
|
743 | + |
|
744 | + /** |
|
745 | + * Adjust the indent of a code block. |
|
746 | + * |
|
747 | + * @param int $start The position of the token in the token stack |
|
748 | + * to start adjusting the indent from. |
|
749 | + * @param int $end The position of the token in the token stack |
|
750 | + * to end adjusting the indent. |
|
751 | + * @param int $change The number of spaces to adjust the indent by |
|
752 | + * (positive or negative). |
|
753 | + * |
|
754 | + * @return void |
|
755 | + */ |
|
756 | + public function changeCodeBlockIndent($start, $end, $change) |
|
757 | + { |
|
758 | + $tokens = $this->currentFile->getTokens(); |
|
759 | + |
|
760 | + $baseIndent = ''; |
|
761 | + if ($change > 0) { |
|
762 | + $baseIndent = str_repeat(' ', $change); |
|
763 | + } |
|
764 | + |
|
765 | + $useChangeset = false; |
|
766 | + if ($this->inChangeset === false) { |
|
767 | + $this->beginChangeset(); |
|
768 | + $useChangeset = true; |
|
769 | + } |
|
770 | + |
|
771 | + for ($i = $start; $i <= $end; $i++) { |
|
772 | + if ($tokens[$i]['column'] !== 1 |
|
773 | + || $tokens[($i + 1)]['line'] !== $tokens[$i]['line'] |
|
774 | + ) { |
|
775 | + continue; |
|
776 | + } |
|
777 | + |
|
778 | + $length = 0; |
|
779 | + if ($tokens[$i]['code'] === T_WHITESPACE |
|
780 | + || $tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE |
|
781 | + ) { |
|
782 | + $length = $tokens[$i]['length']; |
|
783 | + |
|
784 | + $padding = ($length + $change); |
|
785 | + if ($padding > 0) { |
|
786 | + $padding = str_repeat(' ', $padding); |
|
787 | + } else { |
|
788 | + $padding = ''; |
|
789 | + } |
|
790 | + |
|
791 | + $newContent = $padding.ltrim($tokens[$i]['content']); |
|
792 | + } else { |
|
793 | + $newContent = $baseIndent.$tokens[$i]['content']; |
|
794 | + } |
|
795 | + |
|
796 | + $this->replaceToken($i, $newContent); |
|
797 | + }//end for |
|
798 | + |
|
799 | + if ($useChangeset === true) { |
|
800 | + $this->endChangeset(); |
|
801 | + } |
|
802 | + |
|
803 | + }//end changeCodeBlockIndent() |
|
804 | 804 | |
805 | 805 | |
806 | 806 | }//end class |
@@ -15,263 +15,263 @@ |
||
15 | 15 | { |
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * Creates an array of tokens when given some PHP code. |
|
20 | - * |
|
21 | - * Starts by using token_get_all() but does a lot of extra processing |
|
22 | - * to insert information about the context of the token. |
|
23 | - * |
|
24 | - * @param string $string The string to tokenize. |
|
25 | - * @param string $eolChar The EOL character to use for splitting strings. |
|
26 | - * @param int $stackPtr The position of the first token in the file. |
|
27 | - * |
|
28 | - * @return array |
|
29 | - */ |
|
30 | - public function tokenizeString($string, $eolChar, $stackPtr) |
|
31 | - { |
|
32 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
33 | - echo "\t\t*** START COMMENT TOKENIZING ***".PHP_EOL; |
|
34 | - } |
|
35 | - |
|
36 | - $tokens = []; |
|
37 | - $numChars = strlen($string); |
|
38 | - |
|
39 | - /* |
|
18 | + /** |
|
19 | + * Creates an array of tokens when given some PHP code. |
|
20 | + * |
|
21 | + * Starts by using token_get_all() but does a lot of extra processing |
|
22 | + * to insert information about the context of the token. |
|
23 | + * |
|
24 | + * @param string $string The string to tokenize. |
|
25 | + * @param string $eolChar The EOL character to use for splitting strings. |
|
26 | + * @param int $stackPtr The position of the first token in the file. |
|
27 | + * |
|
28 | + * @return array |
|
29 | + */ |
|
30 | + public function tokenizeString($string, $eolChar, $stackPtr) |
|
31 | + { |
|
32 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
33 | + echo "\t\t*** START COMMENT TOKENIZING ***".PHP_EOL; |
|
34 | + } |
|
35 | + |
|
36 | + $tokens = []; |
|
37 | + $numChars = strlen($string); |
|
38 | + |
|
39 | + /* |
|
40 | 40 | Doc block comments start with /*, but typically contain an |
41 | 41 | extra star when they are used for function and class comments. |
42 | 42 | */ |
43 | 43 | |
44 | - $char = ($numChars - strlen(ltrim($string, '/*'))); |
|
45 | - $openTag = substr($string, 0, $char); |
|
46 | - $string = ltrim($string, '/*'); |
|
44 | + $char = ($numChars - strlen(ltrim($string, '/*'))); |
|
45 | + $openTag = substr($string, 0, $char); |
|
46 | + $string = ltrim($string, '/*'); |
|
47 | 47 | |
48 | - $tokens[$stackPtr] = [ |
|
49 | - 'content' => $openTag, |
|
50 | - 'code' => T_DOC_COMMENT_OPEN_TAG, |
|
51 | - 'type' => 'T_DOC_COMMENT_OPEN_TAG', |
|
52 | - 'comment_tags' => [], |
|
53 | - ]; |
|
48 | + $tokens[$stackPtr] = [ |
|
49 | + 'content' => $openTag, |
|
50 | + 'code' => T_DOC_COMMENT_OPEN_TAG, |
|
51 | + 'type' => 'T_DOC_COMMENT_OPEN_TAG', |
|
52 | + 'comment_tags' => [], |
|
53 | + ]; |
|
54 | 54 | |
55 | - $openPtr = $stackPtr; |
|
56 | - $stackPtr++; |
|
55 | + $openPtr = $stackPtr; |
|
56 | + $stackPtr++; |
|
57 | 57 | |
58 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
59 | - $content = Util\Common::prepareForOutput($openTag); |
|
60 | - echo "\t\tCreate comment token: T_DOC_COMMENT_OPEN_TAG => $content".PHP_EOL; |
|
61 | - } |
|
58 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
59 | + $content = Util\Common::prepareForOutput($openTag); |
|
60 | + echo "\t\tCreate comment token: T_DOC_COMMENT_OPEN_TAG => $content".PHP_EOL; |
|
61 | + } |
|
62 | 62 | |
63 | - /* |
|
63 | + /* |
|
64 | 64 | Strip off the close tag so it doesn't interfere with any |
65 | 65 | of our comment line processing. The token will be added to the |
66 | 66 | stack just before we return it. |
67 | 67 | */ |
68 | 68 | |
69 | - $closeTag = [ |
|
70 | - 'content' => substr($string, strlen(rtrim($string, '/*'))), |
|
71 | - 'code' => T_DOC_COMMENT_CLOSE_TAG, |
|
72 | - 'type' => 'T_DOC_COMMENT_CLOSE_TAG', |
|
73 | - 'comment_opener' => $openPtr, |
|
74 | - ]; |
|
69 | + $closeTag = [ |
|
70 | + 'content' => substr($string, strlen(rtrim($string, '/*'))), |
|
71 | + 'code' => T_DOC_COMMENT_CLOSE_TAG, |
|
72 | + 'type' => 'T_DOC_COMMENT_CLOSE_TAG', |
|
73 | + 'comment_opener' => $openPtr, |
|
74 | + ]; |
|
75 | 75 | |
76 | - if ($closeTag['content'] === false) { |
|
77 | - $closeTag['content'] = ''; |
|
78 | - } |
|
76 | + if ($closeTag['content'] === false) { |
|
77 | + $closeTag['content'] = ''; |
|
78 | + } |
|
79 | 79 | |
80 | - $string = rtrim($string, '/*'); |
|
80 | + $string = rtrim($string, '/*'); |
|
81 | 81 | |
82 | - /* |
|
82 | + /* |
|
83 | 83 | Process each line of the comment. |
84 | 84 | */ |
85 | 85 | |
86 | - $lines = explode($eolChar, $string); |
|
87 | - $numLines = count($lines); |
|
88 | - foreach ($lines as $lineNum => $string) { |
|
89 | - if ($lineNum !== ($numLines - 1)) { |
|
90 | - $string .= $eolChar; |
|
91 | - } |
|
92 | - |
|
93 | - $char = 0; |
|
94 | - $numChars = strlen($string); |
|
95 | - |
|
96 | - // We've started a new line, so process the indent. |
|
97 | - $space = $this->collectWhitespace($string, $char, $numChars); |
|
98 | - if ($space !== null) { |
|
99 | - $tokens[$stackPtr] = $space; |
|
100 | - $stackPtr++; |
|
101 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
102 | - $content = Util\Common::prepareForOutput($space['content']); |
|
103 | - echo "\t\tCreate comment token: T_DOC_COMMENT_WHITESPACE => $content".PHP_EOL; |
|
104 | - } |
|
105 | - |
|
106 | - $char += strlen($space['content']); |
|
107 | - if ($char === $numChars) { |
|
108 | - break; |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - if ($string === '') { |
|
113 | - continue; |
|
114 | - } |
|
115 | - |
|
116 | - if ($lineNum > 0 && $string[$char] === '*') { |
|
117 | - // This is a function or class doc block line. |
|
118 | - $char++; |
|
119 | - $tokens[$stackPtr] = [ |
|
120 | - 'content' => '*', |
|
121 | - 'code' => T_DOC_COMMENT_STAR, |
|
122 | - 'type' => 'T_DOC_COMMENT_STAR', |
|
123 | - ]; |
|
124 | - |
|
125 | - $stackPtr++; |
|
126 | - |
|
127 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
128 | - echo "\t\tCreate comment token: T_DOC_COMMENT_STAR => *".PHP_EOL; |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - // Now we are ready to process the actual content of the line. |
|
133 | - $lineTokens = $this->processLine($string, $eolChar, $char, $numChars); |
|
134 | - foreach ($lineTokens as $lineToken) { |
|
135 | - $tokens[$stackPtr] = $lineToken; |
|
136 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
137 | - $content = Util\Common::prepareForOutput($lineToken['content']); |
|
138 | - $type = $lineToken['type']; |
|
139 | - echo "\t\tCreate comment token: $type => $content".PHP_EOL; |
|
140 | - } |
|
141 | - |
|
142 | - if ($lineToken['code'] === T_DOC_COMMENT_TAG) { |
|
143 | - $tokens[$openPtr]['comment_tags'][] = $stackPtr; |
|
144 | - } |
|
145 | - |
|
146 | - $stackPtr++; |
|
147 | - } |
|
148 | - }//end foreach |
|
149 | - |
|
150 | - $tokens[$stackPtr] = $closeTag; |
|
151 | - $tokens[$openPtr]['comment_closer'] = $stackPtr; |
|
152 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
153 | - $content = Util\Common::prepareForOutput($closeTag['content']); |
|
154 | - echo "\t\tCreate comment token: T_DOC_COMMENT_CLOSE_TAG => $content".PHP_EOL; |
|
155 | - } |
|
156 | - |
|
157 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
158 | - echo "\t\t*** END COMMENT TOKENIZING ***".PHP_EOL; |
|
159 | - } |
|
160 | - |
|
161 | - return $tokens; |
|
162 | - |
|
163 | - }//end tokenizeString() |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * Process a single line of a comment. |
|
168 | - * |
|
169 | - * @param string $string The comment string being tokenized. |
|
170 | - * @param string $eolChar The EOL character to use for splitting strings. |
|
171 | - * @param int $start The position in the string to start processing. |
|
172 | - * @param int $end The position in the string to end processing. |
|
173 | - * |
|
174 | - * @return array |
|
175 | - */ |
|
176 | - private function processLine($string, $eolChar, $start, $end) |
|
177 | - { |
|
178 | - $tokens = []; |
|
179 | - |
|
180 | - // Collect content padding. |
|
181 | - $space = $this->collectWhitespace($string, $start, $end); |
|
182 | - if ($space !== null) { |
|
183 | - $tokens[] = $space; |
|
184 | - $start += strlen($space['content']); |
|
185 | - } |
|
186 | - |
|
187 | - if (isset($string[$start]) === false) { |
|
188 | - return $tokens; |
|
189 | - } |
|
190 | - |
|
191 | - if ($string[$start] === '@') { |
|
192 | - // The content up until the first whitespace is the tag name. |
|
193 | - $matches = []; |
|
194 | - preg_match('/@[^\s]+/', $string, $matches, 0, $start); |
|
195 | - if (isset($matches[0]) === true |
|
196 | - && substr(strtolower($matches[0]), 0, 7) !== '@phpcs:' |
|
197 | - ) { |
|
198 | - $tagName = $matches[0]; |
|
199 | - $start += strlen($tagName); |
|
200 | - $tokens[] = [ |
|
201 | - 'content' => $tagName, |
|
202 | - 'code' => T_DOC_COMMENT_TAG, |
|
203 | - 'type' => 'T_DOC_COMMENT_TAG', |
|
204 | - ]; |
|
205 | - |
|
206 | - // Then there will be some whitespace. |
|
207 | - $space = $this->collectWhitespace($string, $start, $end); |
|
208 | - if ($space !== null) { |
|
209 | - $tokens[] = $space; |
|
210 | - $start += strlen($space['content']); |
|
211 | - } |
|
212 | - } |
|
213 | - }//end if |
|
214 | - |
|
215 | - // Process the rest of the line. |
|
216 | - $eol = strpos($string, $eolChar, $start); |
|
217 | - if ($eol === false) { |
|
218 | - $eol = $end; |
|
219 | - } |
|
220 | - |
|
221 | - if ($eol > $start) { |
|
222 | - $tokens[] = [ |
|
223 | - 'content' => substr($string, $start, ($eol - $start)), |
|
224 | - 'code' => T_DOC_COMMENT_STRING, |
|
225 | - 'type' => 'T_DOC_COMMENT_STRING', |
|
226 | - ]; |
|
227 | - } |
|
228 | - |
|
229 | - if ($eol !== $end) { |
|
230 | - $tokens[] = [ |
|
231 | - 'content' => substr($string, $eol, strlen($eolChar)), |
|
232 | - 'code' => T_DOC_COMMENT_WHITESPACE, |
|
233 | - 'type' => 'T_DOC_COMMENT_WHITESPACE', |
|
234 | - ]; |
|
235 | - } |
|
236 | - |
|
237 | - return $tokens; |
|
238 | - |
|
239 | - }//end processLine() |
|
240 | - |
|
241 | - |
|
242 | - /** |
|
243 | - * Collect consecutive whitespace into a single token. |
|
244 | - * |
|
245 | - * @param string $string The comment string being tokenized. |
|
246 | - * @param int $start The position in the string to start processing. |
|
247 | - * @param int $end The position in the string to end processing. |
|
248 | - * |
|
249 | - * @return array|null |
|
250 | - */ |
|
251 | - private function collectWhitespace($string, $start, $end) |
|
252 | - { |
|
253 | - $space = ''; |
|
254 | - for ($start; $start < $end; $start++) { |
|
255 | - if ($string[$start] !== ' ' && $string[$start] !== "\t") { |
|
256 | - break; |
|
257 | - } |
|
258 | - |
|
259 | - $space .= $string[$start]; |
|
260 | - } |
|
261 | - |
|
262 | - if ($space === '') { |
|
263 | - return null; |
|
264 | - } |
|
265 | - |
|
266 | - $token = [ |
|
267 | - 'content' => $space, |
|
268 | - 'code' => T_DOC_COMMENT_WHITESPACE, |
|
269 | - 'type' => 'T_DOC_COMMENT_WHITESPACE', |
|
270 | - ]; |
|
271 | - |
|
272 | - return $token; |
|
273 | - |
|
274 | - }//end collectWhitespace() |
|
86 | + $lines = explode($eolChar, $string); |
|
87 | + $numLines = count($lines); |
|
88 | + foreach ($lines as $lineNum => $string) { |
|
89 | + if ($lineNum !== ($numLines - 1)) { |
|
90 | + $string .= $eolChar; |
|
91 | + } |
|
92 | + |
|
93 | + $char = 0; |
|
94 | + $numChars = strlen($string); |
|
95 | + |
|
96 | + // We've started a new line, so process the indent. |
|
97 | + $space = $this->collectWhitespace($string, $char, $numChars); |
|
98 | + if ($space !== null) { |
|
99 | + $tokens[$stackPtr] = $space; |
|
100 | + $stackPtr++; |
|
101 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
102 | + $content = Util\Common::prepareForOutput($space['content']); |
|
103 | + echo "\t\tCreate comment token: T_DOC_COMMENT_WHITESPACE => $content".PHP_EOL; |
|
104 | + } |
|
105 | + |
|
106 | + $char += strlen($space['content']); |
|
107 | + if ($char === $numChars) { |
|
108 | + break; |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + if ($string === '') { |
|
113 | + continue; |
|
114 | + } |
|
115 | + |
|
116 | + if ($lineNum > 0 && $string[$char] === '*') { |
|
117 | + // This is a function or class doc block line. |
|
118 | + $char++; |
|
119 | + $tokens[$stackPtr] = [ |
|
120 | + 'content' => '*', |
|
121 | + 'code' => T_DOC_COMMENT_STAR, |
|
122 | + 'type' => 'T_DOC_COMMENT_STAR', |
|
123 | + ]; |
|
124 | + |
|
125 | + $stackPtr++; |
|
126 | + |
|
127 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
128 | + echo "\t\tCreate comment token: T_DOC_COMMENT_STAR => *".PHP_EOL; |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + // Now we are ready to process the actual content of the line. |
|
133 | + $lineTokens = $this->processLine($string, $eolChar, $char, $numChars); |
|
134 | + foreach ($lineTokens as $lineToken) { |
|
135 | + $tokens[$stackPtr] = $lineToken; |
|
136 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
137 | + $content = Util\Common::prepareForOutput($lineToken['content']); |
|
138 | + $type = $lineToken['type']; |
|
139 | + echo "\t\tCreate comment token: $type => $content".PHP_EOL; |
|
140 | + } |
|
141 | + |
|
142 | + if ($lineToken['code'] === T_DOC_COMMENT_TAG) { |
|
143 | + $tokens[$openPtr]['comment_tags'][] = $stackPtr; |
|
144 | + } |
|
145 | + |
|
146 | + $stackPtr++; |
|
147 | + } |
|
148 | + }//end foreach |
|
149 | + |
|
150 | + $tokens[$stackPtr] = $closeTag; |
|
151 | + $tokens[$openPtr]['comment_closer'] = $stackPtr; |
|
152 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
153 | + $content = Util\Common::prepareForOutput($closeTag['content']); |
|
154 | + echo "\t\tCreate comment token: T_DOC_COMMENT_CLOSE_TAG => $content".PHP_EOL; |
|
155 | + } |
|
156 | + |
|
157 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
158 | + echo "\t\t*** END COMMENT TOKENIZING ***".PHP_EOL; |
|
159 | + } |
|
160 | + |
|
161 | + return $tokens; |
|
162 | + |
|
163 | + }//end tokenizeString() |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * Process a single line of a comment. |
|
168 | + * |
|
169 | + * @param string $string The comment string being tokenized. |
|
170 | + * @param string $eolChar The EOL character to use for splitting strings. |
|
171 | + * @param int $start The position in the string to start processing. |
|
172 | + * @param int $end The position in the string to end processing. |
|
173 | + * |
|
174 | + * @return array |
|
175 | + */ |
|
176 | + private function processLine($string, $eolChar, $start, $end) |
|
177 | + { |
|
178 | + $tokens = []; |
|
179 | + |
|
180 | + // Collect content padding. |
|
181 | + $space = $this->collectWhitespace($string, $start, $end); |
|
182 | + if ($space !== null) { |
|
183 | + $tokens[] = $space; |
|
184 | + $start += strlen($space['content']); |
|
185 | + } |
|
186 | + |
|
187 | + if (isset($string[$start]) === false) { |
|
188 | + return $tokens; |
|
189 | + } |
|
190 | + |
|
191 | + if ($string[$start] === '@') { |
|
192 | + // The content up until the first whitespace is the tag name. |
|
193 | + $matches = []; |
|
194 | + preg_match('/@[^\s]+/', $string, $matches, 0, $start); |
|
195 | + if (isset($matches[0]) === true |
|
196 | + && substr(strtolower($matches[0]), 0, 7) !== '@phpcs:' |
|
197 | + ) { |
|
198 | + $tagName = $matches[0]; |
|
199 | + $start += strlen($tagName); |
|
200 | + $tokens[] = [ |
|
201 | + 'content' => $tagName, |
|
202 | + 'code' => T_DOC_COMMENT_TAG, |
|
203 | + 'type' => 'T_DOC_COMMENT_TAG', |
|
204 | + ]; |
|
205 | + |
|
206 | + // Then there will be some whitespace. |
|
207 | + $space = $this->collectWhitespace($string, $start, $end); |
|
208 | + if ($space !== null) { |
|
209 | + $tokens[] = $space; |
|
210 | + $start += strlen($space['content']); |
|
211 | + } |
|
212 | + } |
|
213 | + }//end if |
|
214 | + |
|
215 | + // Process the rest of the line. |
|
216 | + $eol = strpos($string, $eolChar, $start); |
|
217 | + if ($eol === false) { |
|
218 | + $eol = $end; |
|
219 | + } |
|
220 | + |
|
221 | + if ($eol > $start) { |
|
222 | + $tokens[] = [ |
|
223 | + 'content' => substr($string, $start, ($eol - $start)), |
|
224 | + 'code' => T_DOC_COMMENT_STRING, |
|
225 | + 'type' => 'T_DOC_COMMENT_STRING', |
|
226 | + ]; |
|
227 | + } |
|
228 | + |
|
229 | + if ($eol !== $end) { |
|
230 | + $tokens[] = [ |
|
231 | + 'content' => substr($string, $eol, strlen($eolChar)), |
|
232 | + 'code' => T_DOC_COMMENT_WHITESPACE, |
|
233 | + 'type' => 'T_DOC_COMMENT_WHITESPACE', |
|
234 | + ]; |
|
235 | + } |
|
236 | + |
|
237 | + return $tokens; |
|
238 | + |
|
239 | + }//end processLine() |
|
240 | + |
|
241 | + |
|
242 | + /** |
|
243 | + * Collect consecutive whitespace into a single token. |
|
244 | + * |
|
245 | + * @param string $string The comment string being tokenized. |
|
246 | + * @param int $start The position in the string to start processing. |
|
247 | + * @param int $end The position in the string to end processing. |
|
248 | + * |
|
249 | + * @return array|null |
|
250 | + */ |
|
251 | + private function collectWhitespace($string, $start, $end) |
|
252 | + { |
|
253 | + $space = ''; |
|
254 | + for ($start; $start < $end; $start++) { |
|
255 | + if ($string[$start] !== ' ' && $string[$start] !== "\t") { |
|
256 | + break; |
|
257 | + } |
|
258 | + |
|
259 | + $space .= $string[$start]; |
|
260 | + } |
|
261 | + |
|
262 | + if ($space === '') { |
|
263 | + return null; |
|
264 | + } |
|
265 | + |
|
266 | + $token = [ |
|
267 | + 'content' => $space, |
|
268 | + 'code' => T_DOC_COMMENT_WHITESPACE, |
|
269 | + 'type' => 'T_DOC_COMMENT_WHITESPACE', |
|
270 | + ]; |
|
271 | + |
|
272 | + return $token; |
|
273 | + |
|
274 | + }//end collectWhitespace() |
|
275 | 275 | |
276 | 276 | |
277 | 277 | }//end class |