diff --git a/src/services/EmbedService.php b/src/services/EmbedService.php index f878fe5..4082d7d 100644 --- a/src/services/EmbedService.php +++ b/src/services/EmbedService.php @@ -6,6 +6,7 @@ use craft\base\Component; use craft\helpers\ConfigHelper; use craft\helpers\Template; + use vaersaagod\toolmate\ToolMate; /** @@ -21,6 +22,7 @@ class EmbedService extends Component * @param string $videoUrl * @param array $params * @return array + * @throws \yii\base\InvalidConfigException */ public function getVideoEmbed(string $videoUrl, array $params = []): array { @@ -267,7 +269,7 @@ public function getVideoEmbed(string $videoUrl, array $params = []): array * @param string $videoUrl The video URL. * @return array An array containing the video info (or false) and the response code (or false). */ - public function getVideoInfo($videoUrl): array + public function getVideoInfo(string $videoUrl): array { // do we have curl? if (function_exists('curl_init')) { @@ -307,7 +309,7 @@ public function getVideoInfo($videoUrl): array * @param string $prefix The prefix that keys should start with in order to be returned. * @return array The array of (unprefixed) key => value pairs that matched the specified prefix. */ - private function getPrefixedParams($params = [], $prefix = ''): array + private function getPrefixedParams(array $params = [], string $prefix = ''): array { $prefixedParams = []; @@ -339,7 +341,7 @@ private function getPrefixedParams($params = [], $prefix = ''): array * @param array $pairs An array of key => value pairs * @return string The resulting string. Ex: key=value&key2=value2 */ - private function makeUrlKeyValuePairsString($pairs = []): string + private function makeUrlKeyValuePairsString(array $pairs = []): string { $chunks = []; diff --git a/src/services/MinifyService.php b/src/services/MinifyService.php index 98732ef..3a4af45 100644 --- a/src/services/MinifyService.php +++ b/src/services/MinifyService.php @@ -3,9 +3,12 @@ namespace vaersaagod\toolmate\services; use craft\base\Component; + use MatthiasMullie\Minify\JS; use MatthiasMullie\Minify\CSS; + use vaersaagod\toolmate\ToolMate; + use voku\helper\HtmlMin; /** @@ -22,7 +25,7 @@ class MinifyService extends Component * @param string $content * @return string */ - public function minify($content): string + public function minify(string $content): string { // todo : Parse DOM and minify any script or style tags separately? return $this->html($content); @@ -32,7 +35,7 @@ public function minify($content): string * @param string $content * @return string */ - public function css($content): string + public function css(string $content): string { $settings = ToolMate::$plugin->getSettings(); @@ -49,7 +52,7 @@ public function css($content): string * @param string $content * @return string */ - public function js($content): string + public function js(string $content): string { $settings = ToolMate::$plugin->getSettings(); @@ -66,7 +69,7 @@ public function js($content): string * @param string $content * @return string */ - public function html($content): string + public function html(string $content): string { $settings = ToolMate::$plugin->getSettings(); diff --git a/src/services/ToolService.php b/src/services/ToolService.php index ed2ea2f..5658a85 100644 --- a/src/services/ToolService.php +++ b/src/services/ToolService.php @@ -5,9 +5,9 @@ use Craft; use craft\base\Component; use craft\helpers\FileHelper; + use vaersaagod\toolmate\ToolMate; -use yii\base\Exception; -use yii\base\InvalidConfigException; + use yii\web\Cookie; /** @@ -23,11 +23,11 @@ class ToolService extends Component /** * Inlines local or remote file. * - * @param $fileName + * @param string $fileName * @param bool $remote * @return false|string */ - public function inline($fileName, $remote = false) + public function inline(string $fileName, bool $remote = false) { if ($remote) { if (strpos($fileName, '//') === 0) { @@ -52,12 +52,12 @@ public function inline($fileName, $remote = false) /** * Stamps a file name or path with timestamp or hash. * - * @param $fileName + * @param string $fileName * @param string $mode * @param string $type * @return string */ - public function stamp($fileName, $mode = 'file', $type = 'ts'): string + public function stamp(string $fileName, string $mode = 'file', string $type = 'ts'): string { $documentRoot = ToolMate::getInstance()->getSettings()->publicRoot; $filePath = FileHelper::normalizePath($documentRoot . '/' . $fileName); @@ -92,11 +92,11 @@ public function stamp($fileName, $mode = 'file', $type = 'ts'): string } /** - * @param $params + * @param array $params * @param bool $secure - * @throws Exception + * @throws \Exception */ - public function setCookie($params, $secure = false) + public function setCookie(array $params, bool $secure = false) { $defaults = [ 'name' => '', @@ -113,7 +113,7 @@ public function setCookie($params, $secure = false) $params = array_merge($defaults, $params); if ($params['name'] === '') { - throw new Exception('Parameter `name` is required for setCookie, mate!', __METHOD__); + throw new \Exception('Parameter `name` is required for setCookie, mate!', __METHOD__); } if ($params['value'] === '') { @@ -129,12 +129,9 @@ public function setCookie($params, $secure = false) try { $cookie->value = Craft::$app->security->hashData(base64_encode(serialize($params['value']))); - } catch (InvalidConfigException $e) { + } catch (\Throwable $e) { Craft::error('Error setting secure cookie: ' . $e->getMessage(), __METHOD__); return; - } catch (Exception $e) { - Craft::error('Error setting secure cookie: ' . $e->getMessage(),__METHOD__); - return; } $cookie->expire = $params['expire']; @@ -167,11 +164,11 @@ public function setCookie($params, $secure = false) } /** - * @param $name + * @param string $name * @param bool $secure * @return mixed|string */ - public function getCookie($name, $secure = false) + public function getCookie(string $name, bool $secure = false) { $result = ''; @@ -181,14 +178,11 @@ public function getCookie($name, $secure = false) if ($cookie !== null) { try { $data = Craft::$app->security->validateData($cookie->value); - } catch (InvalidConfigException $e) { + } catch (\Throwable $e) { Craft::error('Error getting secure cookie: ' . $e->getMessage(), __METHOD__); $data = false; - } catch (Exception $e) { - Craft::error('Error getting secure cookie: ' . $e->getMessage(),__METHOD__); - $data = false; } - + if ($cookie && !empty($cookie->value) && $data !== false) { $result = unserialize(base64_decode($data), ['allowed_classes' => false]); } @@ -211,10 +205,10 @@ public function getCookie($name, $secure = false) * Exactly like hash_file except that the result is always numeric * (consisting of characters 0-9 only). * - * @param $filePath + * @param string $filePath * @return string */ - private function numHashFile($filePath): string + private function numHashFile(string $filePath): string { return implode(unpack('C*', hash_file('crc32b', $filePath, true))); } diff --git a/src/twigextensions/ToolMateTwigExtension.php b/src/twigextensions/ToolMateTwigExtension.php index 65fe77d..0ca32f4 100644 --- a/src/twigextensions/ToolMateTwigExtension.php +++ b/src/twigextensions/ToolMateTwigExtension.php @@ -56,9 +56,9 @@ public function getFunctions(): array /** * @param array $params * @param bool $secure - * @throws \yii\base\Exception + * @throws \Exception */ - public function setCookie($params, $secure = false) + public function setCookie(array $params, bool $secure = false): void { ToolMate::$plugin->tool->setCookie($params, $secure); } @@ -68,7 +68,7 @@ public function setCookie($params, $secure = false) * @param bool $secure * @return mixed|string */ - public function getCookie($name, $secure = false) + public function getCookie(string $name, bool $secure = false) { return ToolMate::$plugin->tool->getCookie($name, $secure); } @@ -76,9 +76,10 @@ public function getCookie($name, $secure = false) /** * @param string $url * @param array $params - * @return mixed|string + * @return array + * @throws \Exception */ - public function getVideoEmbed($url, $params = []) + public function getVideoEmbed(string $url, array $params = []): array { return ToolMate::$plugin->embed->getVideoEmbed($url, $params); } @@ -88,18 +89,18 @@ public function getVideoEmbed($url, $params = []) * @param bool $remote * @return false|string */ - public function inline($fileName, $remote = false) + public function inline(string $fileName, bool $remote = false) { return ToolMate::$plugin->tool->inline($fileName, $remote); } /** - * @param $fileName + * @param string $fileName * @param string $mode * @param string $type * @return string */ - public function stamp($fileName, $mode = 'file', $type = 'ts'): string + public function stamp(string $fileName, string $mode = 'file', string $type = 'ts'): string { return ToolMate::$plugin->tool->stamp($fileName, $mode, $type); } diff --git a/src/variables/ToolMateVariable.php b/src/variables/ToolMateVariable.php index 4ef1d90..0764681 100644 --- a/src/variables/ToolMateVariable.php +++ b/src/variables/ToolMateVariable.php @@ -20,11 +20,10 @@ class ToolMateVariable * Inlines local or remote file. * * @param string $fileName - * @param bool $remote - * - * @return string + * @param bool $remote + * @return false|string */ - public function inline($fileName, $remote = false): string + public function inline(string $fileName, bool $remote = false) { return ToolMate::$plugin->tool->inline($fileName, $remote); } @@ -37,7 +36,7 @@ public function inline($fileName, $remote = false): string * @param string $type * @return string */ - public function stamp($fileName, $mode = 'file', $type = 'ts'): string + public function stamp(string $fileName, string $mode = 'file', string $type = 'ts'): string { return ToolMate::$plugin->tool->stamp($fileName, $mode, $type); } @@ -47,7 +46,7 @@ public function stamp($fileName, $mode = 'file', $type = 'ts'): string * @param bool $secure * @throws \yii\base\Exception */ - public function setCookie($params, $secure = false) + public function setCookie(array $params, bool $secure = false): void { ToolMate::$plugin->tool->setCookie($params, $secure); } @@ -57,17 +56,18 @@ public function setCookie($params, $secure = false) * @param bool $secure * @return mixed|string */ - public function getCookie($name, $secure = false) + public function getCookie(string $name, bool $secure = false) { return ToolMate::$plugin->tool->getCookie($name, $secure); } /** - * @param $url + * @param string $url * @param array $params * @return array + * @throws \Exception */ - public function getVideoEmbed($url, $params = []): array + public function getVideoEmbed(string $url, array $params = []): array { return ToolMate::$plugin->embed->getVideoEmbed($url, $params); }