Issues (3)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/XdtParser.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Druc\XdtParser;
4
5
class XdtParser
0 ignored issues
show
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
6
{
7
    /**
8
     * Array to hold mappings to different xdt keys
9
     * Eg: ['first_name' => '2031', 'last_name' => '2031'];
10
     * @var array
11
     */
12
    private $fieldsMap;
13
14
    /**
15
     * Holds the content unparsed rows
16
     * @var array
17
     */
18
    private $xdtRows = [];
19
20
    /** @var array */
21
    private $parsedRows = [];
22
23
    /**
24
     * @param string $content
25
     * @param array $fieldsMap
26
     * @return XdtParser
27
     */
28 45
    public static function make(string $content, array $fieldsMap = [])
29
    {
30 45
        return new static($content, $fieldsMap);
31
    }
32
33
    /**
34
     * XdtParser constructor.
35
     * @param string $content
36
     * @param array $fieldsMap
37
     */
38 45
    private function __construct(string $content, array $fieldsMap = [])
39
    {
40 45
        $this->fieldsMap = $fieldsMap;
41 45
        $this->xdtRows = explode(PHP_EOL, $content);
42 45
        $this->parseXdtRows();
43 45
    }
44
45 45
    private function parseXdtRows()
46
    {
47 45
        foreach ($this->xdtRows as $row) {
48 45
            if ($row === '') {
49 3
                continue;
50
            }
51 45
            $this->parsedRows[] = $this->parseSingle($row);
52
        }
53 45
    }
54
55
    /**
56
     * @param string $string
57
     * @return array
58
     */
59 45
    public function parseSingle(string $string)
60
    {
61 45
        $matched = preg_match('/^\\r?\\n?(\\d{3})(\\d{4})(.*?)\\r?\\n?$/', $string, $matches);
62
63 45
        if (!$matched) {
64 3
            throw new CorruptedXdt;
65
        }
66
67
        return [
68 45
            'length' => $matches[1] ? intval($matches[1]) : null,
69 45
            'key' => $matches[2] ?? null,
70 45
            'value' => $matches[3] ?? null
71
        ];
72
    }
73
74
    /**
75
     * @param string $field
76
     * @return null
77
     */
78 9
    public function first(string $field)
79
    {
80 9 View Code Duplication
        foreach ($this->parsedRows as $row) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81 9
            if ($row['key'] === $this->getKey($field)) {
82 9
                return $row['value'];
83
            }
84
        }
85
86
        return null;
87
    }
88
89
    /**
90
     * @param string $field
91
     * @return array|mixed|null
92
     */
93 21
    public function find(string $field)
94
    {
95 21
        $result = [];
96
97 21 View Code Duplication
        foreach ($this->parsedRows as $row) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98 21
            if ($row['key'] === $this->getKey($field)) {
99 21
                $result[] = $row['value'];
100
            }
101
        }
102
103 21
        switch (count($result)) {
104 21
            case 0:
105 3
                return null;
106 21
            case 1:
107 12
                return $result[0];
108
            default:
109 21
                return $result;
110
        }
111
    }
112
113
    /**
114
     * @param string $field
115
     * @return string
116
     */
117 33
    public function getKey(string $field)
118
    {
119 33
        return $this->fieldsMap[$field] ?? $field;
120
    }
121
122
    /**
123
     * @param string $key
124
     * @return string
125
     */
126 3
    public function getFieldName(string $key)
127
    {
128 3
        foreach ($this->fieldsMap as $field => $k) {
129 3
            if ($k === $key) {
130 3
                return $field;
131
            }
132
        }
133
        return $key;
134
    }
135
136
    /**
137
     * @return array
138
     */
139 12
    public function getMapped()
140
    {
141 12
        $result = [];
142
143 12
        foreach ($this->fieldsMap as $field => $key) {
144 12
            $result[$field] = $this->find($field);
145
        }
146
147 12
        return $result;
148
    }
149
150
    /**
151
     * @return array
152
     */
153 3
    public function all()
154
    {
155 3
        $result = [];
156
157 3
        foreach ($this->parsedRows as $row) {
158 3
            $field = array_search($row['key'], $this->fieldsMap) ?: $row['key'];
159 3
            $result[$field] = $this->find($field);
160
        }
161
162 3
        return $result;
163
    }
164
165
    /**
166
     * @param array $fieldsMap
167
     * @return XdtParser
168
     */
169 3
    public function setFieldsMap(array $fieldsMap)
170
    {
171 3
        $this->fieldsMap = $fieldsMap;
172 3
        return $this;
173
    }
174
175
    /**
176
     * @return array
177
     */
178
    public function getFieldsMap()
179
    {
180
        return $this->fieldsMap;
181
    }
182
183
    /**
184
     * @param array $fields
185
     */
186 3
    public function addFieldsMap(array $fields)
187
    {
188 3
        foreach ($fields as $field => $key) {
189 3
            $this->fieldsMap[$field] = $key;
190
        }
191 3
    }
192
193
    /**
194
     * @param array $fields
195
     */
196 3
    public function removeFields(array $fields)
197
    {
198 3
        foreach ($fields as $field) {
199 3
            unset($this->fieldsMap[$field]);
200
        }
201 3
    }
202
203
    /**
204
     * @return array
205
     */
206 3
    public function getXdtRows(): array
207
    {
208 3
        return $this->xdtRows;
209
    }
210
}
211