Issues (14)

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/Service/DomHelper.php (2 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
declare(strict_types=1);
4
5
namespace Paysera\PhpStormHelper\Service;
6
7
use DOMDocument;
8
use DOMElement;
9
use DOMNode;
10
use RuntimeException;
11
12
class DomHelper
13
{
14 24 View Code Duplication
    public function loadDocument(string $pathToXml): DOMDocument
0 ignored issues
show
This method seems to be duplicated in 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...
15
    {
16 24
        if (!file_exists($pathToXml)) {
17
            throw new RuntimeException(sprintf('File %s does not exist', $pathToXml));
18
        }
19
20 24
        $document = $this->createEmptyDomDocument();
21 24
        $this->loadXmlContents($document, $pathToXml);
22 24
        return $document;
23
    }
24
25 6 View Code Duplication
    public function loadOrCreateDocument(string $pathToXml): DOMDocument
0 ignored issues
show
This method seems to be duplicated in 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...
26
    {
27 6
        $document = $this->createEmptyDomDocument();
28
29 6
        if (!file_exists($pathToXml)) {
30 3
            return $document;
31
        }
32
33 3
        $this->loadXmlContents($document, $pathToXml);
34
35 3
        return $document;
36
    }
37
38 30
    private function createEmptyDomDocument(): DOMDocument
39
    {
40 30
        $document = new DOMDocument();
41 30
        $document->preserveWhiteSpace = true;
42 30
        $document->formatOutput = true;
43 30
        return $document;
44
    }
45
46 27
    private function loadXmlContents(DOMDocument $document, string $pathToXml)
47
    {
48 27
        libxml_clear_errors();
49 27
        if (!$document->loadXML(file_get_contents($pathToXml))) {
50
            $error = libxml_get_last_error();
51
            $message = $error === false ? 'Cannot load XML file' : $error->message;
52
            throw new RuntimeException($message);
53
        }
54 27
    }
55
56 30
    public function saveDocument(string $path, DOMDocument $document)
57
    {
58 30
        file_put_contents($path, $document->saveXML());
59 30
    }
60
61 24
    public function findNode(DOMNode $node, string $tagName, array $attributes = null): DOMNode
62
    {
63 24
        $node = $this->findOptionalNode($node, $tagName, $attributes);
64 24
        if ($node === null) {
65
            throw new RuntimeException(sprintf('Node <%s> not found', $tagName));
66
        }
67
68 24
        return $node;
69
    }
70
71 30
    public function findOptionalNode(DOMNode $node, string $tagName, array $attributes = null)
72
    {
73 30
        $nodes = $this->findNodes($node, $tagName, $attributes);
74 30
        if (count($nodes) > 1) {
75
            throw new RuntimeException(sprintf('Expected only single <%s> tag', $tagName));
76 30
        } elseif (count($nodes) === 0) {
77 26
            return null;
78
        }
79
80 27
        return $nodes[0];
81
    }
82
83 30
    public function findOrCreateChildNode(DOMNode $node, string $tagName, array $attributes = null): DOMElement
84
    {
85 30
        $internalNode = $this->findOptionalNode($node, $tagName, $attributes);
86 30
        if ($internalNode === null) {
87 26
            $ownerDocument = $node instanceof DOMDocument ? $node : $node->ownerDocument;
88 26
            $internalNode = $ownerDocument->createElement($tagName);
89 26
            $this->applyAttributesToElement($internalNode, $attributes ?? []);
90 26
            $node->appendChild($internalNode);
91
        }
92 30
        return $internalNode;
93
    }
94
95 28
    public function applyAttributesToElement(DOMElement $node, array $attributes)
96
    {
97 28
        foreach ($attributes as $name => $value) {
98 15
            $node->setAttribute($name, $value);
99
        }
100 28
    }
101
102
    /**
103
     * @param DOMNode $node
104
     * @param string $tagName
105
     * @param array|null $attributes
106
     * @return array|DomNode[]
107
     */
108 30
    public function findNodes(DOMNode $node, string $tagName, array $attributes = null): array
109
    {
110 30
        $result = [];
111
112
        /** @var DOMNode $childNode */
113 30
        foreach ($node->childNodes as $childNode) {
114 29
            if ($childNode->nodeName === $tagName && $this->matchesAttributes($childNode, $attributes)) {
115 29
                $result[] = $childNode;
116
            }
117
        }
118
119 30
        return $result;
120
    }
121
122 29
    private function matchesAttributes(DOMNode $childNode, array $attributes = null): bool
123
    {
124 29
        if ($attributes === null) {
125 26
            return true;
126
        }
127
128 29
        foreach ($attributes as $key => $value) {
129 29
            $attributeNode = $childNode->attributes->getNamedItem($key);
130 29
            $attributeValue = $attributeNode !== null ? $attributeNode->nodeValue : null;
131 29
            if ($attributeValue !== $value) {
132 29
                return false;
133
            }
134
        }
135
136 18
        return true;
137
    }
138
139 11
    public function removeNodes(DOMElement $parentNode, string $tagName, array $attributes = null)
140
    {
141 11
        $mappings = $this->findNodes($parentNode, $tagName, $attributes);
142 11
        foreach ($mappings as $mapping) {
143 3
            $parentNode->removeChild($mapping);
144
        }
145 11
    }
146
}
147