diff --git a/Resources/views/Collector/partials/call.html.twig b/Resources/views/Collector/partials/call.html.twig index dd4996e..0feb068 100644 --- a/Resources/views/Collector/partials/call.html.twig +++ b/Resources/views/Collector/partials/call.html.twig @@ -105,11 +105,11 @@ {% if call.curlCommand is defined %}
- +
- @@ -120,11 +120,11 @@ {% if call.blackfireCommand is defined %}
-
-
{{ call.curlCommand | raw }}
+
+
{{ call.curlCommand | raw }}
+
- diff --git a/Resources/views/Collector/partials/request.html.twig b/Resources/views/Collector/partials/request.html.twig index d3596f9..2dd21ed 100644 --- a/Resources/views/Collector/partials/request.html.twig +++ b/Resources/views/Collector/partials/request.html.twig @@ -1,60 +1,60 @@ {% macro display_array_recursive(array, separator = ', ', opening_char = '[', closing_char = ']') -%} -{{ opening_char }} -{%- for key, value in array -%} - {%- if value is iterable -%} - {{ key }} => {{ _self.display_array_recursive(value, separator, opening_char, closing_char) }} - {%- else -%} - {{ key }} => {{ value }} - {%- if not loop.last %}{{ separator }}{% endif -%} - {%- endif -%} -{%- endfor -%} -{{ closing_char }} + {{ opening_char }} + {%- for key, value in array -%} + {%- if value is iterable -%} + {{ key }} => {{ _self.display_array_recursive(value, separator, opening_char, closing_char) }} + {%- else -%} + {{ key }} => {{ value }} + {%- if not loop.last %}{{ separator }}{% endif -%} + {%- endif -%} + {%- endfor -%} + {{ closing_char }} {%- endmacro %} {% if request.query %} -
Query Parameters
+
Query Parameters
-
-
{{ call.blackfireCommand | raw }}
+
+
{{ call.blackfireCommand | raw }}
- +
+ {% for name,parameter in request.query %} - - - - + + + + {% endfor %} - -
{{ name }} - {% if parameter is iterable %} - {{ _self.display_array_recursive(parameter) }} - {% else %} - {{ parameter }} - {% endif %} -
{{ name }} + {% if parameter is iterable %} + {{ _self.display_array_recursive(parameter) }} + {% else %} + {{ parameter }} + {% endif %} +
+ + {% endif %} {% if request.content %} -
Body
+
Body
- - +
+ - - -
+ {{ request.content | simple_http_beautify(request.contentType) }}
+ + {% endif %}
Headers
- +
{% for header in request.headers %} - + {% endfor %} diff --git a/Resources/views/Collector/partials/response.html.twig b/Resources/views/Collector/partials/response.html.twig index 54ebfab..cf30da9 100644 --- a/Resources/views/Collector/partials/response.html.twig +++ b/Resources/views/Collector/partials/response.html.twig @@ -2,21 +2,23 @@
Body
-
{{ header|join(', ') }}{{ header|join(', ') }}
+
- - - + + +
{{ response.body }}
+
{{ response.body }}
+
Headers
- +
{% for header in response.headers %} - diff --git a/Twig/Extension.php b/Twig/Extension.php index 4093551..bfa32cc 100644 --- a/Twig/Extension.php +++ b/Twig/Extension.php @@ -2,18 +2,18 @@ namespace evaisse\SimpleHttpBundle\Twig; +use Symfony\Component\BrowserKit\Exception\JsonException; use Symfony\Component\HttpFoundation\Response; use Twig\Error\LoaderError; -use Twig\Extension\ExtensionInterface; +use Twig\Extension\AbstractExtension; use Twig\Loader\LoaderInterface; use Twig\TwigFilter; use Twig\TwigFunction; -class Extension implements ExtensionInterface +class Extension extends AbstractExtension { public function __construct(protected LoaderInterface $loader) { - $this->loader = $loader; } /** @@ -50,16 +50,11 @@ public function getFunctions(): array ]; } - /** - * @param int|float $number - * @param int $decimals - * @return string - */ - public function numberFormat($number, $decimals = 0): string + public function numberFormat(int|float $number, int $decimals = 0): string { static $locale; - $locale = $locale ? $locale : localeconv(); + $locale = $locale ?: localeconv(); return number_format($number, $decimals, $locale['decimal_point'], $locale['thousands_sep']); } @@ -72,9 +67,9 @@ public function formatMilliseconds($ms): string { if ($ms >= 1) { return $this->numberFormat($ms, 1) . ' s'; - } else { - return $this->numberFormat($ms * 1000) . " ms"; } + + return $this->numberFormat($ms * 1000) . " ms"; } /** @@ -86,15 +81,11 @@ public function md5($str): string return md5($str); } - /** - * @param $file - * @return string - */ - public function assetInclude($file): string + public function assetInclude(string $file): string { try { return $this->loader->getSourceContext($file)->getCode(); - } catch (LoaderError $e) { + } catch (LoaderError) { return ''; } } @@ -103,10 +94,13 @@ public function assetInclude($file): string * @param int|array $codeOrResponse response data or just an http code * @return string */ - public function formatHttpCode($codeOrResponse): string + public function formatHttpCode(int|array $codeOrResponse): string { $d = $this->fetchInfosFromCodeOrResponse($codeOrResponse); - return ''.$d['code'].($d['fromCache']?' +cached':'').''; + return '' . $d['code'] . ($d['fromCache'] ? ' +cached' : '') . + ''; } @@ -114,96 +108,75 @@ public function formatHttpCode($codeOrResponse): string * @param int|array $codeOrResponse response data or just an http code * @return string */ - public function formatHttpCodeAsSfBadge($codeOrResponse): string + public function formatHttpCodeAsSfBadge(int|array $codeOrResponse): string { $d = $this->fetchInfosFromCodeOrResponse($codeOrResponse); - return '' - .''.$d['code'].($d['fromCache']?'+cache':'').'' - .''; + return '' + . '' . $d['code'] . + ($d['fromCache'] ? '+cache' : '') . '' + . ''; } - /** - * @param $code - * @param $contentType - * @return string - */ - public function format($code, $contentType) + public function format(string $code, string $contentType): string { $class = array('hljs'); - if (strpos($contentType, 'application/json') !== false) { + if (str_contains($contentType, 'application/json')) { $class[] = 'json'; $code = @$this->formatJson($code); - } else if (strpos($contentType, 'application/xml') !== false) { + } elseif (str_contains($contentType, 'application/xml')) { $class[] = 'xml'; $code = @$this->formatXml($code); } else { $class[] = 'html'; } - return '
' . htmlentities($code) . '
'; + return '
' . htmlentities($code) . '
'; } - /** - * @param $xml - * @return string - */ - public function formatXml($xml) + public function formatXml(string $xml): string { $domxml = new \DOMDocument('1.0'); $domxml->preserveWhiteSpace = false; $domxml->formatOutput = true; $domxml->loadXML($xml); - return $domxml->saveXML(); - } - /** - * @param $html - * @return mixed - */ - public function formatHtml($html) - { - return $html; + return $domxml->saveXML(); } - /** - * @param $json - * @return string - */ - public function formatJson($json) + public function formatJson(string $json): string { $result = ''; $level = 0; $in_quotes = false; $in_escape = false; - $ends_line_level = NULL; + $ends_line_level = null; $json_length = strlen($json); - + for ($i = 0; $i < $json_length; $i++) { $char = $json[$i]; - $new_line_level = NULL; + $new_line_level = null; $post = ""; - if ($ends_line_level !== NULL) { + if ($ends_line_level !== null) { $new_line_level = $ends_line_level; - $ends_line_level = NULL; + $ends_line_level = null; } if ($in_escape) { $in_escape = false; - } - else if ($char === '"') { + } elseif ($char === '"') { $in_quotes = !$in_quotes; - } - else if (!$in_quotes) { + } elseif (!$in_quotes) { switch ($char) { case '}': case ']': $level--; - $ends_line_level = NULL; + $ends_line_level = null; $new_line_level = $level; break; - case '{': case '[': $level++; + // intentional no break case ',': $ends_line_level = $level; break; @@ -218,19 +191,19 @@ public function formatJson($json) case "\r": $char = ""; $ends_line_level = $new_line_level; - $new_line_level = NULL; + $new_line_level = null; break; } - } - else if ($char === '\\') { + } elseif ($char === '\\') { $in_escape = true; } - if ($new_line_level !== NULL) { - $result.= "\n" . str_repeat(" ", $new_line_level); + if ($new_line_level !== null) { + $result .= "\n" . str_repeat(" ", $new_line_level); } - $result.= $char . $post; + + $result .= $char . $post; } - + return $result; } @@ -239,27 +212,27 @@ public function formatJson($json) * @param int|array $codeOrResponse response data or just an http code * @return array infos "fromCache", "text", "color" */ - protected function fetchInfosFromCodeOrResponse($codeOrResponse) + protected function fetchInfosFromCodeOrResponse(int|array $codeOrResponse): array { $fromCache = false; if (is_array($codeOrResponse)) { $response = $codeOrResponse; - $code = array_key_exists('statusCode', $response)?$response['statusCode']:'N/A'; + $code = array_key_exists('statusCode', $response) ? $response['statusCode'] : 'N/A'; $fromCache = !empty($response['fromHttpCache']); } else { - $code = (int)$codeOrResponse; + $code = $codeOrResponse; } - if ($code >= 500) { + if ($code === 'N/A' || $code >= 500) { $cls = "red"; $level = "error"; - } else if ($code >= 400) { + } elseif ($code >= 400) { $cls = "yellow"; $level = "warning"; - } else if ($code >= 300) { + } elseif ($code >= 300) { $cls = 'blue'; $level = 'info'; - } else if ($code >= 200) { + } elseif ($code >= 200) { $cls = "green"; $level = "success"; } else { @@ -267,7 +240,7 @@ protected function fetchInfosFromCodeOrResponse($codeOrResponse) $level = 'default'; } - $statusText = Response::$statusTexts[$code]; + $statusText = $code === 'N/A' ? 'N/A' : Response::$statusTexts[$code]; return [ 'fromCache' => $fromCache, @@ -282,7 +255,7 @@ protected function fetchInfosFromCodeOrResponse($codeOrResponse) * @param array $response * @return array */ - public function decodeBody(array $response) + public function decodeBody(array $response): array { if (array_key_exists('headers', $response)) { foreach ($response['headers'] as $h) { @@ -294,38 +267,7 @@ public function decodeBody(array $response) } } } - return []; - } - /** - * @inheritDoc - */ - public function getTokenParsers(): array - { - return []; - } - - /** - * @inheritDoc - */ - public function getNodeVisitors(): array - { - return []; - } - - /** - * @inheritDoc - */ - public function getTests(): array - { - return []; - } - - /** - * @inheritDoc - */ - public function getOperators(): array - { return []; } } diff --git a/composer.json b/composer.json index dc80684..ebc268c 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,8 @@ }, "require-dev": { "roave/security-advisories": "dev-latest", - "phpunit/phpunit": "^9.5" + "phpunit/phpunit": "^9.5", + "phpstan/phpstan": "^2.0" }, "suggest": { }, diff --git a/composer.lock b/composer.lock index 23c6305..a585f36 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7a2b55d23532de7093f64d9d15038702", + "content-hash": "8dce778de0f415aa44c7d9a8f5049aa3", "packages": [ { "name": "doctrine/annotations", @@ -4161,6 +4161,64 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "phpstan/phpstan", + "version": "2.1.14", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "8f2e03099cac24ff3b379864d171c5acbfc6b9a2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/8f2e03099cac24ff3b379864d171c5acbfc6b9a2", + "reference": "8f2e03099cac24ff3b379864d171c5acbfc6b9a2", + "shasum": "" + }, + "require": { + "php": "^7.4|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + } + ], + "time": "2025-05-02T15:32:28+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "9.2.29", diff --git a/phpstan.neon b/phpstan.neon index 9fa90dc..8c90e21 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,6 +2,6 @@ parameters: ignoreErrors: - '#.*Serializer.*extends @final class.*#' excludePaths: - - 'vendor' - - 'build' + - vendor/** + - build/** reportUnmatchedIgnoredErrors: false
+ {{ header }}