Skip to content

Commit 588747c

Browse files
committed
[+]: add phpstan
1 parent 4508e5b commit 588747c

10 files changed

+168
-73
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
/.gitignore export-ignore
77
/.travis.yml export-ignore
88
/phpunit.xml export-ignore
9+
/phpcs.php_cs export-ignore
10+
/phpstan.neon export-ignore

.travis.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@ matrix:
1111
- php: 7.1
1212
- php: 7.2
1313
- php: 7.3
14+
- php: 7.4
1415
- php: nightly
1516

1617
before_script:
1718
- php --version
1819
- wget https://scrutinizer-ci.com/ocular.phar
1920
- travis_retry composer self-update
20-
- travis_retry composer require satooshi/php-coveralls:1.0.0
21+
- travis_retry composer require satooshi/php-coveralls
22+
- if [ "$(phpenv version-name)" == 7.3 ]; then travis_retry composer require phpstan/phpstan; fi
2123
- travis_retry composer install --no-interaction --prefer-source
2224
- composer dump-autoload -o
2325

2426
script:
2527
- mkdir -p build/logs
2628
- php vendor/bin/phpunit -c phpunit.xml --debug
29+
- if [ "$(phpenv version-name)" == 7.3 ]; then php vendor/bin/phpstan analyse; fi
2730

2831
after_script:
2932
- php vendor/bin/coveralls -v

composer.json

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
{
22
"name": "sabas/edifact",
33
"description": "Tools in PHP for UN/EDIFACT",
4-
"keywords": ["EDI", "EDIFACT", "message", "container"],
4+
"keywords": [
5+
"EDI",
6+
"EDIFACT",
7+
"message",
8+
"container"
9+
],
510
"homepage": "https://github.com/php-edifact/edifact",
611
"license": "LGPL-3.0-or-later",
712
"authors": [
@@ -20,28 +25,28 @@
2025
"role": "Developer"
2126
}
2227
],
23-
"support": {
24-
"issues": "https://github.com/php-edifact/edifact/issues"
25-
},
26-
"repositories": [
27-
{
28-
"type": "vcs",
29-
"url": "https://github.com/php-edifact/edifact"
30-
}
31-
],
3228
"require": {
3329
"php": ">=7.0.0",
34-
"ext-simplexml": "*",
3530
"ext-json": "*",
36-
"voku/arrayy": "5.*"
31+
"ext-simplexml": "*",
32+
"voku/arrayy": "~7.1"
3733
},
3834
"require-dev": {
39-
"phpunit/phpunit": "~6.0",
40-
"php-edifact/edifact-mapping": "dev-master"
35+
"php-edifact/edifact-mapping": "dev-master",
36+
"phpunit/phpunit": "~6.0"
4137
},
4238
"autoload": {
4339
"psr-4": {
4440
"EDI\\": "src/EDI/"
4541
}
42+
},
43+
"repositories": [
44+
{
45+
"type": "vcs",
46+
"url": "https://github.com/php-edifact/edifact"
47+
}
48+
],
49+
"support": {
50+
"issues": "https://github.com/php-edifact/edifact/issues"
4651
}
4752
}

phpstan.neon

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
level: max
3+
checkMissingIterableValueType: false
4+
paths:
5+
- %currentWorkingDirectory%/src/
6+
ignoreErrors:

src/EDI/Analyser.php

+55-17
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,28 @@
1111
class Analyser
1212
{
1313

14+
/**
15+
* @var array<mixed>
16+
*/
1417
public $segments;
18+
19+
/**
20+
* @var array<mixed>
21+
*/
1522
private $jsonedi;
1623

1724
/**
1825
* @param string $message_xml_file
1926
*
20-
* @return array
27+
* @return array|false
2128
*/
22-
public function loadMessageXml(string $message_xml_file): array
29+
public function loadMessageXml(string $message_xml_file)
2330
{
2431
$messageXmlString = \file_get_contents($message_xml_file);
32+
if ($messageXmlString === false) {
33+
return false;
34+
}
35+
2536
$messageXml = new \SimpleXMLIterator($messageXmlString);
2637
unset($messageXmlString);
2738
$message = [
@@ -92,7 +103,7 @@ protected function readXmlNodes(\SimpleXMLElement $element): array
92103
protected function readAttributesArray(\SimpleXMLElement $element): array
93104
{
94105
$attributes = [];
95-
foreach ($element->attributes() as $attrName => $attr) {
106+
foreach ($element->attributes() ?? [] as $attrName => $attr) {
96107
$attributes[(string)$attrName] = (string)$attr;
97108
}
98109

@@ -104,23 +115,36 @@ protected function readAttributesArray(\SimpleXMLElement $element): array
104115
*
105116
* @param string $codesXml
106117
*
107-
* @return array
118+
* @return array|false
108119
*/
109-
public function loadCodesXml(string $codesXml): array
120+
public function loadCodesXml(string $codesXml)
110121
{
111122
$codesXmlString = \file_get_contents($codesXml);
123+
if ($codesXmlString === false) {
124+
return false;
125+
}
126+
112127
$codesXml = new \SimpleXMLIterator($codesXmlString);
113128
unset($codesXmlString);
114129
$codes = [];
115-
/* @var \SimpleXmlIterator $codeCollection */
116130
foreach ($codesXml as $codeCollection) {
117-
$id = (string)$codeCollection->attributes()->id;
131+
\assert($codeCollection instanceof \SimpleXMLIterator);
132+
133+
$codeCollectionAttributes = $codeCollection->attributes();
134+
if ($codeCollectionAttributes === null) {
135+
continue;
136+
}
137+
138+
$id = (string)$codeCollectionAttributes->id;
118139
$codes[$id] = [];
119-
/* @var \SimpleXmlIterator $codeNode */
120140
foreach ($codeCollection as $codeNode) {
141+
\assert($codeNode instanceof \SimpleXMLIterator);
142+
121143
$codeAttributes = $codeNode->attributes();
122-
$code = (string)$codeAttributes->id;
123-
$codes[$id][$code] = (string)$codeAttributes->desc;
144+
if ($codeAttributes !== null) {
145+
$code = (string)$codeAttributes->id;
146+
$codes[$id][$code] = (string)$codeAttributes->desc;
147+
}
124148
}
125149
}
126150

@@ -133,18 +157,32 @@ public function loadCodesXml(string $codesXml): array
133157
*
134158
* @param string $segment_xml_file
135159
*
136-
* @return array
160+
* @return array|false
137161
*/
138-
public function loadSegmentsXml(string $segment_xml_file): array
162+
public function loadSegmentsXml(string $segment_xml_file)
139163
{
164+
// reset
165+
$this->segments = [];
166+
140167
$segments_xml = \file_get_contents($segment_xml_file);
168+
if ($segments_xml === false) {
169+
return false;
170+
}
141171

142172
$xml = \simplexml_load_string($segments_xml);
143-
unset($segments_xml);
144-
$this->segments = [];
173+
if ($xml === false) {
174+
return false;
175+
}
176+
177+
// free memory
178+
$segments_xml = null;
145179

146-
/* @var \SimpleXMLElement $segmentNode */
147180
foreach ($xml as $segmentNode) {
181+
182+
/** @noinspection PhpSillyAssignmentInspection - hack for phpstan */
183+
/* @var \SimpleXMLElement $segmentNode */
184+
$segmentNode = $segmentNode;
185+
148186
$qualifier = (string)$segmentNode->attributes()->id;
149187
$segment = [];
150188
$segment["attributes"] = $this->readAttributesArray($segmentNode);
@@ -265,9 +303,9 @@ public function process(array $data, array $rawSegments = null): string
265303
/**
266304
* return the processed EDI in json format
267305
*
268-
* @return string json
306+
* @return string|false
269307
*/
270-
public function getJson(): string
308+
public function getJson()
271309
{
272310
return \json_encode($this->jsonedi);
273311
}

src/EDI/Encoder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private function escapeValue(&$str): string
172172
$this->symbRel . $this->symbEnd,
173173
];
174174

175-
return \str_replace($search, $replace, $str);
175+
return \str_replace($search, $replace, (string)$str);
176176
}
177177

178178
/**

src/EDI/Interpreter.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Interpreter
2323
private $xmlSeg;
2424

2525
/**
26-
* @var string
26+
* @var array
2727
*/
2828
private $xmlSvc;
2929

@@ -48,7 +48,7 @@ class Interpreter
4848
private $serviceSeg;
4949

5050
/**
51-
* @var \Closure
51+
* @var callable
5252
*/
5353
private $comparisonFunction;
5454

@@ -90,7 +90,18 @@ class Interpreter
9090
*/
9191
public function __construct(string $xmlMsg, array $xmlSeg, array $xmlSvc, array $messageTextConf = null)
9292
{
93-
$this->xmlMsg = \simplexml_load_file($xmlMsg);
93+
// simplexml_load_file: This can be affected by a PHP bug #62577 (https://bugs.php.net/bug.php?id=62577)
94+
$xmlData = \file_get_contents($xmlMsg);
95+
if ($xmlData === false) {
96+
throw new \InvalidArgumentException('file_get_contents for "' . $xmlMsg . '" faild');
97+
}
98+
99+
$xmlMsgTmp = \simplexml_load_string($xmlData);
100+
if ($xmlMsgTmp === false) {
101+
throw new \InvalidArgumentException('simplexml_load_string for "' . $xmlMsg . '" faild');
102+
}
103+
104+
$this->xmlMsg = $xmlMsgTmp;
94105
$this->xmlSeg = $xmlSeg;
95106
$this->xmlSvc = $xmlSvc;
96107
if ($messageTextConf !== null) {
@@ -571,7 +582,7 @@ public function getJson(bool $pretty = false)
571582
/**
572583
* Get result as Arrayy Object.
573584
*
574-
* @return Arrayy
585+
* @return Arrayy<mixed,mixed>
575586
*/
576587
public function getArrayy()
577588
{
@@ -627,7 +638,7 @@ public function getJsonServiceSegments(bool $pretty = false)
627638
/**
628639
* Get service segments as Arrayy Object.
629640
*
630-
* @return Arrayy
641+
* @return Arrayy<mixed,mixed>
631642
*/
632643
public function getArrayyServiceSegments()
633644
{

0 commit comments

Comments
 (0)