This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
|||||||||||
2 | class Nip_Helper_Strings extends Nip\Helpers\AbstractHelper { |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
3 | ||||||||||||
4 | /** |
|||||||||||
5 | * Limits a string to a certain number of words |
|||||||||||
6 | * |
|||||||||||
7 | * @param string $string |
|||||||||||
8 | * @param int $limit |
|||||||||||
9 | * @param string $end |
|||||||||||
10 | * @return string |
|||||||||||
11 | */ |
|||||||||||
12 | public function limitWords($string, $limit = false, $end = '...') { |
|||||||||||
13 | $words = explode(" ", $string); |
|||||||||||
14 | ||||||||||||
15 | if (count($words) <= $limit) { |
|||||||||||
16 | return $string; |
|||||||||||
17 | } |
|||||||||||
18 | ||||||||||||
19 | $return = []; |
|||||||||||
20 | for ($i = 0; $i < $limit; $i++) { |
|||||||||||
21 | $return[] = $words[$i]; |
|||||||||||
22 | } |
|||||||||||
23 | ||||||||||||
24 | $return[] = $end; |
|||||||||||
25 | ||||||||||||
26 | return implode(" ", $return); |
|||||||||||
27 | } |
|||||||||||
28 | ||||||||||||
29 | ||||||||||||
30 | /** |
|||||||||||
31 | * Injects GET params in links |
|||||||||||
32 | * |
|||||||||||
33 | * @param string $string |
|||||||||||
34 | * @param array $params |
|||||||||||
35 | * @return string |
|||||||||||
36 | */ |
|||||||||||
37 | public function injectParams($string, $params = array()) { |
|||||||||||
38 | $links = preg_split('#(<a\b[^>]+>)#', $string, -1, PREG_SPLIT_DELIM_CAPTURE); |
|||||||||||
39 | $old = $links; |
|||||||||||
40 | ||||||||||||
41 | foreach ($links as &$match) { |
|||||||||||
42 | if (preg_match('/<a\b/', $match) && !preg_match('/(?:#|mailto)/', $match)) { |
|||||||||||
43 | preg_match('/^([^"]+")([^"]+)/', $match, $matches); |
|||||||||||
44 | if ($matches) { |
|||||||||||
0 ignored issues
–
show
The expression
$matches of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
||||||||||||
45 | $link = html_entity_decode($matches[2]); |
|||||||||||
46 | if (strpos($link, "?") === false) { |
|||||||||||
47 | $link .= "?"; |
|||||||||||
48 | } else { |
|||||||||||
49 | $link .= "&"; |
|||||||||||
50 | } |
|||||||||||
51 | ||||||||||||
52 | $link .= http_build_query($params); |
|||||||||||
53 | ||||||||||||
54 | $match = str_replace($matches[2], $link, $match); |
|||||||||||
55 | } |
|||||||||||
56 | } |
|||||||||||
57 | } |
|||||||||||
58 | ||||||||||||
59 | $string = str_replace($old, $links, $string); |
|||||||||||
60 | return $string; |
|||||||||||
61 | } |
|||||||||||
62 | ||||||||||||
63 | ||||||||||||
64 | /** |
|||||||||||
65 | * Converts all relative hrefs and image srcs to absolute |
|||||||||||
66 | * |
|||||||||||
67 | * @param string $string |
|||||||||||
68 | * @param string $base |
|||||||||||
69 | * @return string |
|||||||||||
70 | */ |
|||||||||||
71 | public function relativeToAbsolute($string, $base) { |
|||||||||||
72 | $matches = preg_split('#(<(a|img)\b[^>]+>)#', $string, -1, PREG_SPLIT_DELIM_CAPTURE); |
|||||||||||
73 | $old = $matches; |
|||||||||||
74 | ||||||||||||
75 | foreach ($matches as &$match) { |
|||||||||||
76 | if (preg_match('/<(a|img)\b/', $match) && !preg_match('/(?:http|#|mailto)/', $match)) { |
|||||||||||
77 | $match = preg_replace('/^([^"]+")([^"]+)/', '$1'.$base.'$2', $match); |
|||||||||||
78 | } |
|||||||||||
79 | } |
|||||||||||
80 | ||||||||||||
81 | $string = str_replace($old, $matches, $string); |
|||||||||||
82 | return $string; |
|||||||||||
83 | } |
|||||||||||
84 | ||||||||||||
85 | public function moneyFormat($number) |
|||||||||||
86 | { |
|||||||||||
87 | return money_format('%n', $number); |
|||||||||||
88 | } |
|||||||||||
89 | ||||||||||||
90 | public function cronoTimeInSeconds($time) |
|||||||||||
91 | { |
|||||||||||
92 | $parts = explode(':', $time); |
|||||||||||
93 | $seconds = array_pop($parts); |
|||||||||||
94 | $minutes = array_pop($parts); |
|||||||||||
95 | $hours = array_pop($parts); |
|||||||||||
96 | $days = array_pop($parts); |
|||||||||||
97 | ||||||||||||
98 | return (($days*24 + $hours)*60 + $minutes)*60 + $seconds; |
|||||||||||
99 | } |
|||||||||||
100 | ||||||||||||
101 | public function secondsInCronoTime($seconds) |
|||||||||||
102 | { |
|||||||||||
103 | View Code Duplication | if ($days = intval((floor($seconds / 86400)))) { |
||||||||||
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. ![]() |
||||||||||||
104 | $seconds = $seconds - $days*86400; |
|||||||||||
105 | $return .= ($return ? ':' : '') . str_pad($days, 2, 0,STR_PAD_LEFT); |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
106 | } |
|||||||||||
107 | View Code Duplication | if ($hours = intval((floor($seconds / 3600))) OR $return) { |
||||||||||
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. ![]() Comprehensibility
Best Practice
introduced
by
Using logical operators such as
or instead of || is generally not recommended.
PHP has two types of connecting operators (logical operators, and boolean operators):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like Let’s take a look at a few examples: // Logical operators have lower precedence:
$f = false or true;
// is executed like this:
($f = false) or true;
// Boolean operators have higher precedence:
$f = false || true;
// is executed like this:
$f = (false || true);
Logical Operators are used for Control-FlowOne case where you explicitly want to use logical operators is for control-flow such as this: $x === 5
or die('$x must be 5.');
// Instead of
if ($x !== 5) {
die('$x must be 5.');
}
Since // The following is currently a parse error.
$x === 5
or throw new RuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current PHP code. ![]() |
||||||||||||
108 | $seconds = $seconds - $hours*3600; |
|||||||||||
109 | $return .= ($return ? ':' : '') . str_pad($hours, 2, 0,STR_PAD_LEFT); |
|||||||||||
110 | } |
|||||||||||
111 | View Code Duplication | if ($minutes = intval((floor($seconds / 60))) OR $return) { |
||||||||||
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. ![]() Comprehensibility
Best Practice
introduced
by
Using logical operators such as
or instead of || is generally not recommended.
PHP has two types of connecting operators (logical operators, and boolean operators):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like Let’s take a look at a few examples: // Logical operators have lower precedence:
$f = false or true;
// is executed like this:
($f = false) or true;
// Boolean operators have higher precedence:
$f = false || true;
// is executed like this:
$f = (false || true);
Logical Operators are used for Control-FlowOne case where you explicitly want to use logical operators is for control-flow such as this: $x === 5
or die('$x must be 5.');
// Instead of
if ($x !== 5) {
die('$x must be 5.');
}
Since // The following is currently a parse error.
$x === 5
or throw new RuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current PHP code. ![]() |
||||||||||||
112 | $seconds = $seconds - $minutes*60; |
|||||||||||
113 | $return .= ($return ? ':' : '') . str_pad($minutes, 2, 0, STR_PAD_LEFT); |
|||||||||||
114 | } |
|||||||||||
115 | $seconds = round($seconds, 2); |
|||||||||||
116 | $return .= ($return ? ':' : '') . str_pad($seconds, 2, 0, STR_PAD_LEFT); |
|||||||||||
117 | ||||||||||||
118 | return $return; |
|||||||||||
119 | } |
|||||||||||
120 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.