@@ -54,8 +54,8 @@ |
||
| 54 | 54 | $split = explode("\r\n\r\n", $source); |
| 55 | 55 | |
| 56 | 56 | $http = $split[0]; |
| 57 | - if(isset($split[1]) && $payload = $split[1]) { |
|
| 58 | - if(preg_match('/Content-Type: ([^;]*)/', $http, $matches)) { |
|
| 57 | + if (isset($split[1]) && $payload = $split[1]) { |
|
| 58 | + if (preg_match('/Content-Type: ([^;]*)/', $http, $matches)) { |
|
| 59 | 59 | $mime = $matches[1]; |
| 60 | 60 | } else { |
| 61 | 61 | $mime = 'text/plain'; |
@@ -68,21 +68,19 @@ discard block |
||
| 68 | 68 | public function languageByName($name, $params = []) |
| 69 | 69 | { |
| 70 | 70 | return isset($this->_languages['name'][$name]) ? |
| 71 | - $this->_languages['name'][$name]($params) : |
|
| 72 | - new PlainText($params); |
|
| 71 | + $this->_languages['name'][$name]($params) : new PlainText($params); |
|
| 73 | 72 | } |
| 74 | 73 | |
| 75 | 74 | public function languageByMime($mime, $params = []) |
| 76 | 75 | { |
| 77 | 76 | return isset($this->_languages['mime'][$mime]) ? |
| 78 | - $this->_languages['mime'][$mime]($params) : |
|
| 79 | - new PlainText($params); |
|
| 77 | + $this->_languages['mime'][$mime]($params) : new PlainText($params); |
|
| 80 | 78 | } |
| 81 | 79 | |
| 82 | 80 | public function languageByExt($filename, $params = []) |
| 83 | 81 | { |
| 84 | - foreach($this->_languages['extension'] as $mask => $class) { |
|
| 85 | - if(fnmatch($mask, $filename)) { |
|
| 82 | + foreach ($this->_languages['extension'] as $mask => $class) { |
|
| 83 | + if (fnmatch($mask, $filename)) { |
|
| 86 | 84 | return $class($params); |
| 87 | 85 | } |
| 88 | 86 | } |
@@ -108,7 +106,7 @@ discard block |
||
| 108 | 106 | |
| 109 | 107 | public function registeredLanguages($by = 'name', $class = false) |
| 110 | 108 | { |
| 111 | - return array_map(function ($e) use($class) { |
|
| 109 | + return array_map(function($e) use($class) { |
|
| 112 | 110 | return $e([])->getFQN($class); |
| 113 | 111 | }, $this->_languages[$by]); |
| 114 | 112 | } |
@@ -133,7 +131,7 @@ discard block |
||
| 133 | 131 | |
| 134 | 132 | public function init() |
| 135 | 133 | { |
| 136 | - foreach(include __DIR__.'/Config/aliases.php' as $alias) { |
|
| 134 | + foreach (include __DIR__.'/Config/aliases.php' as $alias) { |
|
| 137 | 135 | $class = $alias[0]; |
| 138 | 136 | unset($alias[0]); |
| 139 | 137 | |
@@ -147,13 +145,13 @@ discard block |
||
| 147 | 145 | */ |
| 148 | 146 | public function register($class, array $options) |
| 149 | 147 | { |
| 150 | - if(!is_callable($class) && is_subclass_of($class, Language::class)) { |
|
| 148 | + if (!is_callable($class) && is_subclass_of($class, Language::class)) { |
|
| 151 | 149 | $class = function($arguments = []) use ($class) { |
| 152 | 150 | return new $class($arguments); |
| 153 | 151 | }; |
| 154 | 152 | } |
| 155 | 153 | |
| 156 | - foreach($options as $name => $values) { |
|
| 154 | + foreach ($options as $name => $values) { |
|
| 157 | 155 | $this->_languages[$name] = array_merge($this->_languages[$name], array_fill_keys($values, $class)); |
| 158 | 156 | } |
| 159 | 157 | } |
@@ -88,7 +88,7 @@ |
||
| 88 | 88 | |
| 89 | 89 | public final function getFQN($class = false) |
| 90 | 90 | { |
| 91 | - $embedded =$this->getEmbedded(); |
|
| 91 | + $embedded = $this->getEmbedded(); |
|
| 92 | 92 | return ($class ? get_class($this) : $this->getIdentifier()).( |
| 93 | 93 | !empty($embedded) |
| 94 | 94 | ? ' + '.implode(', ', array_map($class ? 'get_class' : function(Language $e) { return $e->getIdentifier(); }, $embedded)) |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | protected function getCommandName(InputInterface $input) |
| 35 | 35 | { |
| 36 | 36 | $command = $input->getFirstArgument(); |
| 37 | - if(!$command && !$input->hasParameterOption('--help')) { |
|
| 37 | + if (!$command && !$input->hasParameterOption('--help')) { |
|
| 38 | 38 | return 'list'; |
| 39 | 39 | } elseif ($this->has($command)) { |
| 40 | 40 | return $command; |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | protected function getDefaultInputDefinition() |
| 57 | 57 | { |
| 58 | 58 | $input = parent::getDefaultInputDefinition(); |
| 59 | - $input->setOptions(array_filter($input->getOptions(), function (InputOption $option) { |
|
| 59 | + $input->setOptions(array_filter($input->getOptions(), function(InputOption $option) { |
|
| 60 | 60 | return $option->getShortcut() != 'q'; |
| 61 | 61 | })); |
| 62 | 62 | $input->addOption(new InputOption('no-output', 's', InputOption::VALUE_NONE, 'Disables output, useful for debug')); |
@@ -40,7 +40,7 @@ |
||
| 40 | 40 | $formatters = KeyLighter::get()->registeredFormatters(); |
| 41 | 41 | $table = new Table($output); |
| 42 | 42 | |
| 43 | - if(!$input->getOption('headerless')) { |
|
| 43 | + if (!$input->getOption('headerless')) { |
|
| 44 | 44 | $table->setHeaders(['Name', 'Formatter']); |
| 45 | 45 | } |
| 46 | 46 | |
@@ -46,13 +46,13 @@ discard block |
||
| 46 | 46 | |
| 47 | 47 | $table = new Table($output); |
| 48 | 48 | |
| 49 | - if(!$input->getOption('headerless')) { |
|
| 49 | + if (!$input->getOption('headerless')) { |
|
| 50 | 50 | $table->setHeaders([ucfirst($input->getArgument('by')), $input->getOption('classes') ? 'Class name' : 'Language']); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | $table->setRows(array_map(function($language) { |
| 54 | 54 | return [ |
| 55 | - implode(', ', array_map(function ($f) { return "<comment>{$f}</comment>"; }, $language['aliases'])), |
|
| 55 | + implode(', ', array_map(function($f) { return "<comment>{$f}</comment>"; }, $language['aliases'])), |
|
| 56 | 56 | $language['class'] |
| 57 | 57 | ]; |
| 58 | 58 | }, $input->getOption('no-group') ? $this->processNonGrouped($languages) : $this->processGrouped($languages))); |
@@ -63,8 +63,8 @@ discard block |
||
| 63 | 63 | |
| 64 | 64 | protected function processGrouped($languages) { |
| 65 | 65 | $result = []; |
| 66 | - foreach($languages as $alias => $class) { |
|
| 67 | - if(!isset($result[$class])) { |
|
| 66 | + foreach ($languages as $alias => $class) { |
|
| 67 | + if (!isset($result[$class])) { |
|
| 68 | 68 | $result[$class] = ['aliases' => [], 'class' => $class]; |
| 69 | 69 | } |
| 70 | 70 | |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | |
| 77 | 77 | protected function processNonGrouped($languages) { |
| 78 | 78 | $result = []; |
| 79 | - foreach($languages as $alias => $class) { |
|
| 79 | + foreach ($languages as $alias => $class) { |
|
| 80 | 80 | $result[] = ['aliases' => [$alias], 'class' => $class]; |
| 81 | 81 | } |
| 82 | 82 | |
@@ -44,8 +44,8 @@ discard block |
||
| 44 | 44 | |
| 45 | 45 | public function setRules($rules) |
| 46 | 46 | { |
| 47 | - if(empty($rules)) { |
|
| 48 | - $this->_rules = [ 'none' => Validator::CONTEXT_IN_ONE_OF ]; |
|
| 47 | + if (empty($rules)) { |
|
| 48 | + $this->_rules = ['none' => Validator::CONTEXT_IN_ONE_OF]; |
|
| 49 | 49 | } else { |
| 50 | 50 | foreach ($rules as $key => $rule) { |
| 51 | 51 | list($plain, $type) = $this->_parse($rule); |
@@ -57,8 +57,8 @@ discard block |
||
| 57 | 57 | private function _clean($rule, &$required) |
| 58 | 58 | { |
| 59 | 59 | if (strpos($rule, '.') !== false) { |
| 60 | - foreach (array_filter(array_keys($required), function ($key) use ($rule) { |
|
| 61 | - return fnmatch($key . '.*', $rule); |
|
| 60 | + foreach (array_filter(array_keys($required), function($key) use ($rule) { |
|
| 61 | + return fnmatch($key.'.*', $rule); |
|
| 62 | 62 | }) as $remove) { |
| 63 | 63 | unset($required[$remove]); |
| 64 | 64 | } |
@@ -66,11 +66,11 @@ discard block |
||
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | protected function _validate($context, $rules, $result = false) { |
| 69 | - if(empty($context)) { |
|
| 69 | + if (empty($context)) { |
|
| 70 | 70 | $context = ['none']; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - while(list($rule, $type) = each($rules)) { |
|
| 73 | + while (list($rule, $type) = each($rules)) { |
|
| 74 | 74 | $matched = $this->_matches($context, $rule, $type); |
| 75 | 75 | |
| 76 | 76 | if ($type & Validator::CONTEXT_NOT_IN) { |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | |
| 124 | 124 | $rule = substr($rule, $pos); |
| 125 | 125 | |
| 126 | - if($type & self::CONTEXT_REGEX) { |
|
| 126 | + if ($type & self::CONTEXT_REGEX) { |
|
| 127 | 127 | $rule = "/^$rule(\\.\\w+)?/i"; |
| 128 | 128 | } |
| 129 | 129 | |
@@ -131,22 +131,22 @@ discard block |
||
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | private function _matches($context, $rule, $type) { |
| 134 | - if($type & self::CONTEXT_EXACTLY) { |
|
| 134 | + if ($type & self::CONTEXT_EXACTLY) { |
|
| 135 | 135 | return in_array($rule, $context, true); |
| 136 | - } elseif($type & self::CONTEXT_REGEX) { |
|
| 137 | - foreach($context as $item) { |
|
| 138 | - if(preg_match($rule, $item)) { |
|
| 136 | + } elseif ($type & self::CONTEXT_REGEX) { |
|
| 137 | + foreach ($context as $item) { |
|
| 138 | + if (preg_match($rule, $item)) { |
|
| 139 | 139 | return true; |
| 140 | 140 | } |
| 141 | 141 | } |
| 142 | 142 | return false; |
| 143 | 143 | } else { |
| 144 | - if(in_array($rule, $context, true)) { |
|
| 144 | + if (in_array($rule, $context, true)) { |
|
| 145 | 145 | return true; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | - foreach($context as $item) { |
|
| 149 | - if(fnmatch("$rule.*", $item)) { |
|
| 148 | + foreach ($context as $item) { |
|
| 149 | + if (fnmatch("$rule.*", $item)) { |
|
| 150 | 150 | return true; |
| 151 | 151 | } |
| 152 | 152 | } |
@@ -158,7 +158,7 @@ discard block |
||
| 158 | 158 | { |
| 159 | 159 | static $validator; |
| 160 | 160 | if (!$validator) { |
| 161 | - $validator = new DelegateValidator(function () { |
|
| 161 | + $validator = new DelegateValidator(function() { |
|
| 162 | 162 | return true; |
| 163 | 163 | }); |
| 164 | 164 | } |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | * @param InputInterface $input |
| 48 | 48 | * @param Language $language |
| 49 | 49 | * @param FormatterInterface $formatter |
| 50 | - * @param $source |
|
| 50 | + * @param string $source |
|
| 51 | 51 | */ |
| 52 | 52 | public function __construct( |
| 53 | 53 | OutputInterface $output, InputInterface $input, Language $language, FormatterInterface $formatter, $source |
@@ -97,11 +97,17 @@ discard block |
||
| 97 | 97 | return $formatted; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | + /** |
|
| 101 | + * @param string $feature |
|
| 102 | + */ |
|
| 100 | 103 | public function wants($feature) |
| 101 | 104 | { |
| 102 | 105 | return in_array($feature, $this->_input->getOption('debug')); |
| 103 | 106 | } |
| 104 | 107 | |
| 108 | + /** |
|
| 109 | + * @return Tokens |
|
| 110 | + */ |
|
| 105 | 111 | protected function tokenize() |
| 106 | 112 | { |
| 107 | 113 | $tokens = $this->benchmark(function () { |
@@ -125,12 +131,18 @@ discard block |
||
| 125 | 131 | return $return; |
| 126 | 132 | } |
| 127 | 133 | |
| 134 | + /** |
|
| 135 | + * @param string $message |
|
| 136 | + */ |
|
| 128 | 137 | private function _tree(Tokens $tree, $message, $indented = true) |
| 129 | 138 | { |
| 130 | 139 | $this->_output->writeln("Token tree <comment>$message</comment>: "); |
| 131 | 140 | $this->_output->writeln($this->_debug->format(clone $tree, $indented)); |
| 132 | 141 | } |
| 133 | 142 | |
| 143 | + /** |
|
| 144 | + * @return Tokens |
|
| 145 | + */ |
|
| 134 | 146 | protected function parse(Tokens $tokens) |
| 135 | 147 | { |
| 136 | 148 | $tokens = $this->benchmark(function () use ($tokens) { |
@@ -152,6 +164,9 @@ discard block |
||
| 152 | 164 | }, $this->_times['formatting']); |
| 153 | 165 | } |
| 154 | 166 | |
| 167 | + /** |
|
| 168 | + * @param string $message |
|
| 169 | + */ |
|
| 155 | 170 | private function _slashed($message, $data) |
| 156 | 171 | { |
| 157 | 172 | $this->_output->writeln(sprintf( |
@@ -68,14 +68,14 @@ discard block |
||
| 68 | 68 | $parsed = $this->parse($tokenized); |
| 69 | 69 | $formatted = $this->format($parsed); |
| 70 | 70 | |
| 71 | - if($this->wants('time')) { |
|
| 71 | + if ($this->wants('time')) { |
|
| 72 | 72 | $this->_output->writeln(sprintf( |
| 73 | 73 | '<info>Overall:</info> %.4fs, %s chars/s', |
| 74 | 74 | array_sum($this->_times), number_format(strlen($this->_source) / array_sum($this->_times)) |
| 75 | 75 | )); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - if($this->wants('detailed-time')) { |
|
| 78 | + if ($this->wants('detailed-time')) { |
|
| 79 | 79 | $this->_slashed('Times [s]', array_map(function($t) { |
| 80 | 80 | return number_format($t, 5); |
| 81 | 81 | }, $this->_times)); |
@@ -84,11 +84,11 @@ discard block |
||
| 84 | 84 | }, $this->_times)); |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - if($this->wants('count')) { |
|
| 87 | + if ($this->wants('count')) { |
|
| 88 | 88 | $this->_slashed('Token count', array_map('number_format', $this->_counts)); |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - if($this->wants('density')) { |
|
| 91 | + if ($this->wants('density')) { |
|
| 92 | 92 | $this->_slashed('Token density [tokens/kB]', array_map(function($c) { |
| 93 | 93 | return number_format($c / strlen($this->_source) * 1024, 1); |
| 94 | 94 | }, $this->_counts)); |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | |
| 105 | 105 | protected function tokenize() |
| 106 | 106 | { |
| 107 | - $tokens = $this->benchmark(function () { |
|
| 107 | + $tokens = $this->benchmark(function() { |
|
| 108 | 108 | return $this->_language->tokenize($this->_source); |
| 109 | 109 | }, $this->_times['tokenization']); |
| 110 | 110 | |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | protected function parse(Tokens $tokens) |
| 135 | 135 | { |
| 136 | - $tokens = $this->benchmark(function () use ($tokens) { |
|
| 136 | + $tokens = $this->benchmark(function() use ($tokens) { |
|
| 137 | 137 | return $this->_language->parse($tokens); |
| 138 | 138 | }, $this->_times['parsing']); |
| 139 | 139 | |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | |
| 148 | 148 | protected function format(Tokens $tokens) |
| 149 | 149 | { |
| 150 | - return $this->benchmark(function () use ($tokens) { |
|
| 150 | + return $this->benchmark(function() use ($tokens) { |
|
| 151 | 151 | return $this->_formatter->format($tokens); |
| 152 | 152 | }, $this->_times['formatting']); |
| 153 | 153 | } |
@@ -156,10 +156,10 @@ discard block |
||
| 156 | 156 | { |
| 157 | 157 | $this->_output->writeln(sprintf( |
| 158 | 158 | "$message %s: %s", |
| 159 | - implode(' / ', array_map(function ($f) { |
|
| 159 | + implode(' / ', array_map(function($f) { |
|
| 160 | 160 | return "<comment>{$f}</comment>"; |
| 161 | 161 | }, array_keys($data))), |
| 162 | - implode(' / ', array_map(function ($f) { |
|
| 162 | + implode(' / ', array_map(function($f) { |
|
| 163 | 163 | return "<info>{$f}</info>"; |
| 164 | 164 | }, array_values($data))) |
| 165 | 165 | )); |
@@ -47,23 +47,23 @@ discard block |
||
| 47 | 47 | |
| 48 | 48 | protected function execute(InputInterface $input, OutputInterface $output) |
| 49 | 49 | { |
| 50 | - if(!empty($input->getOption('debug')) && $output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) { |
|
| 50 | + if (!empty($input->getOption('debug')) && $output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) { |
|
| 51 | 51 | $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | $output->writeln($this->getApplication()->getLongVersion()."\n", Output::VERBOSITY_VERBOSE); |
| 55 | 55 | $formatter = KeyLighter::get()->getFormatter($input->getOption('format')) ?: KeyLighter::get()->getDefaultFormatter(); |
| 56 | 56 | |
| 57 | - foreach($input->getArgument('path') as $filename) { |
|
| 57 | + foreach ($input->getArgument('path') as $filename) { |
|
| 58 | 58 | $language = $input->getOption('language') |
| 59 | 59 | ? Language::byName($input->getOption('language')) |
| 60 | 60 | : Language::byFilename($filename); |
| 61 | 61 | |
| 62 | - if(!($source = $this->content($filename))) { |
|
| 62 | + if (!($source = $this->content($filename))) { |
|
| 63 | 63 | throw new InvalidArgumentException(sprintf('Specified file %s doesn\'t exist, check if given path is correct.', $filename)); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - if($output->isVerbose()) { |
|
| 66 | + if ($output->isVerbose()) { |
|
| 67 | 67 | $output->writeln(sprintf( |
| 68 | 68 | "Used file: <path>%s</path>, Language: <language>%s</language>, Formatter: <formatter>%s</formatter>", |
| 69 | 69 | $filename, $language->getFQN(), get_class($formatter) |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | $formatted = KeyLighter::get()->highlight($source, $language, $formatter); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - if(!$input->getOption('no-output')) { |
|
| 78 | + if (!$input->getOption('no-output')) { |
|
| 79 | 79 | $output->writeln($formatted); |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -83,12 +83,12 @@ discard block |
||
| 83 | 83 | |
| 84 | 84 | protected function content($path) |
| 85 | 85 | { |
| 86 | - if(!($file = @fopen($path, 'r'))) { |
|
| 86 | + if (!($file = @fopen($path, 'r'))) { |
|
| 87 | 87 | return false; |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | $content = ''; |
| 91 | - while(!feof($file)) { |
|
| 91 | + while (!feof($file)) { |
|
| 92 | 92 | $content .= fgets($file); |
| 93 | 93 | } |
| 94 | 94 | fclose($file); |