33
2
mirror of https://github.com/joomla-extensions/jedchecker.git synced 2025-01-17 02:55:09 +00:00

change foreach-based to regex-based search in errorreporting.php

This commit is contained in:
Denis Ryabov 2021-05-10 20:20:42 +03:00
parent a8ea5b75c2
commit 893beace31

View File

@ -14,6 +14,9 @@ defined('_JEXEC') or die('Restricted access');
// Include the rule base class // Include the rule base class
require_once JPATH_COMPONENT_ADMINISTRATOR . '/models/rule.php'; require_once JPATH_COMPONENT_ADMINISTRATOR . '/models/rule.php';
// Include the helper class
require_once JPATH_COMPONENT_ADMINISTRATOR . '/libraries/helper.php';
/** /**
* JedcheckerRulesErrorreporting * JedcheckerRulesErrorreporting
* *
@ -46,6 +49,12 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule
*/ */
protected $description = 'COM_JEDCHECKER_RULE_ERRORREPORTING_DESC'; protected $description = 'COM_JEDCHECKER_RULE_ERRORREPORTING_DESC';
/**
* Regular expression to look for error_reporting calls.
*
* @var string
*/
protected $errorreportingRegex;
/** /**
* Initiates the file search and check * Initiates the file search and check
@ -54,6 +63,17 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule
*/ */
public function check() public function check()
{ {
// Get the functions to look for
$codes = explode(',', $this->params->get('errorreportings'));
// Prepare regex
foreach ($codes as $i => $encoding)
{
$codes[$i] = preg_quote(trim($encoding), '/');
}
$this->errorreportingRegex = '/' . implode('|', $codes) . '/i';
// Find all php files of the extension // Find all php files of the extension
$files = JFolder::files($this->basedir, '\.php$', true, true); $files = JFolder::files($this->basedir, '\.php$', true, true);
@ -78,26 +98,23 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule
*/ */
protected function find($file) protected function find($file)
{ {
$content = (array) file($file); $content = file_get_contents($file);
// Get the functions to look for // Exclude non-code content
$errorreportings = explode(',', $this->params->get('errorreportings')); $content = JEDCheckerHelper::cleanPhpCode(
$errorreportings = array_map('trim', $errorreportings); $content,
JEDCheckerHelper::CLEAN_HTML | JEDCheckerHelper::CLEAN_COMMENTS | JEDCheckerHelper::CLEAN_STRINGS
);
$content = JEDCheckerHelper::splitLines($content);
$found = false; $found = false;
foreach ($content as $i => $line) foreach ($content as $i => $line)
{ {
foreach ($errorreportings as $errorreporting) if (preg_match($this->errorreportingRegex, $line))
{ {
$pos_1 = stripos($line, $errorreporting); $found = true;
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_ERROR_ERRORREPORTING'), $i + 1, $line);
if ($pos_1 !== false)
{
$found = true;
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_ERROR_ERRORREPORTING'), $i + 1, $line);
break;
}
} }
} }