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

Diff::generateFileReport()   F

Complexity

Conditions 16
Paths 2001

Size

Total Lines 92
Code Lines 57

Duplication

Lines 25
Ratio 27.17 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 25
loc 92
rs 2
cc 16
eloc 57
nc 2001
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
 * 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) {
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...
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) {
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...
53
                $timeTaken = ((microtime(true) - $startTime) * 1000);
0 ignored issues
show
Bug introduced by
The variable $startTime does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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) {
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...
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