PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/tracy/tracy/src/Tracy/Helpers.php

https://gitlab.com/paveltizek/shop
PHP | 221 lines | 161 code | 31 blank | 29 comment | 26 complexity | b03cd5ba35dd86d005cbacec800c2aad MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Tracy (https://tracy.nette.org)
  4. * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  5. */
  6. namespace Tracy;
  7. /**
  8. * Rendering helpers for Debugger.
  9. */
  10. class Helpers
  11. {
  12. /**
  13. * Returns HTML link to editor.
  14. * @return string
  15. */
  16. public static function editorLink($file, $line = NULL)
  17. {
  18. if ($editor = self::editorUri($file, $line)) {
  19. $file = strtr($file, '\\', '/');
  20. if (preg_match('#(^[a-z]:)?/.{1,50}$#i', $file, $m) && strlen($file) > strlen($m[0])) {
  21. $file = '...' . $m[0];
  22. }
  23. $file = strtr($file, '/', DIRECTORY_SEPARATOR);
  24. return self::formatHtml('<a href="%" title="%">%<b>%</b>%</a>',
  25. $editor,
  26. $file . ($line ? ":$line" : ''),
  27. rtrim(dirname($file), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR,
  28. basename($file),
  29. $line ? ":$line" : ''
  30. );
  31. } else {
  32. return self::formatHtml('<span>%</span>', $file . ($line ? ":$line" : ''));
  33. }
  34. }
  35. /**
  36. * Returns link to editor.
  37. * @return string
  38. */
  39. public static function editorUri($file, $line = NULL)
  40. {
  41. if (Debugger::$editor && $file && is_file($file)) {
  42. return strtr(Debugger::$editor, array('%file' => rawurlencode($file), '%line' => $line ? (int) $line : 1));
  43. }
  44. }
  45. public static function formatHtml($mask)
  46. {
  47. $args = func_get_args();
  48. return preg_replace_callback('#%#', function () use (& $args, & $count) {
  49. return htmlspecialchars($args[++$count], ENT_IGNORE | ENT_QUOTES, 'UTF-8');
  50. }, $mask);
  51. }
  52. public static function findTrace(array $trace, $method, & $index = NULL)
  53. {
  54. $m = explode('::', $method);
  55. foreach ($trace as $i => $item) {
  56. if (isset($item['function']) && $item['function'] === end($m)
  57. && isset($item['class']) === isset($m[1])
  58. && (!isset($item['class']) || $item['class'] === $m[0] || $m[0] === '*' || is_subclass_of($item['class'], $m[0]))
  59. ) {
  60. $index = $i;
  61. return $item;
  62. }
  63. }
  64. }
  65. /**
  66. * @return string
  67. */
  68. public static function getClass($obj)
  69. {
  70. return current(explode("\x00", get_class($obj)));
  71. }
  72. /** @internal */
  73. public static function fixStack($exception)
  74. {
  75. if (function_exists('xdebug_get_function_stack')) {
  76. $stack = array();
  77. foreach (array_slice(array_reverse(xdebug_get_function_stack()), 2, -1) as $row) {
  78. $frame = array(
  79. 'file' => $row['file'],
  80. 'line' => $row['line'],
  81. 'function' => isset($row['function']) ? $row['function'] : '*unknown*',
  82. 'args' => array(),
  83. );
  84. if (!empty($row['class'])) {
  85. $frame['type'] = isset($row['type']) && $row['type'] === 'dynamic' ? '->' : '::';
  86. $frame['class'] = $row['class'];
  87. }
  88. $stack[] = $frame;
  89. }
  90. $ref = new \ReflectionProperty('Exception', 'trace');
  91. $ref->setAccessible(TRUE);
  92. $ref->setValue($exception, $stack);
  93. }
  94. return $exception;
  95. }
  96. /** @internal */
  97. public static function fixEncoding($s)
  98. {
  99. if (PHP_VERSION_ID < 50400) {
  100. return @iconv('UTF-16', 'UTF-8//IGNORE', iconv('UTF-8', 'UTF-16//IGNORE', $s)); // intentionally @
  101. } else {
  102. return htmlspecialchars_decode(htmlspecialchars($s, ENT_NOQUOTES | ENT_IGNORE, 'UTF-8'), ENT_NOQUOTES);
  103. }
  104. }
  105. /** @internal */
  106. public static function errorTypeToString($type)
  107. {
  108. $types = array(
  109. E_ERROR => 'Fatal Error',
  110. E_USER_ERROR => 'User Error',
  111. E_RECOVERABLE_ERROR => 'Recoverable Error',
  112. E_CORE_ERROR => 'Core Error',
  113. E_COMPILE_ERROR => 'Compile Error',
  114. E_PARSE => 'Parse Error',
  115. E_WARNING => 'Warning',
  116. E_CORE_WARNING => 'Core Warning',
  117. E_COMPILE_WARNING => 'Compile Warning',
  118. E_USER_WARNING => 'User Warning',
  119. E_NOTICE => 'Notice',
  120. E_USER_NOTICE => 'User Notice',
  121. E_STRICT => 'Strict standards',
  122. E_DEPRECATED => 'Deprecated',
  123. E_USER_DEPRECATED => 'User Deprecated',
  124. );
  125. return isset($types[$type]) ? $types[$type] : 'Unknown error';
  126. }
  127. /** @internal */
  128. public static function getSource()
  129. {
  130. if (isset($_SERVER['REQUEST_URI'])) {
  131. return (!empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https://' : 'http://')
  132. . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '')
  133. . $_SERVER['REQUEST_URI'];
  134. } else {
  135. return empty($_SERVER['argv']) ? 'CLI' : 'CLI: ' . implode(' ', $_SERVER['argv']);
  136. }
  137. }
  138. /** @internal */
  139. public static function improveException($e)
  140. {
  141. $message = $e->getMessage();
  142. if (!$e instanceof \Error && !$e instanceof \ErrorException) {
  143. // do nothing
  144. } elseif (preg_match('#^Call to undefined function (\S+\\\\)?(\w+)\(#', $message, $m)) {
  145. $funcs = get_defined_functions();
  146. $funcs = array_merge($funcs['internal'], $funcs['user']);
  147. $hint = self::getSuggestion($funcs, $m[1] . $m[2]) ?: self::getSuggestion($funcs, $m[2]);
  148. $message .= ", did you mean $hint()?";
  149. } elseif (preg_match('#^Call to undefined method (\S+)::(\w+)#', $message, $m)) {
  150. $hint = self::getSuggestion(get_class_methods($m[1]), $m[2]);
  151. $message .= ", did you mean $hint()?";
  152. } elseif (preg_match('#^Undefined variable: (\w+)#', $message, $m) && !empty($e->context)) {
  153. $hint = self::getSuggestion(array_keys($e->context), $m[1]);
  154. $message = "Undefined variable $$m[1], did you mean $$hint?";
  155. } elseif (preg_match('#^Undefined property: (\S+)::\$(\w+)#', $message, $m)) {
  156. $rc = new \ReflectionClass($m[1]);
  157. $items = array_diff($rc->getProperties(\ReflectionProperty::IS_PUBLIC), $rc->getProperties(\ReflectionProperty::IS_STATIC));
  158. $hint = self::getSuggestion($items, $m[2]);
  159. $message .= ", did you mean $$hint?";
  160. } elseif (preg_match('#^Access to undeclared static property: (\S+)::\$(\w+)#', $message, $m)) {
  161. $rc = new \ReflectionClass($m[1]);
  162. $items = array_intersect($rc->getProperties(\ReflectionProperty::IS_PUBLIC), $rc->getProperties(\ReflectionProperty::IS_STATIC));
  163. $hint = self::getSuggestion($items, $m[2]);
  164. $message .= ", did you mean $$hint?";
  165. }
  166. if (isset($hint)) {
  167. $ref = new \ReflectionProperty($e, 'message');
  168. $ref->setAccessible(TRUE);
  169. $ref->setValue($e, $message);
  170. }
  171. }
  172. /**
  173. * Finds the best suggestion.
  174. * @return string|NULL
  175. * @internal
  176. */
  177. public static function getSuggestion(array $items, $value)
  178. {
  179. $best = NULL;
  180. $min = (strlen($value) / 4 + 1) * 10 + .1;
  181. foreach (array_unique($items, SORT_REGULAR) as $item) {
  182. $item = is_object($item) ? $item->getName() : $item;
  183. if (($len = levenshtein($item, $value, 10, 11, 10)) > 0 && $len < $min) {
  184. $min = $len;
  185. $best = $item;
  186. }
  187. }
  188. return $best;
  189. }
  190. }