Skip to content

Commit a1257b9

Browse files
committed
[~]: optimize performance
1 parent 0a5c6e6 commit a1257b9

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

src/EDI/Parser.php

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Parser
2828
/**
2929
* @var string
3030
*/
31-
private $stripChars = "/[\x01-\x1F\x80-\xFF]/"; //UNOB encoding set
31+
private $stripChars = "/[\x01-\x1F\x80-\xFF]/"; // UNOB encoding set
3232

3333
/**
3434
* @var string
@@ -156,19 +156,21 @@ public function __construct($url = null)
156156
*/
157157
public function parse(&$file2): array
158158
{
159-
$t = \count($file2);
160-
for ($i = 1; $i <= $t; $i++) {
161-
$line = \array_shift($file2);
162-
163-
// Null byte and carriage return removal (CR+LF)
164-
$line = \preg_replace('#[\x00\r\n]#', '', $line);
165-
if (\preg_match($this->stripChars, $line)) {
166-
$this->errors[] = "There's a not printable character on line " . $i . ": " . $line;
159+
$i = 0;
160+
foreach ($file2 as &$line) {
161+
++$i;
162+
163+
// Null byte and carriage return removal. (CR+LF)
164+
$line = str_replace(["\x00", "\r", "\n"], '', $line);
165+
166+
// Basic sanitization, remove non printable chars.
167+
$lineTrim = \trim($line);
168+
$line = \preg_replace($this->stripChars, '', $lineTrim);
169+
$line_bytes = \strlen($line);
170+
if ($line_bytes !== \strlen($lineTrim)) {
171+
$this->errors[] = "There's a not printable character on line " . $i . ": " . $lineTrim;
167172
}
168-
169-
// Basic sanitization, remove non printable chars
170-
$line = \preg_replace($this->stripChars, '', \trim($line));
171-
if (\strlen($line) < 2) {
173+
if ($line_bytes < 2) {
172174
continue;
173175
}
174176

@@ -200,7 +202,6 @@ public function parse(&$file2): array
200202
return $this->parsedfile;
201203
}
202204

203-
204205
/**
205206
* Reset UNA's characters definition
206207
*
@@ -240,19 +241,19 @@ private function resetUNB()
240241
public function analyseUNA($line)
241242
{
242243
$line = \substr($line, 0, 6);
243-
if (isset($line{0})) {
244-
$this->sepComp = \preg_quote($line{0}, self::$DELIMITER);
245-
if (isset($line{1})) {
246-
$this->sepData = \preg_quote($line{1}, self::$DELIMITER);
247-
if (isset($line{2})) {
248-
$this->sepDec = $line{2}; // See later if a preg_quote is needed
249-
if (isset($line{3})) {
250-
$this->symbRel = \preg_quote($line{3}, self::$DELIMITER);
251-
$this->symbUnescapedRel = $line{3};
252-
if (isset($line{4})) {
253-
$this->symbRep = $line{4}; // See later if a preg_quote is needed
254-
if (isset($line{5})) {
255-
$this->symbEnd = \preg_quote($line{5}, self::$DELIMITER);
244+
if (isset($line[0])) {
245+
$this->sepComp = \preg_quote($line[0], self::$DELIMITER);
246+
if (isset($line[1])) {
247+
$this->sepData = \preg_quote($line[1], self::$DELIMITER);
248+
if (isset($line[2])) {
249+
$this->sepDec = $line[2]; // See later if a preg_quote is needed
250+
if (isset($line[3])) {
251+
$this->symbRel = \preg_quote($line[3], self::$DELIMITER);
252+
$this->symbUnescapedRel = $line[3];
253+
if (isset($line[4])) {
254+
$this->symbRep = $line[4]; // See later if a preg_quote is needed
255+
if (isset($line[5])) {
256+
$this->symbEnd = \preg_quote($line[5], self::$DELIMITER);
256257
}
257258
}
258259
}
@@ -275,9 +276,7 @@ public function analyseUNB($encoding)
275276
$encoding = $encoding[0];
276277
}
277278
$this->encoding = $encoding;
278-
/**
279-
* If there's a regex defined for this character set, use it
280-
*/
279+
// If there's a regex defined for this character set, use it.
281280
if (isset($this->encodingToStripChars[$encoding])) {
282281
$this->setStripRegex($this->encodingToStripChars[$encoding]);
283282
}
@@ -296,12 +295,14 @@ public function analyseUNH($line)
296295
if (\count($line) < 3) {
297296
return;
298297
}
298+
299299
$lineElement = $line[2];
300300
if (!\is_array($lineElement)) {
301301
$this->messageFormat = $lineElement;
302302

303303
return;
304304
}
305+
305306
$this->messageFormat = $lineElement[0];
306307
$this->messageDirectory = $lineElement[2];
307308
}
@@ -315,11 +316,19 @@ public function analyseUNH($line)
315316
*/
316317
private function unwrap(&$string): array
317318
{
318-
if (!$this->unaChecked && \strpos($string, "UNA") === 0) {
319+
if (
320+
!$this->unaChecked
321+
&&
322+
\strpos($string, "UNA") === 0
323+
) {
319324
$this->analyseUNA(\preg_replace("#^UNA#", "", substr($string, 0, 9)));
320325
}
321326

322-
if (!$this->unbChecked && \strpos($string, "UNB") === 0) {
327+
if (
328+
!$this->unbChecked
329+
&&
330+
\strpos($string, "UNB") === 0
331+
) {
323332
$this->analyseUNB(\preg_replace("#^UNB\+#", "", substr($string, 0, 8)));
324333
}
325334

tests/EDITest/ParserTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ public function testUNAString()
172172
$this->assertEmpty($result);
173173
}
174174

175+
/*
176+
public function testBigFile()
177+
{
178+
$p = new Parser();
179+
$p->load(__DIR__ . "/../files/example_big.edi");
180+
$result = $p->errors();
181+
$this->assertSame([], $result);
182+
}
183+
*/
184+
175185
public function testReleaseCharacter()
176186
{
177187
$p = new Parser();

0 commit comments

Comments
 (0)