From 46c2abbff468a6f595b41d9977befe71dda6248c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Fri, 29 Jan 2021 15:33:37 +0100 Subject: [PATCH] Fix Coding standard by current Nette rules --- src/Api.php | 28 ++++++++++++++++++++-------- src/AssetLoaderException.php | 2 +- src/Helpers.php | 14 +++++++++----- src/Minifier/DefaultCssMinifier.php | 6 ++---- src/Minifier/JShrinkMinifier.php | 13 ++++++------- 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/Api.php b/src/Api.php index 8aa803a..152df09 100644 --- a/src/Api.php +++ b/src/Api.php @@ -30,8 +30,13 @@ final class Api * @param string[] $formatHeaders * @param string[] $formatHtmlInjects */ - public function __construct(string $basePath, array $data, array $formatHeaders, array $formatHtmlInjects, Minifier $minifier) - { + public function __construct( + string $basePath, + array $data, + array $formatHeaders, + array $formatHtmlInjects, + Minifier $minifier + ) { $this->basePath = $basePath; $this->data = $data; $this->formatHeaders = $formatHeaders; @@ -52,7 +57,7 @@ public function getHtmlInit(string $route): string return implode("\n", array_merge( $this->renderInjectTagsByData('global-' . preg_replace('/^([^-]+)-(.*)$/', '$1', $routePath), $this->findGlobalData($route)), - $this->renderInjectTagsByData($routePath, $this->findLocalData($route)) + $this->renderInjectTagsByData($routePath, $this->findLocalData($route)), )); } @@ -83,7 +88,7 @@ public function run(string $path): void } else { throw new \RuntimeException( 'Content type for format "' . $format . '" does not exist. ' - . 'Did you mean "' . implode('", "', array_keys($this->formatHeaders)) . '"?' + . 'Did you mean "' . implode('", "', array_keys($this->formatHeaders)) . '"?', ); } @@ -104,7 +109,10 @@ public function run(string $path): void $tsString = gmdate('D, d M Y H:i:s ', $topModTime) . 'GMT'; $etag = 'EN' . $topModTime; - if (($_SERVER['HTTP_IF_NONE_MATCH'] ?? '') === $etag && ($_SERVER['HTTP_IF_MODIFIED_SINCE'] ?? '') === $tsString) { + if ( + ($_SERVER['HTTP_IF_NONE_MATCH'] ?? '') === $etag + && ($_SERVER['HTTP_IF_MODIFIED_SINCE'] ?? '') === $tsString + ) { header('HTTP/1.1 304 Not Modified'); die; } @@ -135,7 +143,11 @@ private function renderInjectTagsByData(string $route, array $data): array foreach ($data[$format] ?? [] as $item) { if (preg_match('/^((?:https?:)?\/\/)(.+)$/', $item, $itemParser)) { $return[] = str_replace('%path%', ($itemParser[1] === '//' ? 'https://' : $itemParser[1]) . $itemParser[2], $this->formatHtmlInjects[$format]); - } elseif (is_file($filePath = $this->basePath . '/' . trim($item, '/')) === true && ($modificationTime = (int) filemtime($filePath)) > 0 && $modificationTime > $topModTime) { + } elseif ( + is_file($filePath = $this->basePath . '/' . trim($item, '/')) === true + && ($modificationTime = (int) filemtime($filePath)) > 0 + && $modificationTime > $topModTime + ) { $topModTime = $modificationTime; } } @@ -144,7 +156,7 @@ private function renderInjectTagsByData(string $route, array $data): array '%path%', Url::get()->getBaseUrl() . '/assets/web-loader/' . $route . '.' . $format . ($topModTime > 0 ? '?v=' . substr(md5((string) $topModTime), 0, 6) : ''), - $this->formatHtmlInjects[$format] + $this->formatHtmlInjects[$format], ); } } @@ -222,7 +234,7 @@ private function parseRoute(string $route): array throw new AssetLoaderException( 'Route "' . $route . '" is invalid. ' . 'Route must be absolute "Module:Presenter:action" or end ' - . 'with dynamic part in format "Module:*" or "Module:Presenter:*".' + . 'with dynamic part in format "Module:*" or "Module:Presenter:*".', ); } } diff --git a/src/AssetLoaderException.php b/src/AssetLoaderException.php index 38d8ef5..aeb2032 100644 --- a/src/AssetLoaderException.php +++ b/src/AssetLoaderException.php @@ -12,7 +12,7 @@ public static function routeIsInInvalidFormat(string $route): void throw new self( 'Route "' . $route . '" is invalid. ' . 'Route must be absolute "Module:Presenter:action" or end ' - . 'with dynamic part in format "Module:*" or "Module:Presenter:*".' + . 'with dynamic part in format "Module:*" or "Module:Presenter:*".', ); } } diff --git a/src/Helpers.php b/src/Helpers.php index 84fa91d..c1bccea 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -10,11 +10,10 @@ final class Helpers { - /** @throws \Error */ public function __construct() { - throw new \Error('Class ' . get_class($this) . ' is static and cannot be instantiated.'); + throw new \Error('Class ' . static::class . ' is static and cannot be instantiated.'); } @@ -28,8 +27,11 @@ public static function processPath(Request $httpRequest): string } - public static function formatRoute(string $module, string $presenter = 'Homepage', string $action = 'default'): string - { + public static function formatRoute( + string $module, + string $presenter = 'Homepage', + string $action = 'default' + ): string { return self::firstUpper($module) . ':' . self::firstUpper($presenter) . ':' . $action; } @@ -71,6 +73,8 @@ public static function firstLower(string $s): string */ public static function length(string $s): int { - return function_exists('mb_strlen') ? mb_strlen($s, 'UTF-8') : strlen(utf8_decode($s)); + return function_exists('mb_strlen') + ? mb_strlen($s, 'UTF-8') + : strlen(utf8_decode($s)); } } diff --git a/src/Minifier/DefaultCssMinifier.php b/src/Minifier/DefaultCssMinifier.php index f154d60..4f9e281 100644 --- a/src/Minifier/DefaultCssMinifier.php +++ b/src/Minifier/DefaultCssMinifier.php @@ -12,13 +12,11 @@ public function minify(string $haystack): string $return = (string) preg_replace_callback( '#[ \t\r\n]+|<(/)?(textarea|pre)(?=\W)#i', fn (array $match): string => empty($match[2]) ? ' ' : $match[0], - $haystack + $haystack, ); $return = (string) preg_replace('/(\w|;)\s+({|})\s+(\w|\.|#)/', '$1$2$3', $return); $return = str_replace(';}', '}', $return); $return = (string) preg_replace('/(\w)\s*:\s+(\w|#|-|.)/', '$1:$2', $return); - $return = (string) preg_replace('/\s*\/\*+[^\*]+\*+\/\s*/', '', $return); - - return $return; + return (string) preg_replace('/\s*\/\*+[^\*]+\*+\/\s*/', '', $return); } } diff --git a/src/Minifier/JShrinkMinifier.php b/src/Minifier/JShrinkMinifier.php index c4735a0..a782d2a 100644 --- a/src/Minifier/JShrinkMinifier.php +++ b/src/Minifier/JShrinkMinifier.php @@ -299,7 +299,6 @@ protected function getChar(): ?string * performance benefits as the skipping is done using native functions (ie, * c code) rather than in script php. * - * * @return string Next 'real' character to be processed. * @throws \RuntimeException */ @@ -338,7 +337,9 @@ protected function getReal(): ?string */ protected function processOneLineComments(int $startIndex): void { - $thirdCommentString = $this->index < $this->len ? $this->input[$this->index] : null; + $thirdCommentString = $this->index < $this->len + ? $this->input[$this->index] + : null; // kill rest of line $this->getNext("\n"); @@ -381,12 +382,12 @@ protected function processMultiLineComments(int $startIndex): void $this->a = ' '; // If the comment started on a new line we let it stay on the new line - if ($this->input[($startIndex - 1)] === "\n") { + if ($this->input[$startIndex - 1] === "\n") { echo "\n"; } } - $endPoint = ($this->index - 1) - $startIndex; + $endPoint = $this->index - 1 - $startIndex; echo substr($this->input, $startIndex, $endPoint); $this->c = $char; @@ -553,11 +554,9 @@ protected function lock(string $js): string $this->locks[$lock] = $matches[2]; - $js = (string) preg_replace('/([+-])\s+([+-])/S', '$1' . $lock . '$2', $js); + return (string) preg_replace('/([+-])\s+([+-])/S', '$1' . $lock . '$2', $js); /* -- */ - - return $js; }