Skip to content

Commit

Permalink
bumped to php 7.3
Browse files Browse the repository at this point in the history
  • Loading branch information
simialbi committed Jul 28, 2022
1 parent 61af529 commit 2546b3f
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 147 deletions.
109 changes: 56 additions & 53 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
{
"name": "simialbi/yii2-visualcaptcha",
"description": "Visual captcha implementation for yii2 framework",
"keywords": [
"yii2",
"visualcaptcha",
"visual",
"captcha",
"widget"
],
"type": "yii2-extension",
"license": "MIT",
"support": {
"issues": "https://github.com/simialbi/yii2-visualcaptcha/issues",
"source": "https://github.com/simialbi/yii2-visualcaptcha"
},
"authors": [
{
"name": "Simon Karlen",
"email": "[email protected]"
"name": "simialbi/yii2-visualcaptcha",
"description": "Visual captcha implementation for yii2 framework",
"keywords": [
"yii2",
"visualcaptcha",
"visual",
"captcha",
"widget"
],
"type": "yii2-extension",
"license": "MIT",
"support": {
"issues": "https://github.com/simialbi/yii2-visualcaptcha/issues",
"source": "https://github.com/simialbi/yii2-visualcaptcha"
},
"authors": [
{
"name": "Simon Karlen",
"email": "[email protected]"
}
],
"prefer-stable": true,
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
],
"require": {
"php": ">=7.3",
"yiisoft/yii2": "^2.0.15",
"simialbi/yii2-simialbi-base": ">=0.13.1 <1.0 | ^1.0.0"
},
"require-dev": {
"yiisoft/yii2-coding-standards": "~2.0",
"phpunit/phpunit": "^9.5.21"
},
"suggest": {
"ext-openssl": "Needed in captcha component in utilRandomHex method if yii method fails"
},
"config": {
"process-timeout": 1800,
"allow-plugins": {
"yiisoft/yii2-composer": true
}
},
"autoload": {
"psr-4": {
"simialbi\\yii2\\visualcaptcha\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"yiiunit\\extensions\\visualcaptcha\\": "tests"
}
}
],
"prefer-stable": true,
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
],
"require": {
"php": ">=5.6",
"yiisoft/yii2": "^2.0.15",
"simialbi/yii2-simialbi-base": ">=0.5 <1.0 | ^1.0.0"
},
"require-dev": {
"yiisoft/yii2-coding-standards": "~2.0",
"phpunit/phpunit": "< 7"
},
"suggest": {
"ext-openssl": "Needed in captcha component in utilRandomHex method if yii method fails"
},
"config": {
"process-timeout": 1800
},
"autoload": {
"psr-4": {
"simialbi\\yii2\\visualcaptcha\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"yiiunit\\extensions\\visualcaptcha\\": "tests"
}
}
}
}
5 changes: 2 additions & 3 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Module extends \simialbi\yii2\base\Module implements BootstrapInterface
/**
* {@inheritdoc}
* @throws \yii\base\InvalidConfigException
* @throws \ReflectionException
*/
public function init()
{
Expand All @@ -41,7 +40,7 @@ public function init()
* @return null|components\Captcha
* @throws \yii\base\InvalidConfigException
*/
public function getCaptcha()
public function getCaptcha(): ?components\Captcha
{
$captcha = $this->get('captcha');
/* @var components\Captcha $captcha */
Expand All @@ -63,4 +62,4 @@ public function bootstrap($app)
'/' . $this->id . '/audio/<type:(mp3|ogg)>' => $this->id . '/audio/index'
]);
}
}
}
77 changes: 43 additions & 34 deletions src/components/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@ class Captcha extends Component
/**
* Generate a new valid option
* @param integer $numberOfOptions Number of options. This parameter is optional. Defaults to 5.
* @throws \Exception
*/
public function generate($numberOfOptions = 5)
public function generate(int $numberOfOptions = 5)
{
$imageValues = [];

Expand Down Expand Up @@ -311,22 +312,28 @@ public function generate($numberOfOptions = 5)

do {
$newImageOption = $this->utilArraySample($this->sessionImageOptions);
} while ($oldImageOption && ArrayHelper::getValue($oldImageOption,
'path') === ArrayHelper::getValue($newImageOption, 'path'));
} while (
$oldImageOption &&
ArrayHelper::getValue($oldImageOption, 'path') === ArrayHelper::getValue($newImageOption, 'path')
);

$this->validImageOption = $newImageOption;

do {
$newAudioOption = $this->utilArraySample($this->audioOptions);
} while ($oldAudioOption && ArrayHelper::getValue($oldAudioOption,
'path') === ArrayHelper::getValue($newAudioOption, 'path'));
} while (
$oldAudioOption &&
ArrayHelper::getValue($oldAudioOption, 'path') === ArrayHelper::getValue($newAudioOption, 'path')
);

$this->validAudioOption = $newAudioOption;

$this->frontendData = [
'values' => $imageValues,
'imageName' => Yii::t('simialbi/visualcaptcha/names',
ArrayHelper::getValue($this->validImageOption, 'name')),
'imageName' => Yii::t(
'simialbi/visualcaptcha/names',
ArrayHelper::getValue($this->validImageOption, 'name')
),
'imageFieldName' => $this->namespace ? $this->namespace . '[' . $this->utilRandomHex(10) . ']' : $this->utilRandomHex(10),
'audioFieldName' => $this->namespace ? $this->namespace . '[' . $this->utilRandomHex(10) . ']' : $this->utilRandomHex(10)
];
Expand All @@ -338,7 +345,7 @@ public function generate($numberOfOptions = 5)
* @param integer $count
* @return array
*/
private function utilArraySample($arr, $count = 1)
private function utilArraySample(array $arr, int $count = 1): array
{
if ($count == 1) {
return $arr[array_rand($arr)];
Expand All @@ -362,7 +369,7 @@ private function utilArraySample($arr, $count = 1)
* @param integer $count
* @return string
*/
private function utilRandomHex($count)
private function utilRandomHex(int $count): string
{
try {
$bytes = Yii::$app->security->generateRandomKey($count);
Expand All @@ -375,35 +382,35 @@ private function utilRandomHex($count)

/**
* Get data to be used by the frontend
* @return array
* @return array|null
*/
public function getFrontendData()
public function getFrontendData(): ?array
{
return Yii::$app->session->get($this->sessionPrefix . 'frontendData');
}

/**
* Set data to be used by the frontend
* @param array $frontendData
* @param array|null $frontendData
*/
public function setFrontendData($frontendData)
public function setFrontendData(?array $frontendData)
{
Yii::$app->session->set($this->sessionPrefix . 'frontendData', $frontendData);
}

/**
* @return string
* @return string|null
*/
public function getNamespace()
public function getNamespace(): ?string
{
return $this->_namespace;
}

/**
*
* @param string $namespace
* @param string|null $namespace
*/
public function setNamespace($namespace)
public function setNamespace(?string $namespace)
{
$this->_namespace = $namespace;

Expand All @@ -417,54 +424,54 @@ public function setNamespace($namespace)

/**
* Get the current validImageOption
* @return array
* @return array|null
*/
public function getValidImageOption()
public function getValidImageOption(): ?array
{
return Yii::$app->session->get($this->sessionPrefix . 'validImageOption');
}

/**
* Set the current validImageOption
* @param array $validImageOption
* @param array|null $validImageOption
*/
public function setValidImageOption($validImageOption)
public function setValidImageOption(?array $validImageOption)
{
Yii::$app->session->set($this->sessionPrefix . 'validImageOption', $validImageOption);
}

/**
* Get the current validAudioOption
* @return array
* @return array|null
*/
public function getValidAudioOption()
public function getValidAudioOption(): ?array
{
return Yii::$app->session->get($this->sessionPrefix . 'validAudioOption');
}

/**
* Get the current validAudioOption
* @param array $validAudioOption
* @param array|null $validAudioOption
*/
public function setValidAudioOption($validAudioOption)
public function setValidAudioOption(?array $validAudioOption)
{
Yii::$app->session->set($this->sessionPrefix . 'validAudioOption', $validAudioOption);
}

/**
* Return generated image options
* @return array
* @return array|null
*/
public function getSessionImageOptions()
public function getSessionImageOptions(): ?array
{
return Yii::$app->session->get($this->sessionPrefix . 'images');
}

/**
* Set generated image options
* @param array $images
* @param array|null $images
*/
public function setSessionImageOptions($images)
public function setSessionImageOptions(?array $images)
{
Yii::$app->session->set($this->sessionPrefix . 'images', $images);
}
Expand All @@ -473,8 +480,9 @@ public function setSessionImageOptions($images)
* Validate the sent image value with the validImageOption
* @param string $sentOption
* @return boolean
* @throws \Exception
*/
public function validateImage($sentOption)
public function validateImage(string $sentOption): bool
{
return ($sentOption == ArrayHelper::getValue($this->validImageOption, 'value'));
}
Expand All @@ -483,8 +491,9 @@ public function validateImage($sentOption)
* Validate the sent audio value with the validAudioOption
* @param string $sentOption
* @return boolean
* @throws \Exception
*/
public function validateAudio($sentOption)
public function validateAudio(string $sentOption): bool
{
$validAnswers = explode('|', strtoupper(ArrayHelper::getValue($this->validAudioOption, 'value')));
return (in_array(strtoupper($sentOption), $validAnswers));
Expand All @@ -493,9 +502,9 @@ public function validateAudio($sentOption)
/**
* Return the image string from cache to avoid I/O
* @param string $filePath
* @return bool|string
* @return string
*/
public function getImage($filePath)
public function getImage(string $filePath): string
{
if (Yii::$app->cache) {
$cacheKey = md5($filePath);
Expand All @@ -512,4 +521,4 @@ public function getImage($filePath)

return $img;
}
}
}
4 changes: 2 additions & 2 deletions src/controllers/AudioController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AudioController extends Controller
* @return \yii\web\Response
* @throws NotFoundHttpException
*/
public function actionIndex($type = 'mp3', $namespace = null)
public function actionIndex(string $type = 'mp3', ?string $namespace = null): \yii\web\Response
{
$captcha = $this->module->captcha;
$captcha->namespace = $namespace;
Expand All @@ -52,4 +52,4 @@ public function actionIndex($type = 'mp3', $namespace = null)
'inline' => true
]);
}
}
}
5 changes: 3 additions & 2 deletions src/controllers/CaptchaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CaptchaController extends Controller
/**
* {@inheritdoc}
*/
public function behaviors()
public function behaviors(): array
{
return [
'access' => [
Expand Down Expand Up @@ -58,8 +58,9 @@ public function behaviors()
* @param string|null $namespace string the value of the parameter sent to the server for the namespace,
* if it's not set up, no namespace will be sent
* @return array
* @throws \Exception
*/
public function actionIndex($howMany = 5, $namespace = null)
public function actionIndex(int $howMany = 5, ?string $namespace = null): array
{
$captcha = $this->module->captcha;
$captcha->namespace = $namespace;
Expand Down
Loading

0 comments on commit 2546b3f

Please sign in to comment.