Skip to content

Commit 15f61d0

Browse files
committed
[-]: use "strpos()" before "preg_replace()" only if it makes sense
1 parent aa4f65e commit 15f61d0

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

src/EDI/Parser.php

+34-19
Original file line numberDiff line numberDiff line change
@@ -345,24 +345,33 @@ private function unwrap(string &$string): array
345345
if (
346346
!$this->unaChecked
347347
&&
348-
\strpos($string, "UNA") === 0
348+
\strpos($string, 'UNA') === 0
349349
) {
350-
$this->analyseUNA(\preg_replace("#^UNA#", '', substr($string, 0, 9)));
350+
$this->analyseUNA(
351+
\substr(\substr($string, 3), 0, 9)
352+
);
351353
}
352354

353355
if (
354356
!$this->unbChecked
355357
&&
356-
\strpos($string, "UNB") === 0
358+
\strpos($string, 'UNB') === 0
357359
) {
358-
$this->analyseUNB(\preg_replace("#^UNB\+#", '', substr($string, 0, 8)));
360+
$this->analyseUNB(
361+
\preg_replace(
362+
"#^UNB\+#",
363+
'',
364+
\substr($string, 0, 8)
365+
)
366+
);
359367
}
360368

361369
$string = \preg_replace(
362370
"/(([^" . $this->symbRel . "]" . $this->symbRel . "{2})+|[^" . $this->symbRel . "])" . $this->symbEnd . "/",
363371
"$1" . $this->stringSafe,
364372
$string
365373
);
374+
366375
$file = \preg_split(
367376
self::$DELIMITER . $this->stringSafe . self::$DELIMITER . "i",
368377
$string
@@ -393,31 +402,36 @@ private function unwrap(string &$string): array
393402
*/
394403
private function splitSegment(string &$str): array
395404
{
396-
// remove ending symbEnd
397-
if (\strpos($str, $this->symbEnd) !== false) {
398-
$str = \preg_replace(
405+
// remove ending "symbEnd"
406+
$str = \trim(
407+
\preg_replace(
399408
self::$DELIMITER . $this->symbEnd . '$' . self::$DELIMITER,
400409
'',
401410
$str
402-
);
403-
}
404-
405-
$str = \trim($str);
411+
)
412+
);
406413

407-
// replace duplicate symbRel
414+
// replace duplicate "symbRel"
408415
$str = \str_replace(
409416
$this->symbUnescapedRel . $this->symbUnescapedRel,
410417
$this->stringSafe,
411418
$str
412419
);
413420

414-
// split on sepData if not escaped (negative lookbehind)
421+
// split on "sepData" if not escaped (negative lookbehind)
415422
$matches = \preg_split(
416423
self::$DELIMITER . "(?<!" . $this->symbRel . ")" . $this->sepData . self::$DELIMITER,
417424
$str
418425
);
426+
// fallback
427+
if ($matches === false) {
428+
$matches = [];
429+
}
419430

420431
foreach ($matches as &$value) {
432+
if ($value === '') {
433+
continue;
434+
}
421435

422436
// INFO:
423437
//
@@ -439,15 +453,11 @@ private function splitSegment(string &$str): array
439453
}
440454
}
441455

442-
// split on sepComp
456+
// split on "sepComp"
443457
$value = $this->splitData($value);
444458
}
445459
unset($value);
446460

447-
if ($matches === false) {
448-
$matches = [];
449-
}
450-
451461
return $matches;
452462
}
453463

@@ -481,15 +491,20 @@ private function splitData(string &$str)
481491
);
482492
};
483493

494+
// check for "sepUnescapedComp" in the string
484495
if (\strpos($str, $this->sepUnescapedComp) === false) {
485496
return $replace($str);
486497
}
487498

488-
// split on sepComp if not escaped (negative lookbehind)
499+
// split on "sepComp" if not escaped (negative lookbehind)
489500
$array = \preg_split(
490501
self::$DELIMITER . "(?<!" . $this->symbRel . ")" . $this->sepComp . self::$DELIMITER,
491502
$str
492503
);
504+
// fallback
505+
if ($array === false) {
506+
$array = [[]];
507+
}
493508

494509
if (\count($array) === 1) {
495510
return $replace($str);

tests/EDITest/ParserTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testFileOk()
6767
{
6868
$string = file_get_contents(__DIR__ . '/../files/example_order_ok.edi');
6969

70-
for ($i = 0; $i < 2; $i++) { // keep for simple performance tests
70+
for ($i = 0; $i < 100; $i++) { // keep for simple performance tests
7171
$errors = (new Parser($string))->errors();
7272
}
7373

0 commit comments

Comments
 (0)