kadet1090 /
KeyLighter
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Highlighter |
||
| 4 | * |
||
| 5 | * Copyright (C) 2015, Some right reserved. |
||
| 6 | * |
||
| 7 | * @author Kacper "Kadet" Donat <[email protected]> |
||
| 8 | * |
||
| 9 | * Contact with author: |
||
| 10 | * Xmpp: [email protected] |
||
| 11 | * E-mail: [email protected] |
||
| 12 | * |
||
| 13 | * From Kadet with love. |
||
| 14 | */ |
||
| 15 | |||
| 16 | namespace Kadet\Highlighter; |
||
| 17 | |||
| 18 | use Kadet\Highlighter\Formatter\CliFormatter; |
||
| 19 | use Kadet\Highlighter\Formatter\FormatterInterface; |
||
| 20 | use Kadet\Highlighter\Formatter\HtmlFormatter; |
||
| 21 | use Kadet\Highlighter\Language\Language; |
||
| 22 | use Kadet\Highlighter\Utils\Singleton; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * KeyLighter helper class, used to simplify usage. |
||
| 26 | * |
||
| 27 | * @package Kadet\Highlighter |
||
| 28 | */ |
||
| 29 | class KeyLighter |
||
| 30 | { |
||
| 31 | use Singleton; |
||
| 32 | |||
| 33 | const VERSION = '0.2.0'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Registered aliases |
||
| 37 | * |
||
| 38 | * @var string[] |
||
| 39 | */ |
||
| 40 | private $_languages = []; |
||
| 41 | |||
| 42 | /** @var FormatterInterface */ |
||
| 43 | private $_formatter = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param string $name |
||
| 47 | * |
||
| 48 | * @return Language |
||
| 49 | */ |
||
| 50 | 3 | public function getLanguage($name) { |
|
| 51 | 3 | $embedded = []; |
|
| 52 | 3 | if(($pos = strpos($name, '>')) !== false) { |
|
| 53 | 1 | $embedded[] = self::getLanguage(trim(substr($name, $pos + 1))); |
|
| 54 | 1 | $name = trim(substr($name, 0, $pos)); |
|
| 55 | 1 | } |
|
| 56 | |||
| 57 | 3 | $lang = isset($this->_languages[$name]) ? $this->_languages[$name] : 'Kadet\\Highlighter\\Language\\PlainText'; |
|
| 58 | 3 | return new $lang([ |
|
| 59 | 'embedded' => $embedded |
||
| 60 | 3 | ]); |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param Language|callable|string $language |
||
| 65 | * @param array[string] $aliases |
||
|
1 ignored issue
–
show
|
|||
| 66 | */ |
||
| 67 | 5 | public function registerLanguage($language, $aliases) { |
|
| 68 | 5 | $this->_languages = array_merge($this->_languages, array_fill_keys($aliases, $language)); |
|
|
1 ignored issue
–
show
It seems like
array_merge($this->_lang...s($aliases, $language)) of type array is incompatible with the declared type array<integer,string> of property $_languages.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 69 | 5 | } |
|
| 70 | |||
| 71 | 5 | public function setDefaultFormatter(FormatterInterface $formatter) { |
|
| 72 | 5 | $this->_formatter = $formatter; |
|
| 73 | 5 | } |
|
| 74 | |||
| 75 | 1 | public function registeredLanguages() { |
|
| 76 | 1 | return $this->_languages; |
|
| 77 | } |
||
| 78 | |||
| 79 | public function getDefaultFormatter() { |
||
| 80 | return $this->_formatter; |
||
| 81 | } |
||
| 82 | |||
| 83 | 4 | public function highlight($source, $language, FormatterInterface $formatter = null) { |
|
| 84 | 4 | $formatter = $formatter ?: $this->getDefaultFormatter(); |
|
| 85 | |||
| 86 | 4 | if(!$language instanceof Language) { |
|
| 87 | 1 | $language = $this->getLanguage($language); |
|
| 88 | 1 | } |
|
| 89 | |||
| 90 | 4 | return $formatter->format($language->parse($source)); |
|
| 91 | } |
||
| 92 | |||
| 93 | 5 | public function __construct() { |
|
| 94 | 5 | $this->setDefaultFormatter( |
|
| 95 | 5 | php_sapi_name() === 'cli' ? |
|
| 96 | 5 | new CliFormatter() : |
|
| 97 | new HtmlFormatter() |
||
| 98 | 5 | ); |
|
| 99 | |||
| 100 | 5 | $this->registerLanguage('Kadet\\Highlighter\\Language\\PhpLanguage', ['php']); |
|
| 101 | 5 | $this->registerLanguage('Kadet\\Highlighter\\Language\\XmlLanguage', ['xml', 'xaml']); |
|
| 102 | 5 | $this->registerLanguage('Kadet\\Highlighter\\Language\\HtmlLanguage', ['html', 'htm']); |
|
| 103 | 5 | $this->registerLanguage('Kadet\\Highlighter\\Language\\PowerShellLanguage', ['powershell', 'posh', 'ps1']); |
|
| 104 | 5 | $this->registerLanguage('Kadet\\Highlighter\\Language\\PlainText', ['plaintext', 'text', 'none', 'txt']); |
|
| 105 | 5 | $this->registerLanguage('Kadet\\Highlighter\\Language\\LatexLanguage', ['tex', 'latex']); |
|
| 106 | 5 | $this->registerLanguage('Kadet\\Highlighter\\Language\\IniLanguage', ['ini', 'cfg']); |
|
| 107 | 5 | $this->registerLanguage('Kadet\\Highlighter\\Language\\JavaScriptLanguage', ['js', 'jscript', 'javascript']); |
|
| 108 | 5 | $this->registerLanguage('Kadet\\Highlighter\\Language\\CssLanguage', ['css']); |
|
| 109 | } |
||
| 110 | } |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.