diff --git a/src/Psalm/CodeLocation.php b/src/Psalm/CodeLocation.php index 588a83230e5..15ed58cea67 100644 --- a/src/Psalm/CodeLocation.php +++ b/src/Psalm/CodeLocation.php @@ -11,11 +11,11 @@ use function preg_replace; use Psalm\Internal\Analyzer\CommentAnalyzer; use function str_replace; -use function strlen; -use function strpos; -use function strrpos; -use function substr; -use function substr_count; +use function mb_strlen; +use function mb_strpos; +use function mb_strrpos; +use function mb_substr; +use function mb_substr_count; use function trim; class CodeLocation @@ -156,12 +156,12 @@ private function calculateRealLocation(): void $file_contents = $codebase->getFileContents($this->file_path); - $file_length = strlen($file_contents); + $file_length = mb_strlen($file_contents); $search_limit = $this->single_line ? $this->selection_start : $this->selection_end; if ($search_limit <= $file_length) { - $preview_end = strpos( + $preview_end = mb_strpos( $file_contents, "\n", $search_limit @@ -183,7 +183,7 @@ private function calculateRealLocation(): void ) { $preview_lines = explode( "\n", - substr( + mb_substr( $file_contents, $this->preview_start, $this->selection_start - $this->preview_start - 1 @@ -195,7 +195,7 @@ private function calculateRealLocation(): void $comment_line_offset = $this->docblock_line_number - $this->docblock_start_line_number; for ($i = 0; $i < $comment_line_offset; ++$i) { - $preview_offset += strlen($preview_lines[$i]) + 1; + $preview_offset += mb_strlen($preview_lines[$i]) + 1; } if (!isset($preview_lines[$i])) { @@ -206,10 +206,10 @@ private function calculateRealLocation(): void $indentation = (int)strpos($key_line, '@'); - $key_line = trim(preg_replace('@\**/\s*@', '', substr($key_line, $indentation))); + $key_line = trim(preg_replace('@\**/\s*@', '', mb_substr($key_line, $indentation))); $this->selection_start = $preview_offset + $indentation + $this->preview_start; - $this->selection_end = $this->selection_start + strlen($key_line); + $this->selection_end = $this->selection_start + mb_strlen($key_line); } if ($this->regex_type !== null) { @@ -258,7 +258,7 @@ private function calculateRealLocation(): void throw new \UnexpectedValueException('Unrecognised regex type ' . $this->regex_type); } - $preview_snippet = substr( + $preview_snippet = mb_substr( $file_contents, $this->selection_start, $this->selection_end - $this->selection_start @@ -271,7 +271,7 @@ private function calculateRealLocation(): void if (preg_match($regex, $preview_snippet, $matches, PREG_OFFSET_CAPTURE)) { $this->selection_start = $this->selection_start + (int)$matches[$match_offset][1]; - $this->selection_end = $this->selection_start + strlen((string)$matches[$match_offset][0]); + $this->selection_end = $this->selection_start + mb_strlen((string)$matches[$match_offset][0]); } } @@ -279,7 +279,7 @@ private function calculateRealLocation(): void $this->preview_start = (int)strrpos( $file_contents, "\n", - min($this->preview_start, $this->selection_start) - strlen($file_contents) + min($this->preview_start, $this->selection_start) - mb_strlen($file_contents) ) + 1; $this->selection_start = max($this->preview_start, $this->selection_start); @@ -289,7 +289,7 @@ private function calculateRealLocation(): void $this->preview_end = (int)strrpos( $file_contents, "\n", - $this->selection_end + 200 - strlen($file_contents) + $this->selection_end + 200 - mb_strlen($file_contents) ); // if the line is over 200 characters long @@ -298,20 +298,20 @@ private function calculateRealLocation(): void } } - $this->snippet = substr($file_contents, $this->preview_start, $this->preview_end - $this->preview_start); - $this->text = substr($file_contents, $this->selection_start, $this->selection_end - $this->selection_start); + $this->snippet = mb_substr($file_contents, $this->preview_start, $this->preview_end - $this->preview_start); + $this->text = mb_substr($file_contents, $this->selection_start, $this->selection_end - $this->selection_start); // reset preview start to beginning of line $this->column_from = $this->selection_start - - (int)strrpos($file_contents, "\n", $this->selection_start - strlen($file_contents)); + (int)strrpos($file_contents, "\n", $this->selection_start - mb_strlen($file_contents)); - $newlines = substr_count($this->text, "\n"); + $newlines = mb_substr_count($this->text, "\n"); if ($newlines) { - $last_newline_pos = strrpos($file_contents, "\n", $this->selection_end - strlen($file_contents) - 1); + $last_newline_pos = mb_strrpos($file_contents, "\n", $this->selection_end - mb_strlen($file_contents) - 1); $this->column_to = $this->selection_end - (int)$last_newline_pos; } else { - $this->column_to = $this->column_from + strlen($this->text); + $this->column_to = $this->column_from + mb_strlen($this->text); } $this->end_line_number = $this->getLineNumber() + $newlines;