PHP_CodeSniffer_CLI::runphpcbf PHP Method

runphpcbf() public method

Run the PHPCBF script.
public runphpcbf ( ) : array
return array
    public function runphpcbf()
    {
        if (defined('PHP_CODESNIFFER_CBF') === false) {
            define('PHP_CODESNIFFER_CBF', true);
        }
        if (is_file(dirname(__FILE__) . '/../CodeSniffer/Reporting.php') === true) {
            include_once dirname(__FILE__) . '/../CodeSniffer/Reporting.php';
        } else {
            include_once 'PHP/CodeSniffer/Reporting.php';
        }
        PHP_CodeSniffer_Reporting::startTiming();
        $this->checkRequirements();
        $this->dieOnUnknownArg = false;
        // Override some of the command line settings that might break the fixes.
        $cliValues = $this->getCommandLineValues();
        $cliValues['verbosity'] = 0;
        $cliValues['showProgress'] = false;
        $cliValues['generator'] = '';
        $cliValues['explain'] = false;
        $cliValues['interactive'] = false;
        $cliValues['showSources'] = false;
        $cliValues['reportFile'] = null;
        $cliValues['reports'] = array();
        $suffix = '';
        if (isset($cliValues['suffix']) === true) {
            $suffix = $cliValues['suffix'];
        }
        $allowPatch = true;
        if (isset($cliValues['no-patch']) === true || empty($cliValues['files']) === true) {
            // They either asked for this,
            // or they are using STDIN, which can't use diff.
            $allowPatch = false;
        }
        if ($suffix === '' && $allowPatch === true) {
            // Using the diff/patch tools.
            $diffFile = getcwd() . '/phpcbf-fixed.diff';
            $cliValues['reports'] = array('diff' => $diffFile);
            if (file_exists($diffFile) === true) {
                unlink($diffFile);
            }
        } else {
            // Replace the file without the patch command
            // or writing to a file with a new suffix.
            $cliValues['reports'] = array('cbf' => null);
            $cliValues['phpcbf-suffix'] = $suffix;
        }
        $numErrors = $this->process($cliValues);
        if ($suffix === '' && $allowPatch === true) {
            if (file_exists($diffFile) === false) {
                // Nothing to fix.
                if ($numErrors === 0) {
                    // And no errors reported.
                    $exit = 0;
                } else {
                    // Errors we can't fix.
                    $exit = 2;
                }
            } else {
                if (filesize($diffFile) < 10) {
                    // Empty or bad diff file.
                    if ($numErrors === 0) {
                        // And no errors reported.
                        $exit = 0;
                    } else {
                        // Errors we can't fix.
                        $exit = 2;
                    }
                } else {
                    $cmd = "patch -p0 -ui \"{$diffFile}\"";
                    $output = array();
                    $retVal = null;
                    exec($cmd, $output, $retVal);
                    if ($retVal === 0) {
                        // Everything went well.
                        $filesPatched = count($output);
                        echo "Patched {$filesPatched} file";
                        if ($filesPatched > 1) {
                            echo 's';
                        }
                        echo PHP_EOL;
                        $exit = 1;
                    } else {
                        print_r($output);
                        echo "Returned: {$retVal}" . PHP_EOL;
                        $exit = 3;
                    }
                }
                //end if
                unlink($diffFile);
            }
            //end if
        } else {
            // File are being patched manually, so we can't tell
            // how many errors were fixed.
            $exit = 1;
        }
        //end if
        if ($exit === 0) {
            echo 'No fixable errors were found' . PHP_EOL;
        } else {
            if ($exit === 2) {
                echo 'PHPCBF could not fix all the errors found' . PHP_EOL;
            }
        }
        PHP_CodeSniffer_Reporting::printRunTime();
        exit($exit);
    }

Usage Example

 /**
  * @codeCoverageIgnore hard coded `exit()` in `parent::runphpcbf`
  */
 public function runphpcbf()
 {
     $this->processConfig();
     parent::runphpcbf();
 }