#!/usr/bin/php
<?php
/**
 * PHP_CodeSniffer tokenises PHP code and detects violations of a
 * defined set of coding standards.
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHP_CodeSniffer
 * @author    Greg Sherwood <gsherwood@squiz.net>
 * @author    Marc McIntyre <mmcintyre@squiz.net>
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 * @version   CVS: $Id: phpcs,v 1.11 2007/02/18 22:35:53 squiz Exp $
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 */

error_reporting(E_ALL | E_STRICT);
require_once 'PHP/CodeSniffer.php';

/**
 * Prints out the usage information for this script.
 *
 * @return void
 */
function PHP_CodeSniffer_printUsage()
{
    echo "Usage: phpcs [-nvi] [--report=<report>] [--standard=<standard>] [--extensions=<extensions>] <file> ...\n";
    echo " <file>       : one or more files and/or directories to check\n";
    echo " <standard>   : the name of the coding standard to use\n";
    echo " <report>     : print either the \"full\" or \"summary\" report\n";
    echo " <extensions> : a comma separated list of file extensions to check (only valid if checking a directory)\n";
    echo " -n           : do not print warnings\n";
    echo " -v[v][v]     : print verbose output\n";
    echo " -i           : show a list of installed coding standards\n";
    echo " -h --help    : print this help message\n";
    echo " --version    : print version information\n";
    exit();

}//end PHP_CodeSniffer_printUsage()


/**
 * Prints out a list of installed coding standards.
 *
 * @return void
 */
function PHP_CodeSniffer_printInstalledStandards()
{
    $installedStandards = PHP_CodeSniffer::getInstalledStandards();
    $numStandards       = count($installedStandards);

    if ($numStandards === 0) {
        echo "No coding standards are installed.\n";
    } else {
        $lastStandard = array_pop($installedStandards);
        if ($numStandards === 1) {
            echo "The only coding standard installed is $lastStandard\n";
        } else {
            $standardList  = implode(', ', $installedStandards);
            $standardList .= ' and '.$lastStandard;
            echo "The installed coding standards are $standardList.\n";
        }
    }

}//end PHP_CodeSniffer_printInstalledStandards()


// First, we need to ensure there is at least one coding standard
// installed, or they will never be able to run this script.
$installedStandards = PHP_CodeSniffer::getInstalledStandards();
if (count($installedStandards) === 0) {
    echo "ERROR: There are no coding standards installed.\n";
    exit();
}


// The default values for config settings.
$files        = array();
$standard     = 'PEAR';
$verbosity    = 0;
$report       = 'full';
$showWarnings = true;
$extensions   = array();

for ($i = 1; $i < $_SERVER['argc']; $i++) {
    $arg = $_SERVER['argv'][$i];
    if ($arg{0} === '-') {

        /*
            Check for all "--" switches first.
            Then check the "-" switches in one go.
        */

        if ($arg{1} === '-') {
            if ($arg === '--help') {
                PHP_CodeSniffer_printUsage();
            }

            if ($arg === '--version') {
                echo "PHP_CodeSniffer version 0.5.0 (beta) ";
                echo "by Squiz Pty Ltd. (http://www.squiz.net)\n";
                exit();
            }

            if (substr($arg, 0, 9) === '--report=') {
                $report = substr($arg, 9);
                if ($report !== 'full' && $report !== 'summary') {
                    echo "ERROR: Report type \"$report\" not known.\n\n";
                    exit();
                }
            } else if (substr($arg, 0, 11) === '--standard=') {
                $standard = substr($arg, 11);
            } else if (substr($arg, 0, 13) === '--extensions=') {
                $extensions = explode(',', substr($arg, 13));
            } else {
                echo "ERROR: option \"$arg\" not known.\n\n";
                PHP_CodeSniffer_printUsage();
            }
        } else {
            $switches = str_split($arg);
            foreach ($switches as $switch) {
                if ($switch === '-') {
                    continue;
                }

                switch ($switch) {
                case 'h':
                    PHP_CodeSniffer_printUsage();
                    break;
                case 'i' :
                    PHP_CodeSniffer_printInstalledStandards();
                    exit();
                    break;
                case 'v' :
                    $verbosity++;
                    break;
                case 'n' :
                    $showWarnings = false;
                    break;
                default:
                    echo "ERROR: option \"$switch\" not known.\n\n";
                    PHP_CodeSniffer_printUsage();
                }//end switch
            }//end foreach
        }//end else
    } else {
        // Assume everything that is not a switch is a file or directory.
        $files[] = $arg;
    }
}//end for

if (empty($files) === true) {
    echo "ERROR: You must supply at least one file or directory to process.\n\n";
    PHP_CodeSniffer_printUsage();
}

foreach ($files as $file) {
    if (file_exists($file) === false) {
        echo "ERROR: The file \"$file\" does not exist.\n\n";
        PHP_CodeSniffer_printUsage();
    }
}

if (count($installedStandards) > 1) {
    if ($standard === null) {
        echo "ERROR: You must supply the name of the coding standard to use.\n\n";
        PHP_CodeSniffer_printUsage();
    }
} else {
    $standard = array_pop($installedStandards);
}

// Check if the standard name is valid. If not, check that the case
// was not entered incorrectly before throwing an error.
if (PHP_CodeSniffer::isInstalledStandard($standard) === false) {
    $installedStandards = PHP_CodeSniffer::getInstalledStandards();
    foreach ($installedStandards as $validStandard) {
        if (strtolower($standard) === strtolower($validStandard)) {
            $standard = $validStandard;
            break;
        }
    }
}

if (PHP_CodeSniffer::isInstalledStandard($standard) === false) {
    // They didn't select a valid coding standard, so help them
    // out by letting them know which standards are installed.
    echo 'ERROR: the "'.$standard.'" coding standard is not installed. ';
    PHP_CodeSniffer_printInstalledStandards();
    exit();
}

$phpcs = new PHP_CodeSniffer($verbosity);

// Set file extensions if they were specified. Otherwise,
// let PHP_CodeSniffer decide on the defaults.
if (empty($extensions) === false) {
    $phpcs->setAllowedFileExtensions($extensions);
}

$phpcs->process($files, $standard);
if ($report === 'summary') {
    $phpcs->printErrorReportSummary($showWarnings);
} else {
    $phpcs->printErrorReport($showWarnings);
}

?>
