1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Diff report for Symplify\PHP7_CodeSniffer. |
4
|
|
|
* |
5
|
|
|
* @author Greg Sherwood <[email protected]> |
6
|
|
|
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) |
7
|
|
|
* @license https://github.com/squizlabs/Symplify\PHP7_CodeSniffer/blob/master/licence.txt BSD Licence |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Symplify\PHP7_CodeSniffer\Reports; |
11
|
|
|
|
12
|
|
|
use Symplify\PHP7_CodeSniffer\Files\File; |
13
|
|
|
use Symplify\PHP7_CodeSniffer\Util; |
14
|
|
|
|
15
|
|
|
class Diff implements Report |
16
|
|
|
{ |
17
|
|
|
|
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 \Symplify\PHP7_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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
if (PHP_CodeSniffer_CBF === true) { |
74
|
|
|
ob_end_clean(); |
75
|
|
|
$startTime = microtime(true); |
76
|
|
|
echo "\t=> Fixing file: $errors/$errors violations remaining"; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$fixed = $phpcsFile->fixer->fixFile(); |
80
|
|
|
|
81
|
|
|
if (PHP_CodeSniffer_CBF === true) { |
82
|
|
|
if ($fixed === false) { |
83
|
|
|
echo "\033[31mERROR\033[0m"; |
84
|
|
|
} else { |
85
|
|
|
echo "\033[32mDONE\033[0m"; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$timeTaken = ((microtime(true) - $startTime) * 1000); |
89
|
|
View Code Duplication |
if ($timeTaken < 1000) { |
|
|
|
|
90
|
|
|
$timeTaken = round($timeTaken); |
91
|
|
|
echo " in {$timeTaken}ms".PHP_EOL; |
92
|
|
|
} else { |
93
|
|
|
$timeTaken = round(($timeTaken / 1000), 2); |
94
|
|
|
echo " in $timeTaken secs".PHP_EOL; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
ob_start(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (PHP_CodeSniffer_VERBOSITY > 1) { |
101
|
|
|
echo "\t*** END FILE FIXING ***".PHP_EOL; |
102
|
|
|
ob_start(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($fixed === false) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (PHP_CodeSniffer_CBF === true) { |
110
|
|
|
// Diff without colours. |
111
|
|
|
$diff = $phpcsFile->fixer->generateDiff(null, false); |
112
|
|
|
} else { |
113
|
|
|
$diff = $phpcsFile->fixer->generateDiff(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($diff === '') { |
117
|
|
|
// Nothing to print. |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
echo $diff.PHP_EOL; |
122
|
|
|
return true; |
123
|
|
|
|
124
|
|
|
}//end generateFileReport() |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Prints all errors and warnings for each file processed. |
129
|
|
|
* |
130
|
|
|
* @param string $cachedData Any partial report data that was returned from |
131
|
|
|
* generateFileReport during the run. |
132
|
|
|
* @param int $totalFiles Total number of files processed during the run. |
133
|
|
|
* @param int $totalErrors Total number of errors found during the run. |
134
|
|
|
* @param int $totalWarnings Total number of warnings found during the run. |
135
|
|
|
* @param int $totalFixable Total number of problems that can be fixed. |
136
|
|
|
* @param bool $showSources Show sources? |
137
|
|
|
* @param int $width Maximum allowed line width. |
138
|
|
|
* @param bool $interactive Are we running in interactive mode? |
139
|
|
|
* @param bool $toScreen Is the report being printed to screen? |
140
|
|
|
* |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
public function generate( |
144
|
|
|
$cachedData, |
145
|
|
|
$totalFiles, |
146
|
|
|
$totalErrors, |
147
|
|
|
$totalWarnings, |
148
|
|
|
$totalFixable, |
149
|
|
|
$showSources=false, |
150
|
|
|
$width=80, |
151
|
|
|
$interactive=false, |
152
|
|
|
$toScreen=true |
153
|
|
|
) { |
154
|
|
|
echo $cachedData; |
155
|
|
|
if ($toScreen === true) { |
156
|
|
|
echo PHP_EOL; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
}//end generate() |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
}//end class |
163
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.