1 | <?php |
||
32 | final class Utils |
||
33 | { |
||
34 | /** |
||
35 | * Get the content of given file and returns the results. |
||
36 | * |
||
37 | * @param string $file The path to the file |
||
38 | * |
||
39 | * @return mixed The results of the include |
||
40 | * @since 0.2.4 |
||
41 | * @codeCoverageIgnore |
||
42 | */ |
||
43 | public static function getContent($file = null) |
||
44 | { |
||
45 | return file_get_contents(realpath($file)); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Get the given file and returns the results. |
||
50 | * |
||
51 | * @param string $path The path to the file |
||
52 | * |
||
53 | * @return mixed The results of the include |
||
54 | * @since 0.2.4 |
||
55 | * @codeCoverageIgnore |
||
56 | */ |
||
57 | public static function getFile($path = null) |
||
58 | { |
||
59 | return file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Takes a value and checks if it's a Closure or not, if it's a Closure it |
||
64 | * will return the result of the closure, if not, it will simply return the |
||
65 | * value. |
||
66 | * |
||
67 | * @param mixed $var The value to get |
||
68 | * |
||
69 | * @return mixed |
||
70 | * @since 0.1.0 |
||
71 | * @codeCoverageIgnore |
||
72 | */ |
||
73 | public static function checkValue($var = null) |
||
74 | { |
||
75 | return ($var instanceof Closure) ? $var() : $var; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Trim array elements. |
||
80 | * |
||
81 | * @param array $content The array to trim |
||
82 | * |
||
83 | * @return mixed |
||
84 | * @since 0.2.4 |
||
85 | * @codeCoverageIgnore |
||
86 | */ |
||
87 | public static function trimArrayElements($content = null) |
||
88 | { |
||
89 | $cb = function ($el) { |
||
90 | return trim($el); |
||
91 | }; |
||
92 | |||
93 | $content = array_map($cb, $content); |
||
94 | |||
95 | return $content; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Remove quotes. |
||
100 | * |
||
101 | * @param string $string The string to remove quotes from |
||
102 | * |
||
103 | * @return string |
||
104 | * @since 0.2.4 |
||
105 | * @codeCoverageIgnore |
||
106 | */ |
||
107 | public static function removeQuotes($string = null) |
||
108 | { |
||
109 | if (substr($string, -1) === '"' && substr($string, 0, 1) === '"') { |
||
110 | $string = substr($string, 1, -1); |
||
111 | } |
||
112 | |||
113 | return $string; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Strip Backslashes from given string. |
||
118 | * |
||
119 | * @param array|null $content String |
||
120 | * |
||
121 | * @return array |
||
122 | * @since 0.2.4 |
||
123 | * @codeCoverageIgnore |
||
124 | */ |
||
125 | public static function stripBackslashes($content = null) |
||
126 | { |
||
127 | foreach ($content as $lineNb => $line) { |
||
|
|||
128 | if (substr($line[2], -1) === '\\') { |
||
129 | $content[$lineNb][2] = trim(substr($line[2], 0, -1)); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | return $content; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Checks if the string starts with the given needle. |
||
138 | * |
||
139 | * @param string $needle Search string |
||
140 | * @param string $string String to search in |
||
141 | * |
||
142 | * @return bool |
||
143 | * @since 0.2.4 |
||
144 | * @codeCoverageIgnore |
||
145 | */ |
||
146 | public static function stringStart($needle, $string) |
||
150 | |||
151 | /** |
||
152 | * Opens given file and convert it to an array. |
||
153 | * |
||
154 | * @param string $path The path to the file |
||
155 | * |
||
156 | * @return array |
||
157 | * @since 0.2.4 |
||
158 | * @codeCoverageIgnore |
||
159 | */ |
||
160 | public static function fileToArray($path = null) |
||
174 | |||
175 | /** |
||
176 | * Opens given file and convert it to an array. |
||
177 | * |
||
178 | * @param string $content The file content |
||
179 | * |
||
180 | * @return string |
||
181 | * @since 0.2.4 |
||
182 | * @codeCoverageIgnore |
||
183 | */ |
||
184 | public static function fileContentToArray($content = null) |
||
201 | |||
202 | /** |
||
203 | * Get lines matching number. |
||
204 | * |
||
205 | * @param string $type The line type |
||
206 | * @param array $analysis Array to analyze |
||
207 | * |
||
208 | * @return int |
||
209 | * @since 0.2.4 |
||
210 | * @codeCoverageIgnore |
||
211 | */ |
||
212 | public static function getNumberLinesMatching($type, array $analysis) |
||
224 | |||
225 | /** |
||
226 | * __toString. |
||
227 | * |
||
228 | * @return string |
||
229 | * @since 0.1.2 |
||
230 | * @codeCoverageIgnore |
||
231 | */ |
||
232 | public function __toString() |
||
236 | } |
||
237 | |||
239 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.