Skip to content

Commit

Permalink
Fix Coding standard by current Nette rules
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Jan 29, 2021
1 parent a3eb4b3 commit 46c2abb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
28 changes: 20 additions & 8 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)),
));
}

Expand Down Expand Up @@ -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)) . '"?',
);
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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],
);
}
}
Expand Down Expand Up @@ -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:*".',
);
}
}
2 changes: 1 addition & 1 deletion src/AssetLoaderException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:*".',
);
}
}
14 changes: 9 additions & 5 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}


Expand All @@ -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;
}

Expand Down Expand Up @@ -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));
}
}
6 changes: 2 additions & 4 deletions src/Minifier/DefaultCssMinifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 6 additions & 7 deletions src/Minifier/JShrinkMinifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}


Expand Down

0 comments on commit 46c2abb

Please sign in to comment.