Test Failed
Push — main ( 6b9298...8f70c4 )
by Daniel
03:22
created

TraitBasic::getJsonFromFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 23
ccs 10
cts 10
cp 1
crap 4
rs 9.8666

2 Methods

Rating   Name   Duplication   Size   Complexity  
A TraitBasic::getLineStringFromNumber() 0 3 2
A TraitBasic::getHierarchyTagOrder() 0 4 1
1
<?php
2
3
/*
4
 * Copyright (c) 2024, Daniel Popiniuc and its licensors.
5
 *
6
 * All rights reserved. This program and the accompanying materials
7
 * are made available under the terms of the Eclipse Public License v1.0
8
 * which accompanies this distribution, and is available at
9
 * http://www.eclipse.org/legal/epl-v20.html
10
 *
11
 * Contributors:
12
 *    Daniel Popiniuc
13
 */
14
15
namespace danielgp\efactura;
16
17
trait TraitBasic
18
{
19
    use \danielgp\io_operations\InputOutputFiles;
20
21
    protected array $arraySettings   = [];
22 1
    protected array $arrayProcessing = [];
23
24 1
    private function getCommentsFromFileAsArray(): array
25
    {
26
        return $this->getArrayFromJsonFile(__DIR__
27 13
                . DIRECTORY_SEPARATOR . 'config', 'ElectronicInvoiceComments.json');
28
    }
29 13
30 13
    private function getElements(\SimpleXMLElement | null $arrayIn): array
31 13
    {
32 13
        $arrayToReturn = [];
33 13
        if (!is_null($arrayIn)) {
34
            if (count($arrayIn->children($this->arrayProcessing['mapping']['cbc'], true)) !== 0) { // checking if we have cbc elements
35
                foreach ($arrayIn->children($this->arrayProcessing['mapping']['cbc'], true) as $key => $value) {
36 13
                    $arrayToReturn[$key] = $this->getElementSingle($value);
37 13
                }
38 13
            }
39
            if (count($arrayIn->children($this->arrayProcessing['mapping']['cac'], true)) !== 0) { // checking if we have cac elements
40
                foreach ($arrayIn->children($this->arrayProcessing['mapping']['cac'], true) as $key => $value) {
41
                    $arrayToReturn[$key] = $this->getElements($value);
42 13
                }
43
            }
44
        }
45 13
        return $arrayToReturn;
46
    }
47 13
48 13
    private function getElementSingle(\SimpleXMLElement | null $value)
49 13
    {
50 13
        $arrayToReturn = [];
51
        if (!is_null($value)) {
52 13
            if (count($value->attributes()) === 0) {
53 13
                $arrayToReturn = $value->__toString();
54 13
            } else {
55
                $arrayToReturn['value'] = $value->__toString();
56
                foreach ($value->attributes() as $keyA => $valueA) {
57 13
                    if (str_ends_with($valueA, ':CommonAggregateComponents-2')) {
0 ignored issues
show
Bug introduced by
It seems like $valueA can also be of type null; however, parameter $haystack of str_ends_with() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
                    if (str_ends_with(/** @scrutinizer ignore-type */ $valueA, ':CommonAggregateComponents-2')) {
Loading history...
58
                        // nothing
59
                    } else {
60
                        $arrayToReturn[$keyA] = $valueA->__toString();
61
                    }
62 13
                }
63
            }
64
        }
65 13
        return $arrayToReturn;
66
    }
67 13
68 13
    private function getHierarchyTagOrder(): void
69
    {
70
        $this->arraySettings['CustomOrder'] = $this->getArrayFromJsonFile(__DIR__
71
            . DIRECTORY_SEPARATOR . 'config', 'ElectronicInvoiceHierarchyTagOrder.json');
72
    }
73 13
74 13
    private function getLineStringFromNumber(int $intLineNo): string
75
    {
76
        return ($intLineNo < 10 ? '0' : '') . $intLineNo;
77
    }
78
79 13
    private function getMultipleElementsByKey(\SimpleXMLElement $arrayData): array
80 13
    {
81 13
        $arrayOutput = [];
82 13
        $intLineNo   = 0;
83
        foreach ($arrayData as $value2) {
84
            $intLineNo++;
85
            $intLineStr               = $this->getLineStringFromNumber($intLineNo);
86
            $arrayOutput[$intLineStr] = $this->getElements($value2);
87 13
        }
88
        return $arrayOutput;
89
    }
90 13
91
    private function getMultipleElementsStandard(array | \SimpleXMLElement $arrayIn): array
92 13
    {
93
        $arrayToReturn = [];
94
        $intLineNo     = 0;
95 13
        foreach ($arrayIn as $child) {
96
            $intLineNo++;
97 13
            $intLineStr = $this->getLineStringFromNumber($intLineNo);
98
            foreach ($child->children($this->arrayProcessing['mapping']['cbc'], true) as $key2 => $value2) {
99
                if (count($value2->attributes()) === 0) {
100 13
                    $arrayToReturn[$intLineStr][$key2] = $value2->__toString();
101
                } else {
102 13
                    $arrayToReturn[$intLineStr][$key2]['value'] = $value2->__toString();
103 13
                    foreach ($value2->attributes() as $keyA => $valueA) {
104 13
                        if (str_ends_with($valueA, ':CommonAggregateComponents-2')) {
105 13
                            // nothing
106 13
                        } else {
107 13
                            $arrayToReturn[$intLineStr][$key2][$keyA] = $valueA->__toString();
108
                        }
109 13
                    }
110
                }
111
            }
112 4
            foreach ($child->children($this->arrayProcessing['mapping']['cac'], true) as $key2 => $value2) {
113
                $arrayToReturn[$intLineStr][$key2] = $this->getElements($value2);
114 4
            }
115 4
        }
116 4
        return $arrayToReturn;
117 4
    }
118 4
119 4
    private function getProcessingDetails(): void
120 4
    {
121 4
        $this->arrayProcessing = $this->getArrayFromJsonFile(__DIR__
122
            . DIRECTORY_SEPARATOR . 'config', 'ElectronicInvoiceProcessingDetails.json');
123 3
    }
124 3
125 3
    public function getRightMethod(string $existingFunction, $givenParameters = null): array | string
126
    {
127
        try {
128 3
            if (is_array($givenParameters)) {
129
                return call_user_func_array([$this, $existingFunction], [$givenParameters]);
130
            } else {
131
                return call_user_func([$this, $existingFunction], $givenParameters);
132
            }
133 4
            // @codeCoverageIgnoreStart
134 3
        } catch (\Exception $ex) {
135
            echo $ex->getMessage();
136
            return false;
137 4
        }
138
        // @codeCoverageIgnoreEnd
139
    }
140 13
141
    protected function loadSettingsFromFile(): void
142 13
    {
143
        $this->arraySettings = $this->getArrayFromJsonFile(__DIR__
144
            . DIRECTORY_SEPARATOR . 'config', 'ElectronicInvoiceSettings.json');
145 13
    }
146
}
147