/**
 * Checks an individual other file and adds basic problems to result.
 * @param string $file File to check
 * @param array $problems Existing problem structure from PHPCodeSniffer
 *   to which new problems will be added
 */
function local_codechecker_check_other_file($file, &$problems)
{
    if (!array_key_exists($file, $problems)) {
        $problems[$file] = array('warnings' => array(), 'errors' => array(), 'numWarnings' => 0, 'numErrors' => 0);
    }
    // Certain files are permitted lines of any length because they are
    // auto-generated
    $allowanylength = in_array(basename($file), array('install.xml'));
    $lines = file($file);
    $index = 0;
    $blankrun = 0;
    foreach ($lines as $l) {
        $index++;
        // Incorrect [Windows] line ending
        if (strpos($l, "\r\n") !== false && empty($donecrlf)) {
            local_codechecker_add_problem($problems, $file, $index, 'crlf');
            $donecrlf = true;
        }
        // Missing line ending (at EOF presumably)
        if (strpos($l, "\n") === false) {
            local_codechecker_add_problem($problems, $file, $index, 'missinglf');
        }
        $l = rtrim($l);
        if ($l === '') {
            $blankrun++;
        } else {
            $blankrun = 0;
        }
        // Whitespace at EOL
        if (preg_match('~ +$~', $l)) {
            local_codechecker_add_problem($problems, $file, $index, 'eol');
        }
        // Tab anywhere in line
        if (preg_match('~\\t~', $l)) {
            local_codechecker_add_problem($problems, $file, $index, 'tab');
        }
        if (strlen($l) > 140 && !$allowanylength) {
            // Line length > 140
            local_codechecker_add_problem($problems, $file, $index, 'toolong');
        } else {
            if (strlen($l) > 100 && !$allowanylength) {
                // Line length > 100
                local_codechecker_add_problem($problems, $file, $index, 'ratherlong', true);
            }
        }
    }
    if ($blankrun > 0) {
        local_codechecker_add_problem($problems, $file, $index - $blankrun, 'extralfs');
    }
}
/**
 * Checks an individual other file and adds basic problems to result.
 * @param string $file File to check
 * @param SimpleXMLElement $xml structure containin all violations
 *   to which new problems will be added.
 */
function local_codechecker_check_other_file($file, $xml)
{
    // If the file does not exist, add it.
    $fileinxml = $xml->xpath("file[@name='{$file}']");
    if (!count($fileinxml)) {
        $fileinxml = $xml->addChild('file');
        $fileinxml->addAttribute('name', $file);
        $fileinxml->addAttribute('errors', 0);
        $fileinxml->addAttribute('warnings', 0);
    }
    // Certain files are permitted lines of any length because they are
    // Auto-generated.
    $allowanylength = in_array(basename($file), array('install.xml')) || substr($file, -4, 4) === '.csv';
    $lines = file($file);
    $index = 0;
    $blankrun = 0;
    foreach ($lines as $l) {
        $index++;
        // Incorrect [Windows] line ending.
        if (strpos($l, "\r\n") !== false && empty($donecrlf)) {
            local_codechecker_add_problem($fileinxml, $file, $index, 'crlf');
            $donecrlf = true;
        }
        // Missing line ending (at EOF presumably).
        if (strpos($l, "\n") === false) {
            local_codechecker_add_problem($fileinxml, $file, $index, 'missinglf');
        }
        $l = rtrim($l);
        if ($l === '') {
            $blankrun++;
        } else {
            $blankrun = 0;
        }
        // Whitespace at EOL.
        if (preg_match('~ +$~', $l)) {
            local_codechecker_add_problem($fileinxml, $file, $index, 'eol');
        }
        // Tab anywhere in line.
        if (preg_match('~\\t~', $l)) {
            local_codechecker_add_problem($fileinxml, $file, $index, 'tab');
        }
        if (strlen($l) > 180 && !$allowanylength) {
            // Line length > 180.
            local_codechecker_add_problem($fileinxml, $file, $index, 'toolong');
        } else {
            if (strlen($l) > 132 && !$allowanylength) {
                // Line length > 132.
                local_codechecker_add_problem($fileinxml, $file, $index, 'ratherlong', true);
            }
        }
    }
    if ($blankrun > 0) {
        local_codechecker_add_problem($fileinxml, $file, $index - $blankrun, 'extralfs');
    }
}