Skip to content

Commit 2949865

Browse files
committed
Cleaning
1 parent 29f4c57 commit 2949865

9 files changed

+234
-512
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{
1414
"name": "Stefano Sabatini",
1515
"email": "[email protected]",
16-
"homepage": "http://stefanosabatini.eu",
16+
"homepage": "http://stefanosabatini.com",
1717
"role": "Developer"
1818
},
1919
{
@@ -34,7 +34,7 @@
3434
"require-dev": {
3535
"php-edifact/edifact-mapping": "dev-master",
3636
"phpunit/phpunit": "~9.0",
37-
"voku/arrayy": "~7.1"
37+
"symplify/easy-coding-standard": "^12.0"
3838
},
3939
"autoload": {
4040
"psr-4": {

ecs.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
6+
use Symplify\EasyCodingStandard\Config\ECSConfig;
7+
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
8+
9+
return function (ECSConfig $ecsConfig): void {
10+
$ecsConfig->paths([
11+
__DIR__ . '/src',
12+
__DIR__ . '/tests',
13+
]);
14+
15+
// this way you add a single rule
16+
$ecsConfig->rules([
17+
NoUnusedImportsFixer::class,
18+
]);
19+
20+
// this way you can add sets - group of rules
21+
$ecsConfig->sets([
22+
// run and fix, one by one
23+
SetList::SPACES,
24+
SetList::ARRAY,
25+
// SetList::DOCBLOCK,
26+
// SetList::NAMESPACES,
27+
// SetList::COMMENTS,
28+
SetList::PSR_12,
29+
SetList::LARAVEL,
30+
SetList::CLEAN_CODE,
31+
]);
32+
};

phpcs.php_cs

-238
This file was deleted.

phpunit.xml

-6
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,4 @@
1414
<directory>tests</directory>
1515
</testsuite>
1616
</testsuites>
17-
18-
<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
19-
<include>
20-
<directory>src</directory>
21-
</include>
22-
</source>
2317
</phpunit>

src/EDI/Analyser.php

+38-51
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ class Analyser
2424
* @var array<mixed>
2525
*/
2626
private $jsonedi;
27+
2728
/**
2829
* @var array
2930
*/
3031
private $codes;
3132

32-
public function setXml($segments, $codes) {
33+
public function setXml($segments, $codes)
34+
{
3335
$this->segments = $segments;
3436
$this->codes = $codes;
3537
}
3638

3739
/**
38-
* @param string $message_xml_file
39-
*
4040
* @return array|false
4141
*/
4242
public function loadMessageXml(string $message_xml_file)
@@ -57,7 +57,6 @@ public function loadMessageXml(string $message_xml_file)
5757
/**
5858
* get all data element codes
5959
*
60-
* @param string $codesXml
6160
*
6261
* @return array|false
6362
*/
@@ -99,8 +98,6 @@ public function loadCodesXml(string $codesXml)
9998
* composite_data_element same as in XML
10099
* For single message, it's vailable also in (new EDI\Mapping\MappingProvider($version))->loadSegmentsXml()
101100
*
102-
* @param string $segmentXmlFile
103-
* @param bool $discardOldSegments
104101
*
105102
* @return array|false
106103
*/
@@ -134,7 +131,7 @@ public function loadSegmentsXml(string $segmentXmlFile, bool $discardOldSegments
134131
$segment = [];
135132
$segment['attributes'] = $this->readAttributesArray($segmentNode);
136133
$details = $this->readXmlNodes($segmentNode);
137-
if (!empty($details)) {
134+
if (! empty($details)) {
138135
$segment['details'] = $details;
139136
}
140137
$this->segments[$qualifier] = $segment;
@@ -147,13 +144,14 @@ public function loadSegmentsXml(string $segmentXmlFile, bool $discardOldSegments
147144
* Load segment definitions from multiple files
148145
*
149146
* @see Analyser::loadSegmentsXml()
147+
*
150148
* @param string[] $segmentXmlFiles
151149
* @return array|false
152150
*/
153151
public function loadMultiSegmentsXml(array $segmentXmlFiles)
154152
{
155153
foreach ($segmentXmlFiles as $xmlFile) {
156-
if (!$result = $this->loadSegmentsXml($xmlFile, false)) {
154+
if (! $result = $this->loadSegmentsXml($xmlFile, false)) {
157155
return $result;
158156
}
159157
}
@@ -166,7 +164,6 @@ public function loadMultiSegmentsXml(array $segmentXmlFiles)
166164
*
167165
* @param array $data by EDI\Parser:parse() created array from plain EDI message
168166
* @param array|null $rawSegments (optional) List of raw segments from EDI\Parser::getRawSegments
169-
*
170167
* @return string file
171168
*/
172169
public function process(array $data, array $rawSegments = null): string
@@ -185,25 +182,27 @@ public function process(array $data, array $rawSegments = null): string
185182
$attributes = $this->segments[$id]['attributes'];
186183
$details_desc = $this->segments[$id]['details'];
187184

188-
$idHeader = $id . ' - ' . $attributes['name'];
189-
if($this->directory && $id !== 'UNB') {
190-
$idHeader .= ' https://service.unece.org/trade/untdid/' . strtolower($this->directory) . '/trsd/trsd' . strtolower($id) . '.htm';
185+
$idHeader = $id.' - '.$attributes['name'];
186+
if ($this->directory && $id !== 'UNB') {
187+
$idHeader .= ' https://service.unece.org/trade/untdid/'.strtolower($this->directory).'/trsd/trsd'.strtolower($id).'.htm';
191188
}
192189
$r[] = $idHeader;
193-
$r[] = ' (' . \wordwrap($attributes['desc'], 75, \PHP_EOL . ' ') . ')';
190+
$r[] = ' ('.\wordwrap($attributes['desc'], 75, \PHP_EOL.' ').')';
194191

195-
$jsonelements = ['segmentCode' => $id];
192+
$jsonelements = [
193+
'segmentCode' => $id,
194+
];
196195
foreach ($segment as $idx => $detail) {
197196
$n = $idx - 1;
198-
if ($idx == 0 || !isset($details_desc[$n])) {
197+
if ($idx == 0 || ! isset($details_desc[$n])) {
199198
continue;
200199
}
201200
$d_desc_attr = $details_desc[$n]['attributes'];
202-
$l1 = ' ' . $d_desc_attr['id'] . ' - ' . $d_desc_attr['name'];
203-
$l2 = ' ' . \wordwrap($d_desc_attr['desc'], 71, \PHP_EOL . ' ');
201+
$l1 = ' '.$d_desc_attr['id'].' - '.$d_desc_attr['name'];
202+
$l2 = ' '.\wordwrap($d_desc_attr['desc'], 71, \PHP_EOL.' ');
204203

205204
if (\is_array($detail)) {
206-
$r[] = ' [' . $n . '] ' . \implode(',', $detail);
205+
$r[] = ' ['.$n.'] '.\implode(',', $detail);
207206
$r[] = $l1;
208207
$r[] = $l2;
209208

@@ -214,25 +213,25 @@ public function process(array $data, array $rawSegments = null): string
214213
foreach ($detail as $d_n => $d_detail) {
215214
$d_sub_desc_attr = $sub_details_desc[$d_n]['attributes'];
216215
$codeElementId = $d_sub_desc_attr['id'];
217-
$line = ' [' . $d_n . '] ' . $d_detail;
218-
if(isset($this->codes[(int)$codeElementId][$d_detail])){
219-
$line .= ' - ' . \wordwrap($this->codes[$codeElementId][$d_detail], 69, \PHP_EOL . ' ');
216+
$line = ' ['.$d_n.'] '.$d_detail;
217+
if (isset($this->codes[(int) $codeElementId][$d_detail])) {
218+
$line .= ' - '.\wordwrap($this->codes[$codeElementId][$d_detail], 69, \PHP_EOL.' ');
220219
}
221220
$r[] = $line;
222221

223-
$r[] = ' id: ' . $codeElementId . ' - ' . $d_sub_desc_attr['name'];
224-
$r[] = ' ' . \wordwrap($d_sub_desc_attr['desc'], 69, \PHP_EOL . ' ');
225-
$r[] = ' type: ' . $d_sub_desc_attr['type'];
222+
$r[] = ' id: '.$codeElementId.' - '.$d_sub_desc_attr['name'];
223+
$r[] = ' '.\wordwrap($d_sub_desc_attr['desc'], 69, \PHP_EOL.' ');
224+
$r[] = ' type: '.$d_sub_desc_attr['type'];
226225

227226
$jsoncomposite[$d_sub_desc_attr['name']] = $d_detail;
228227
if (isset($d_sub_desc_attr['maxlength'])) {
229-
$r[] = ' maxlen: ' . $d_sub_desc_attr['maxlength'];
228+
$r[] = ' maxlen: '.$d_sub_desc_attr['maxlength'];
230229
}
231230
if (isset($d_sub_desc_attr['required'])) {
232-
$r[] = ' required: ' . $d_sub_desc_attr['required'];
231+
$r[] = ' required: '.$d_sub_desc_attr['required'];
233232
}
234233
if (isset($d_sub_desc_attr['length'])) {
235-
$r[] = ' length: ' . $d_sub_desc_attr['length'];
234+
$r[] = ' length: '.$d_sub_desc_attr['length'];
236235
}
237236

238237
// check for skipped data
@@ -256,16 +255,16 @@ public function process(array $data, array $rawSegments = null): string
256255
$jsonelements[$d_desc_attr['name']] = $jsoncomposite;
257256
} else {
258257
$codeElementId = $d_desc_attr['id'];
259-
$line = ' [' . $n . '] ' . $detail;
260-
if(isset($this->codes[(int)$codeElementId][$detail])){
261-
/*
262-
* for retrieving code element description when first element of the segment
263-
* is a data element and not a composite one. Ex: NAD segment.
264-
* We rewrite also l1 line for adding 'id:' prefix before data element id.
265-
* It's just a cosmetic fix
266-
*/
267-
$line .= ' - ' . \wordwrap($this->codes[$codeElementId][$detail], 71, \PHP_EOL . ' ');
268-
$l1 = ' id: ' . $d_desc_attr['id'] . ' - ' . $d_desc_attr['name'];
258+
$line = ' ['.$n.'] '.$detail;
259+
if (isset($this->codes[(int) $codeElementId][$detail])) {
260+
/*
261+
* for retrieving code element description when first element of the segment
262+
* is a data element and not a composite one. Ex: NAD segment.
263+
* We rewrite also l1 line for adding 'id:' prefix before data element id.
264+
* It's just a cosmetic fix
265+
*/
266+
$line .= ' - '.\wordwrap($this->codes[$codeElementId][$detail], 71, \PHP_EOL.' ');
267+
$l1 = ' id: '.$d_desc_attr['id'].' - '.$d_desc_attr['name'];
269268
}
270269
$r[] = $line;
271270
$r[] = $l1;
@@ -296,17 +295,13 @@ public function getJson()
296295

297296
/**
298297
* read default values in given message xml
299-
*
300-
* @param \SimpleXMLElement $message
301-
*
302-
* @return array
303298
*/
304299
protected function readMessageDefaults(\SimpleXMLElement $message): array
305300
{
306301
// init
307302
$defaults = [];
308303

309-
/* @var \SimpleXMLElement $defaultValueNode */
304+
/** @var \SimpleXMLElement $defaultValueNode */
310305
foreach ($message->defaults[0] ?? [] as $defaultValueNode) {
311306
$attributes = $defaultValueNode->attributes();
312307
$id = (string) $attributes->id;
@@ -318,10 +313,6 @@ protected function readMessageDefaults(\SimpleXMLElement $message): array
318313

319314
/**
320315
* read message segments and groups
321-
*
322-
* @param \SimpleXMLElement $element
323-
*
324-
* @return array
325316
*/
326317
protected function readXmlNodes(\SimpleXMLElement $element): array
327318
{
@@ -334,7 +325,7 @@ protected function readXmlNodes(\SimpleXMLElement $element): array
334325
$arrayElement['type'] = $name;
335326
$arrayElement['attributes'] = $this->readAttributesArray($node);
336327
$details = $this->readXmlNodes($node);
337-
if (!empty($details)) {
328+
if (! empty($details)) {
338329
$arrayElement['details'] = $details;
339330
}
340331
$arrayElements[] = $arrayElement;
@@ -345,10 +336,6 @@ protected function readXmlNodes(\SimpleXMLElement $element): array
345336

346337
/**
347338
* return an xml elements attributes in as array
348-
*
349-
* @param \SimpleXMLElement $element
350-
*
351-
* @return array
352339
*/
353340
protected function readAttributesArray(\SimpleXMLElement $element): array
354341
{

src/EDI/Encoder.php

+12-30
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ public function __construct($array = null, $compact = true)
8181
* @param array[] $array
8282
* @param bool $compact All segments on a single line?
8383
* @param bool $filterKeys
84-
*
85-
* @return string
8684
*/
8785
public function encode(array $array, $compact = true, $filterKeys = false): string
8886
{
@@ -93,13 +91,13 @@ public function encode(array $array, $compact = true, $filterKeys = false): stri
9391
$count = \count($array);
9492
$k = 0;
9593
foreach ($array as $row) {
96-
++$k;
94+
$k++;
9795
if ($filterKeys) {
9896
unset($row['segmentIdx']);
9997
}
10098
$row = \array_values($row);
10199
$edistring .= $this->encodeSegment($row);
102-
if (!$compact && $k < $count) {
100+
if (! $compact && $k < $count) {
103101
$edistring .= "\n";
104102
}
105103
}
@@ -108,11 +106,6 @@ public function encode(array $array, $compact = true, $filterKeys = false): stri
108106
return $edistring;
109107
}
110108

111-
/**
112-
* @param array $row
113-
*
114-
* @return string
115-
*/
116109
public function encodeSegment(array $row): string
117110
{
118111
// init
@@ -155,34 +148,25 @@ public function encodeSegment(array $row): string
155148
return $str;
156149
}
157150

158-
/**
159-
* @return string
160-
*/
161151
public function get(): string
162152
{
163153
if ($this->UNAActive) {
164-
$una = 'UNA' . $this->sepComp .
165-
$this->sepData .
166-
$this->sepDec .
167-
$this->symbRel .
168-
$this->symbRep .
154+
$una = 'UNA'.$this->sepComp.
155+
$this->sepData.
156+
$this->sepDec.
157+
$this->symbRel.
158+
$this->symbRep.
169159
$this->symbEnd;
170160
if ($this->compact === false) {
171161
$una .= "\n";
172162
}
173163

174-
return $una . $this->output;
164+
return $una.$this->output;
175165
}
176166

177167
return $this->output;
178168
}
179169

180-
/**
181-
* @param string $chars
182-
* @param bool $user_call
183-
*
184-
* @return bool
185-
*/
186170
public function setUNA(string $chars, bool $user_call = true): bool
187171
{
188172
if (\strlen($chars) == 6) {
@@ -225,8 +209,6 @@ public function disableUNA()
225209

226210
/**
227211
* @param int|string $str
228-
*
229-
* @return string
230212
*/
231213
private function escapeValue(&$str): string
232214
{
@@ -237,10 +219,10 @@ private function escapeValue(&$str): string
237219
$this->symbEnd,
238220
];
239221
$replace = [
240-
$this->symbRel . $this->symbRel,
241-
$this->symbRel . $this->sepComp,
242-
$this->symbRel . $this->sepData,
243-
$this->symbRel . $this->symbEnd,
222+
$this->symbRel.$this->symbRel,
223+
$this->symbRel.$this->sepComp,
224+
$this->symbRel.$this->sepData,
225+
$this->symbRel.$this->symbEnd,
244226
];
245227

246228
return \str_replace($search, $replace, (string) $str);

0 commit comments

Comments
 (0)