Completed
Pull Request — master (#9)
by Tomáš
16:51 queued 12:18
created

Full::generateFileReport()   F

Complexity

Conditions 25
Paths > 20000

Size

Total Lines 143
Code Lines 85

Duplication

Lines 68
Ratio 47.55 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 68
loc 143
rs 2
cc 25
eloc 85
nc 80641
nop 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Full 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 Full 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
        if ($report['errors'] === 0 && $report['warnings'] === 0) {
36
            // Nothing to print.
37
            return false;
38
        }
39
40
        // The length of the word ERROR or WARNING; used for padding.
41
        if ($report['warnings'] > 0) {
42
            $typeLength = 7;
43
        } else {
44
            $typeLength = 5;
45
        }
46
47
        // Work out the max line number length for formatting.
48
        $maxLineNumLength = max(array_map('strlen', array_keys($report['messages'])));
49
50
        // The padding that all lines will require that are
51
        // printing an error message overflow.
52
        $paddingLine2  = str_repeat(' ', ($maxLineNumLength + 1));
53
        $paddingLine2 .= ' | ';
54
        $paddingLine2 .= str_repeat(' ', $typeLength);
55
        $paddingLine2 .= ' | ';
56
        if ($report['fixable'] > 0) {
57
            $paddingLine2 .= '    ';
58
        }
59
60
        $paddingLength = strlen($paddingLine2);
61
62
        // Make sure the report width isn't too big.
63
        $maxErrorLength = 0;
64 View Code Duplication
        foreach ($report['messages'] as $line => $lineErrors) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
65
            foreach ($lineErrors as $column => $colErrors) {
66
                foreach ($colErrors as $error) {
67
                    $length = strlen($error['message']);
68
                    if ($showSources === true) {
69
                        $length += (strlen($error['source']) + 3);
70
                    }
71
72
                    $maxErrorLength = max($maxErrorLength, ($length + 1));
73
                }
74
            }
75
        }
76
77
        $file       = $report['filename'];
78
        $fileLength = strlen($file);
79
        $maxWidth   = max(($fileLength + 6), ($maxErrorLength + $paddingLength));
80
        $width      = min($width, $maxWidth);
81
        if ($width < 70) {
82
            $width = 70;
83
        }
84
85
        echo PHP_EOL."\033[1mFILE: ";
86 View Code Duplication
        if ($fileLength <= ($width - 6)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
87
            echo $file;
88
        } else {
89
            echo '...'.substr($file, ($fileLength - ($width - 6)));
90
        }
91
92
        echo "\033[0m".PHP_EOL;
93
        echo str_repeat('-', $width).PHP_EOL;
94
95
        echo "\033[1m".'FOUND '.$report['errors'].' ERROR';
96
        if ($report['errors'] !== 1) {
97
            echo 'S';
98
        }
99
100 View Code Duplication
        if ($report['warnings'] > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
101
            echo ' AND '.$report['warnings'].' WARNING';
102
            if ($report['warnings'] !== 1) {
103
                echo 'S';
104
            }
105
        }
106
107
        echo ' AFFECTING '.count($report['messages']).' LINE';
108
        if (count($report['messages']) !== 1) {
109
            echo 'S';
110
        }
111
112
        echo "\033[0m".PHP_EOL;
113
        echo str_repeat('-', $width).PHP_EOL;
114
115
        // The maximum amount of space an error message can use.
116
        $maxErrorSpace = ($width - $paddingLength - 1);
117
        if ($showSources === true) {
118
            // Account for the chars used to print colors.
119
            $maxErrorSpace += 8;
120
        }
121
122
        foreach ($report['messages'] as $line => $lineErrors) {
123 View Code Duplication
            foreach ($lineErrors as $column => $colErrors) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
124
                foreach ($colErrors as $error) {
125
                    $message = $error['message'];
126
                    $message = str_replace("\n", "\n".$paddingLine2, $message);
127
                    if ($showSources === true) {
128
                        $message = "\033[1m".$message."\033[0m".' ('.$error['source'].')';
129
                    }
130
131
                    // The padding that goes on the front of the line.
132
                    $padding  = ($maxLineNumLength - strlen($line));
133
                    $errorMsg = wordwrap(
134
                        $message,
135
                        $maxErrorSpace,
136
                        PHP_EOL.$paddingLine2
137
                    );
138
139
                    echo ' '.str_repeat(' ', $padding).$line.' | ';
140
                    if ($error['type'] === 'ERROR') {
141
                        echo "\033[31mERROR\033[0m";
142
                        if ($report['warnings'] > 0) {
143
                            echo '  ';
144
                        }
145
                    } else {
146
                        echo "\033[33mWARNING\033[0m";
147
                    }
148
149
                    echo ' | ';
150
                    if ($report['fixable'] > 0) {
151
                        echo '[';
152
                        if ($error['fixable'] === true) {
153
                            echo 'x';
154
                        } else {
155
                            echo ' ';
156
                        }
157
158
                        echo '] ';
159
                    }
160
161
                    echo $errorMsg.PHP_EOL;
162
                }//end foreach
163
            }//end foreach
164
        }//end foreach
165
166
        echo str_repeat('-', $width).PHP_EOL;
167 View Code Duplication
        if ($report['fixable'] > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
168
            echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL;
169
            echo str_repeat('-', $width).PHP_EOL;
170
        }
171
172
        echo PHP_EOL;
173
        return true;
174
175
    }//end generateFileReport()
176
177
178
    /**
179
     * Prints all errors and warnings for each file processed.
180
     *
181
     * @param string $cachedData    Any partial report data that was returned from
182
     *                              generateFileReport during the run.
183
     * @param int    $totalFiles    Total number of files processed during the run.
184
     * @param int    $totalErrors   Total number of errors found during the run.
185
     * @param int    $totalWarnings Total number of warnings found during the run.
186
     * @param int    $totalFixable  Total number of problems that can be fixed.
187
     * @param bool   $showSources   Show sources?
188
     * @param int    $width         Maximum allowed line width.
189
     * @param bool   $interactive   Are we running in interactive mode?
190
     * @param bool   $toScreen      Is the report being printed to screen?
191
     *
192
     * @return void
193
     */
194 View Code Duplication
    public function generate(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
195
        $cachedData,
196
        $totalFiles,
197
        $totalErrors,
198
        $totalWarnings,
199
        $totalFixable,
200
        $showSources=false,
201
        $width=80,
202
        $interactive=false,
203
        $toScreen=true
204
    ) {
205
        if ($cachedData === '') {
206
            return;
207
        }
208
209
        echo $cachedData;
210
211
        if ($toScreen === true && $interactive === false) {
212
            Util\Timing::printRunTime();
213
        }
214
215
    }//end generate()
216
217
218
}//end class
219