Updated ProfFileParser.class.php to PSR-12

This commit is contained in:
B3none 2023-12-01 00:29:45 +00:00 committed by David Anderson
parent 8eeea2aba1
commit 30415e299e

View File

@ -4,24 +4,23 @@ class ProfReport
{ {
public $time; public $time;
public $uptime; public $uptime;
public $items = array(); public array $items = [];
} }
class ProfReportParser class ProfReportParser
{ {
private $report; private bool|ProfReport $report = false;
private $curtype; private bool|string $curtype = false;
public $last_error; public string $last_error;
public function Parse($file) public function Parse(string $file): bool|ProfReport
{ {
$this->report = FALSE; $this->report = false;
$this->curtype = FALSE; $this->curtype = false;
if (($contents = file_get_contents($file)) === FALSE) if (($contents = file_get_contents($file)) === false) {
{
$this->last_error = 'File not found'; $this->last_error = 'File not found';
return FALSE; return false;
} }
$xml = xml_parser_create(); $xml = xml_parser_create();
@ -29,45 +28,36 @@ class ProfReportParser
xml_set_element_handler($xml, 'tag_open', 'tag_close'); xml_set_element_handler($xml, 'tag_open', 'tag_close');
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, false); xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, false);
if (!xml_parse($xml, $contents)) if (!xml_parse($xml, $contents)) {
{
$this->last_error = 'Line: ' . xml_get_current_line_number($xml) . ' -- ' . xml_error_string(xml_get_error_code($xml)); $this->last_error = 'Line: ' . xml_get_current_line_number($xml) . ' -- ' . xml_error_string(xml_get_error_code($xml));
return FALSE; return false;
} }
return $this->report; return $this->report;
} }
public function tag_open($parser, $tag, $attrs) public function tag_open($parser, string $tag, array $attrs): void
{ {
if ($tag == 'profile') if ($tag === 'profile') {
{
$this->report = new ProfReport(); $this->report = new ProfReport();
$this->report->time = $attrs['time']; $this->report->time = $attrs['time'];
$this->report->uptime = $attrs['uptime']; $this->report->uptime = $attrs['uptime'];
} } elseif ($tag === 'report') {
else if ($tag == 'report')
{
$this->curtype = $attrs['name']; $this->curtype = $attrs['name'];
} } elseif ($tag === 'item') {
else if ($tag == 'item') if ($this->report === false || $this->curtype === false) {
{
if ($this->report === FALSE || $this->curtype === FALSE)
{
return; return;
} }
$attrs['type'] = $this->curtype; $attrs['type'] = $this->curtype;
$this->report->items[] = $attrs; $this->report->items[] = $attrs;
} }
} }
public function tag_close($parser, $tag) public function tag_close($parser, string $tag): void
{ {
if ($tag == 'report') if ($tag === 'report') {
{ $this->curtype = false;
$this->curtype = FALSE;
} }
} }
} }
?>