Skip to content

Commit aa4f65e

Browse files
committed
[+]: "Parser" -> ~ 50% better parse performance
[+]: use php7 parameter type hints [*]: add ".travis.yml" -> https://travis-ci.com (https://codecov.io | https://coveralls.io | https://scrutinizer-ci.com) [*]: add ".editorconfig" -> same base config in vim, PhpStorm, ... [*]: add ".gitattributes" -> exclude some files, if you install the package via composer
1 parent a1257b9 commit aa4f65e

10 files changed

+243
-102
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 4
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true

.gitattributes

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
* text=auto
2+
3+
/tests export-ignore
4+
/.editorconfig export-ignore
5+
/.gitattributes export-ignore
6+
/.gitignore export-ignore
7+
/.travis.yml export-ignore
8+
/phpunit.xml export-ignore

.travis.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
language: php
2+
3+
sudo: false
4+
5+
matrix:
6+
fast_finish: true
7+
allow_failures:
8+
- php: nightly
9+
include:
10+
- php: 7.0
11+
- php: 7.1
12+
- php: 7.2
13+
- php: 7.3
14+
- php: nightly
15+
16+
before_script:
17+
- php --version
18+
- wget https://scrutinizer-ci.com/ocular.phar
19+
- travis_retry composer self-update
20+
- travis_retry composer require satooshi/php-coveralls:1.0.0
21+
- travis_retry composer install --no-interaction --prefer-source
22+
- composer dump-autoload -o
23+
24+
script:
25+
- mkdir -p build/logs
26+
- php vendor/bin/phpunit -c phpunit.xml --debug
27+
28+
after_script:
29+
- php vendor/bin/coveralls -v
30+
- php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml
31+
- bash <(curl -s https://codecov.io/bash)

phpunit.xml

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit bootstrap="tests/bootstrap.php" colors="true">
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="tests/bootstrap.php"
13+
verbose="true"
14+
testdox="true"
15+
>
416
<testsuites>
517
<testsuite name="sabas/edifact">
618
<directory>./tests/EDITest</directory>
719
</testsuite>
820
</testsuites>
921
<filter>
10-
<whitelist>
11-
<directory suffix=".php">./src/</directory>
12-
</whitelist>
22+
<whitelist processUncoveredFilesFromWhitelist="true">
23+
<directory suffix=".php">./src/</directory>
24+
</whitelist>
25+
<blacklist>
26+
<directory suffix=".php">./vendor</directory>
27+
</blacklist>
1328
</filter>
1429
<logging>
15-
<log type="coverage-html" target="./codeCoverage"/>
30+
<log type="coverage-clover" target="build/logs/clover.xml"/>
1631
</logging>
1732
</phpunit>

src/EDI/Analyser.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Analyser
1919
*
2020
* @return array
2121
*/
22-
public function loadMessageXml($message_xml_file): array
22+
public function loadMessageXml(string $message_xml_file): array
2323
{
2424
$messageXmlString = \file_get_contents($message_xml_file);
2525
$messageXml = new \SimpleXMLIterator($messageXmlString);
@@ -106,7 +106,7 @@ protected function readAttributesArray(\SimpleXMLElement $element): array
106106
*
107107
* @return array
108108
*/
109-
public function loadCodesXml($codesXml): array
109+
public function loadCodesXml(string $codesXml): array
110110
{
111111
$codesXmlString = \file_get_contents($codesXml);
112112
$codesXml = new \SimpleXMLIterator($codesXmlString);
@@ -135,7 +135,7 @@ public function loadCodesXml($codesXml): array
135135
*
136136
* @return array
137137
*/
138-
public function loadSegmentsXml($segment_xml_file): array
138+
public function loadSegmentsXml(string $segment_xml_file): array
139139
{
140140
$segments_xml = \file_get_contents($segment_xml_file);
141141

@@ -166,7 +166,7 @@ public function loadSegmentsXml($segment_xml_file): array
166166
*
167167
* @return string file
168168
*/
169-
public function process($data, $rawSegments = null): string
169+
public function process(array $data, array $rawSegments = null): string
170170
{
171171
$r = [];
172172
foreach ($data as $nrow => $segment) {

src/EDI/Encoder.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,21 @@ public function __construct($array = null, $wrap = true)
7878
}
7979

8080
/**
81-
* @param array $arr
81+
* @param array $array
8282
* @param bool $wrap
8383
* @param bool $filterKeys
8484
*
8585
* @return string
8686
*/
87-
public function encode($arr, $wrap = true, $filterKeys = false): string
87+
public function encode(array $array, $wrap = true, $filterKeys = false): string
8888
{
89-
$this->originalArray = $arr;
89+
$this->originalArray = $array;
9090
$this->wrap = $wrap;
9191

9292
$edistring = '';
93-
$count = \count($arr);
93+
$count = \count($array);
9494
$k = 0;
95-
foreach ($arr as $row) {
95+
foreach ($array as $row) {
9696
$k++;
9797
if ($filterKeys) {
9898
unset($row['segmentIdx']);
@@ -113,7 +113,7 @@ public function encode($arr, $wrap = true, $filterKeys = false): string
113113
*
114114
* @return string
115115
*/
116-
public function encodeSegment($row): string
116+
public function encodeSegment(array $row): string
117117
{
118118
// init
119119
$str = '';
@@ -122,7 +122,7 @@ public function encodeSegment($row): string
122122
foreach ($row as $i => &$iValue) {
123123
if (\is_array($iValue)) {
124124
if (
125-
\count($iValue) == 1
125+
\count($iValue) === 1
126126
&&
127127
\is_array(\reset($iValue))
128128
) {
@@ -159,7 +159,7 @@ public function encodeSegment($row): string
159159
*
160160
* @return string
161161
*/
162-
private function escapeValue(&$str): string
162+
private function escapeValue(string &$str): string
163163
{
164164
$search = [
165165
$this->symbRel,
@@ -205,7 +205,7 @@ public function get(): string
205205
*
206206
* @return bool
207207
*/
208-
public function setUNA($chars, $user_call = true): bool
208+
public function setUNA(string $chars, bool $user_call = true): bool
209209
{
210210
if (
211211
\is_string($chars)

src/EDI/Interpreter.php

+24-20
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ class Interpreter
8181
/**
8282
* Split multiple messages and process
8383
*
84-
* @param string $xmlMsg Path to XML Message representation
85-
* @param array $xmlSeg Segments processed by EDI\Analyser::loadSegmentsXml
86-
* @param array $xmlSvc Service segments processed by EDI\Analyser::loadSegmentsXml
87-
* @param array $messageTextConf Personalisation of error messages
84+
* @param string $xmlMsg Path to XML Message representation
85+
* @param array $xmlSeg Segments processed by EDI\Analyser::loadSegmentsXml
86+
* @param array $xmlSvc Service segments processed by EDI\Analyser::loadSegmentsXml
87+
* @param null|array $messageTextConf Personalisation of error messages
8888
*/
89-
public function __construct($xmlMsg, $xmlSeg, $xmlSvc, $messageTextConf = null)
89+
public function __construct(string $xmlMsg, array $xmlSeg, array $xmlSvc, array $messageTextConf = null)
9090
{
9191
$this->xmlMsg = \simplexml_load_file($xmlMsg);
9292
$this->xmlSeg = $xmlSeg;
@@ -108,7 +108,7 @@ public function __construct($xmlMsg, $xmlSeg, $xmlSvc, $messageTextConf = null)
108108
*
109109
* @return void
110110
*/
111-
public function setMessageTextConf($messageTextConf)
111+
public function setMessageTextConf(array $messageTextConf)
112112
{
113113
$this->messageTextConf = \array_replace($this->messageTextConf, $messageTextConf);
114114
}
@@ -120,7 +120,7 @@ public function setMessageTextConf($messageTextConf)
120120
*
121121
* @return void
122122
*/
123-
public function setSegmentTemplates($segmentTemplates)
123+
public function setSegmentTemplates(array $segmentTemplates)
124124
{
125125
$this->segmentTemplates = $segmentTemplates;
126126
}
@@ -132,7 +132,7 @@ public function setSegmentTemplates($segmentTemplates)
132132
*
133133
* @return void
134134
*/
135-
public function setGroupTemplates($groupTemplates)
135+
public function setGroupTemplates(array $groupTemplates)
136136
{
137137
$this->groupTemplates = $groupTemplates;
138138
}
@@ -156,7 +156,7 @@ public function setComparisonFunction(callable $func)
156156
*
157157
* @return array
158158
*/
159-
public function prepare($parsed): array
159+
public function prepare(array $parsed): array
160160
{
161161
$this->msgs = $this->splitMessages($parsed, $this->errors);
162162
$groups = [];
@@ -267,7 +267,7 @@ private function splitMessages(&$parsed, &$errors): array
267267
*
268268
* @return array
269269
*/
270-
private function loopMessage(&$message, $xml, &$errors): array
270+
private function loopMessage(array &$message, \SimpleXMLElement $xml, array &$errors): array
271271
{
272272
// init
273273
$groupedEdi = [];
@@ -301,7 +301,7 @@ private function loopMessage(&$message, $xml, &$errors): array
301301
*
302302
* @return void
303303
*/
304-
private function processXmlGroup($elm, &$message, &$segmentIdx, &$array, &$errors)
304+
private function processXmlGroup(\SimpleXMLElement $elm, array &$message, int &$segmentIdx, array &$array, array &$errors)
305305
{
306306
// init
307307
$groupVisited = false;
@@ -345,7 +345,7 @@ private function processXmlGroup($elm, &$message, &$segmentIdx, &$array, &$error
345345

346346
}
347347

348-
if (\count($newGroup) == 0) {
348+
if (\count($newGroup) === 0) {
349349
return;
350350
}
351351

@@ -363,9 +363,11 @@ private function processXmlGroup($elm, &$message, &$segmentIdx, &$array, &$error
363363
*
364364
* @return void
365365
*/
366-
private function processXmlSegment($elm, &$message, &$segmentIdx, &$array, &$errors)
366+
private function processXmlSegment(\SimpleXMLElement $elm, array &$message, int &$segmentIdx, array &$array, array &$errors)
367367
{
368+
// init
368369
$segmentVisited = false;
370+
369371
for ($i = 0; $i < $elm['maxrepeat']; $i++) {
370372

371373
if (\call_user_func($this->comparisonFunction, $message[$segmentIdx], $elm)) {
@@ -412,7 +414,7 @@ private function processXmlSegment($elm, &$message, &$segmentIdx, &$array, &$err
412414
*
413415
* @return void
414416
*/
415-
private function doAddArray(&$array, &$jsonMessage)
417+
private function doAddArray(array &$array, array &$jsonMessage)
416418
{
417419
if (isset($array[$jsonMessage['key']])) {
418420
if (
@@ -435,12 +437,12 @@ private function doAddArray(&$array, &$jsonMessage)
435437
*
436438
* @param array $segment
437439
* @param array $xmlMap
438-
* @param int $segmentIdx
440+
* @param null|int $segmentIdx
439441
* @param null|array $errors
440442
*
441443
* @return array
442444
*/
443-
private function processSegment(&$segment, &$xmlMap, $segmentIdx, &$errors = null): array
445+
private function processSegment(array &$segment, array &$xmlMap, $segmentIdx, array &$errors = null): array
444446
{
445447
$id = $segment[0];
446448

@@ -508,7 +510,7 @@ private function processSegment(&$segment, &$xmlMap, $segmentIdx, &$errors = nul
508510
$jsoncomposite = $detail;
509511
}
510512

511-
if (array_key_exists($d_desc_attr['name'], $jsonelements)) {
513+
if (\array_key_exists($d_desc_attr['name'], $jsonelements)) {
512514
$jsonelements[$d_desc_attr['name'] . $n] = $jsoncomposite;
513515
} else {
514516
$jsonelements[$d_desc_attr['name']] = $jsoncomposite;
@@ -535,9 +537,11 @@ private function processSegment(&$segment, &$xmlMap, $segmentIdx, &$errors = nul
535537
*
536538
* @return array
537539
*/
538-
private function processService(&$segments): array
540+
private function processService(array &$segments): array
539541
{
542+
// init
540543
$processed = [];
544+
541545
foreach ($segments as &$seg) {
542546
$jsonsegment = $this->processSegment($seg, $this->xmlSvc, null);
543547
$processed[$jsonsegment['key']] = $jsonsegment['value'];
@@ -553,7 +557,7 @@ private function processService(&$segments): array
553557
*
554558
* @return false|string
555559
*/
556-
public function getJson($pretty = false)
560+
public function getJson(bool $pretty = false)
557561
{
558562
if ($pretty) {
559563
return \json_encode($this->ediGroups, JSON_PRETTY_PRINT);
@@ -593,7 +597,7 @@ public function getServiceSegments()
593597
*
594598
* @return false|string
595599
*/
596-
public function getJsonServiceSegments($pretty = false)
600+
public function getJsonServiceSegments(bool $pretty = false)
597601
{
598602
if ($pretty) {
599603
return \json_encode($this->serviceSeg, JSON_PRETTY_PRINT);

0 commit comments

Comments
 (0)