1111class 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 }
0 commit comments