| Total Complexity | 12 |
| Total Lines | 81 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 17 | class Highlighter |
||
| 18 | { |
||
| 19 | /** @var StyleInterface */ |
||
| 20 | private $r = null; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param StyleInterface $renderer |
||
| 24 | */ |
||
| 25 | public function __construct(StyleInterface $renderer) |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Highlight PHP source and return N lines around target line. |
||
| 32 | * |
||
| 33 | * @param string $source |
||
| 34 | * @param int $line |
||
| 35 | * @param int $around |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | public function highlightLines(string $source, int $line, int $around = 5): string |
||
| 39 | { |
||
| 40 | $lines = explode("\n", str_replace("\r\n", "\n", $this->highlight($source))); |
||
| 41 | |||
| 42 | $result = ''; |
||
| 43 | foreach ($lines as $number => $code) { |
||
| 44 | $human = $number + 1; |
||
| 45 | if (!empty($around) && ($human < $line - $around || $human >= $line + $around + 1)) { |
||
| 46 | //Not included in a range |
||
| 47 | continue; |
||
| 48 | } |
||
| 49 | |||
| 50 | $result .= $this->r->line($human, mb_convert_encoding($code, 'utf-8'), $human === $line); |
||
| 51 | } |
||
| 52 | |||
| 53 | return $result; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns highlighted PHP source. |
||
| 58 | * |
||
| 59 | * @param string $source |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | public function highlight(string $source): string |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get all tokens from PHP source normalized to always include line number. |
||
| 76 | * |
||
| 77 | * @param string $source |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | private function getTokens(string $source): array |
||
| 98 | } |
||
| 99 | } |
||
| 100 |