Skip to content

Commit

Permalink
A bit of housekeeping, adds missing type declarations and makes sure …
Browse files Browse the repository at this point in the history
…phpdoc blocks are correct
  • Loading branch information
mmikkel committed Oct 5, 2021
1 parent ba21e6a commit 202e8d5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 47 deletions.
8 changes: 5 additions & 3 deletions src/services/EmbedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use craft\base\Component;
use craft\helpers\ConfigHelper;
use craft\helpers\Template;

use vaersaagod\toolmate\ToolMate;

/**
Expand All @@ -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
{
Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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 = [];

Expand Down
11 changes: 7 additions & 4 deletions src/services/MinifyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
Expand All @@ -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();

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

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

Expand Down
40 changes: 17 additions & 23 deletions src/services/ToolService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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' => '',
Expand All @@ -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'] === '') {
Expand All @@ -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'];
Expand Down Expand Up @@ -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 = '';

Expand All @@ -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]);
}
Expand All @@ -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)));
}
Expand Down
17 changes: 9 additions & 8 deletions src/twigextensions/ToolMateTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -68,17 +68,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 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);
}
Expand All @@ -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);
}
Expand Down
18 changes: 9 additions & 9 deletions src/variables/ToolMateVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 202e8d5

Please sign in to comment.