Issues (5)

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/Botonomous/utility/StringUtility.php (2 issues)

Severity

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 Botonomous\utility;
4
5
use Botonomous\BotonomousException;
6
7
/**
8
 * Class StringUtility.
9
 */
10
class StringUtility extends AbstractUtility
11
{
12
    /**
13
     * @param string $json
14
     *
15
     * @throws \Exception
16
     *
17
     * @return array|mixed
18
     */
19 22
    public function jsonToArray(string $json)
20
    {
21 22
        $array = empty($json) ? [] : json_decode($json, true);
22 22
        if ($array === null || !is_array($array) || json_last_error() !== 0) {
23 1
            throw new BotonomousException('Invalid JSON content');
24
        }
25
26 22
        return $array;
27
    }
28
29
    /**
30
     * @param string $toRemove
31
     * @param string $subject
32
     *
33
     * @return string
34
     */
35 1
    public function removeStringFromString(string $toRemove, string $subject): string
36
    {
37
        // pattern: !\s+! is used to replace multiple spaces with single space
38 1
        return trim(preg_replace('!\s+!', ' ', str_replace($toRemove, '', $subject)));
39
    }
40
41
    /**
42
     * @param string $toFind
43
     * @param string $wordBoundary
44
     *
45
     * @return string
46
     */
47 11
    private function getFindPattern(string $toFind, string $wordBoundary): string
48
    {
49 11
        return $wordBoundary === true ? "/\b{$toFind}\b/" : "/{$toFind}/";
50
    }
51
52
    /**
53
     * @param string $toFind
54
     * @param string $subject
55
     * @param bool   $wordBoundary If true $toFind is searched with word boundaries
56
     *
57
     * @return bool
58
     */
59 5
    public function findInString(string $toFind, string $subject, bool $wordBoundary = true): bool
60
    {
61 5
        $pattern = $this->getFindPattern($toFind, $wordBoundary);
0 ignored issues
show
$wordBoundary is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
63 5
        return preg_match($pattern, $subject) ? true : false;
64
    }
65
66
    /**
67
     * @param string $toFind
68
     * @param string $subject
69
     * @param bool   $wordBoundary
70
     *
71
     * @return mixed
72
     */
73 6
    public function findPositionInString(string $toFind, string $subject, bool $wordBoundary = true)
74
    {
75 6
        $pattern = $this->getFindPattern($toFind, $wordBoundary);
0 ignored issues
show
$wordBoundary is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76 6
        $positions = [];
77 6
        if (preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE) && !empty($matches[0])) {
78 4
            foreach ($matches[0] as $match) {
79 4
                $positions[] = $match[1];
80
            }
81
        }
82
83 6
        return $positions;
84
    }
85
86
    /**
87
     * Convert snake case to camel case e.g. admin_user becomes AdminUser.
88
     *
89
     * @param string $string
90
     *
91
     * @return string
92
     */
93 20
    public function snakeCaseToCamelCase(string $string)
94
    {
95 20
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
96
    }
97
98
    /**
99
     * Check subject to see whether $string1 is followed by $string2.
100
     *
101
     * @param string $subject
102
     * @param string $string1
103
     * @param string $string2
104
     * @param array  $exceptions
105
     *
106
     * @return bool
107
     */
108 1
    public function isString1FollowedByString2(
109
        string $subject,
110
        string $string1,
111
        string $string2,
112
        array $exceptions = []
113
    ): bool {
114 1
        $exceptionsString = '';
115 1
        if (!empty($exceptions)) {
116 1
            $exceptions = implode('|', $exceptions);
117 1
            $exceptionsString = "(?<!{$exceptions})";
118
        }
119
120 1
        $pattern = '/'.$string1.'(?:\s+\w+'.$exceptionsString.'){0,2}\s+'.$string2.'\b/';
121
122 1
        return preg_match($pattern, $subject) ? true : false;
123
    }
124
125
    /**
126
     * @param string $haystack
127
     * @param string $needle
128
     *
129
     * @return bool
130
     */
131 13
    public function endsWith(string $haystack, string $needle): bool
132
    {
133 13
        $length = strlen($needle);
134
135 13
        if ($length === 0) {
136 1
            return true;
137
        }
138
139 13
        return substr($haystack, -$length) === $needle;
140
    }
141
142
    /**
143
     * Apply replacements in a string
144
     * Replacement key in the string should be like {replacementKey}.
145
     *
146
     * @param $subject mixed
147
     * @param $replacements array
148
     *
149
     * @return mixed
150
     */
151 109
    public function applyReplacements($subject, array $replacements)
152
    {
153 109
        if (empty($replacements) || !is_string($subject)) {
154 107
            return $subject;
155
        }
156
157 5
        foreach ($replacements as $key => $value) {
158 5
            $subject = str_replace('{'.$key.'}', $value, $subject);
159
        }
160
161 5
        return $subject;
162
    }
163
}
164