Back to Language class

Method debugFile

public int
debugFile
(mixed $filename)
Debugs a language file
Parameters
  • string $filename Absolute path to the file to debug
Returns
  • int A count of the number of parsing errors
Since
  • 3.6.3
-
  • \InvalidArgumentException
Class: Language
Project: Joomla

Method debugFile - Source code

/**
 * Debugs a language file
 *
 * @param   string  $filename  Absolute path to the file to debug
 *
 * @return  integer  A count of the number of parsing errors
 *
 * @since   3.6.3
 * @throws  \InvalidArgumentException
 */
public function debugFile($filename)
{
    // Make sure our file actually exists
    if (!is_file($filename)) {
        throw new \InvalidArgumentException(sprintf('Unable to locate file "%s" for debugging', $filename));
    }
    // Initialise variables for manually parsing the file for common errors.
    $reservedWord = array('YES', 'NO', 'NULL', 'FALSE', 'ON', 'OFF', 'NONE', 'TRUE');
    $debug = $this->getDebug();
    $this->debug = false;
    $errors = array();
    $php_errormsg = null;
    // Open the file as a stream.
    $file = new \SplFileObject($filename);
    foreach ($file as $lineNumber => $line) {
        // Avoid BOM error as BOM is OK when using parse_ini.
        if ($lineNumber == 0) {
            $line = str_replace("", '', $line);
        }
        $line = trim($line);
        // Ignore comment lines.
        if (!\strlen($line) || $line['0'] == ';') {
            continue;
        }
        // Ignore grouping tag lines, like: [group]
        if (preg_match('#^\\[[^\\]]*\\](\\s*;.*)?$#', $line)) {
            continue;
        }
        // Remove any escaped double quotes \" from the equation
        $line = str_replace('\\"', '', $line);
        $realNumber = $lineNumber + 1;
        // Check for odd number of double quotes.
        if (substr_count($line, '"') % 2 != 0) {
            $errors[] = $realNumber;
            continue;
        }
        // Check that the line passes the necessary format.
        if (!preg_match('#^[A-Z][A-Z0-9_:\\*\\-\\.]*\\s*=\\s*".*"(\\s*;.*)?$#', $line)) {
            $errors[] = $realNumber;
            continue;
        }
        // Check that the key is not in the reserved constants list.
        $key = strtoupper(trim(substr($line, 0, strpos($line, '='))));
        if (\in_array($key, $reservedWord)) {
            $errors[] = $realNumber;
        }
    }
    // Check if we encountered any errors.
    if (\count($errors)) {
        $this->errorfiles[$filename] = $errors;
    } elseif ($php_errormsg) {
        // We didn't find any errors but there's probably a parse notice.
        $this->errorfiles['PHP' . $filename] = 'PHP parser errors :' . $php_errormsg;
    }
    $this->debug = $debug;
    return \count($errors);
}