From 2546b3f39ff9ad3274442e3a1b41ecff1be2bb05 Mon Sep 17 00:00:00 2001 From: Simon Karlen Date: Thu, 28 Jul 2022 15:47:26 +0200 Subject: [PATCH] bumped to php 7.3 --- composer.json | 109 +++++++++++----------- src/Module.php | 5 +- src/components/Captcha.php | 77 ++++++++------- src/controllers/AudioController.php | 4 +- src/controllers/CaptchaController.php | 5 +- src/controllers/ImageController.php | 5 +- src/validators/VisualCaptchaValidator.php | 5 +- src/widgets/VisualCaptcha.php | 7 +- tests/TestCase.php | 10 +- tests/bootstrap.php | 6 +- tests/compatibility.php | 36 ------- 11 files changed, 122 insertions(+), 147 deletions(-) delete mode 100644 tests/compatibility.php diff --git a/composer.json b/composer.json index 0b37c80..610e6dc 100644 --- a/composer.json +++ b/composer.json @@ -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": "simi.albi@gmail.com" + "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": "simi.albi@gmail.com" + } + ], + "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" - } - } -} \ No newline at end of file +} diff --git a/src/Module.php b/src/Module.php index e2720e1..9b2c102 100644 --- a/src/Module.php +++ b/src/Module.php @@ -23,7 +23,6 @@ class Module extends \simialbi\yii2\base\Module implements BootstrapInterface /** * {@inheritdoc} * @throws \yii\base\InvalidConfigException - * @throws \ReflectionException */ public function init() { @@ -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 */ @@ -63,4 +62,4 @@ public function bootstrap($app) '/' . $this->id . '/audio/' => $this->id . '/audio/index' ]); } -} \ No newline at end of file +} diff --git a/src/components/Captcha.php b/src/components/Captcha.php index 9fe76ef..34886a3 100644 --- a/src/components/Captcha.php +++ b/src/components/Captcha.php @@ -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 = []; @@ -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) ]; @@ -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)]; @@ -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); @@ -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; @@ -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); } @@ -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')); } @@ -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)); @@ -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); @@ -512,4 +521,4 @@ public function getImage($filePath) return $img; } -} \ No newline at end of file +} diff --git a/src/controllers/AudioController.php b/src/controllers/AudioController.php index 71cc2c1..b78c4b8 100644 --- a/src/controllers/AudioController.php +++ b/src/controllers/AudioController.php @@ -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; @@ -52,4 +52,4 @@ public function actionIndex($type = 'mp3', $namespace = null) 'inline' => true ]); } -} \ No newline at end of file +} diff --git a/src/controllers/CaptchaController.php b/src/controllers/CaptchaController.php index cb53f60..f789e43 100644 --- a/src/controllers/CaptchaController.php +++ b/src/controllers/CaptchaController.php @@ -23,7 +23,7 @@ class CaptchaController extends Controller /** * {@inheritdoc} */ - public function behaviors() + public function behaviors(): array { return [ 'access' => [ @@ -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; diff --git a/src/controllers/ImageController.php b/src/controllers/ImageController.php index ba73624..d2ea87d 100644 --- a/src/controllers/ImageController.php +++ b/src/controllers/ImageController.php @@ -29,8 +29,9 @@ class ImageController extends Controller * * @throws NotFoundHttpException * @throws \yii\web\RangeNotSatisfiableHttpException + * @throws \Exception */ - public function actionIndex($index, $retina = false, $namespace = null) + public function actionIndex(int $index, bool $retina = false, ?string $namespace = null): \yii\web\Response { $captcha = $this->module->captcha; $captcha->namespace = $namespace; @@ -58,4 +59,4 @@ public function actionIndex($index, $retina = false, $namespace = null) 'inline' => true ]); } -} \ No newline at end of file +} diff --git a/src/validators/VisualCaptchaValidator.php b/src/validators/VisualCaptchaValidator.php index bee3e3c..2365da5 100644 --- a/src/validators/VisualCaptchaValidator.php +++ b/src/validators/VisualCaptchaValidator.php @@ -59,8 +59,9 @@ public function validateAttribute($model, $attribute) * {@inheritdoc} * @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 + * @throws \Exception */ - protected function validateValue($value, $namespace = null) + protected function validateValue($value, ?string $namespace = null): ?array { $this->captcha->namespace = $namespace; $imageFieldName = trim(str_replace( @@ -86,7 +87,7 @@ protected function validateValue($value, $namespace = null) * Get captcha component * @return \simialbi\yii2\visualcaptcha\components\Captcha */ - protected function getCaptcha() + protected function getCaptcha(): \simialbi\yii2\visualcaptcha\components\Captcha { $module = Yii::$app->getModule($this->moduleId); /* @var $module \simialbi\yii2\visualcaptcha\Module */ diff --git a/src/widgets/VisualCaptcha.php b/src/widgets/VisualCaptcha.php index d3d3616..c7156f8 100644 --- a/src/widgets/VisualCaptcha.php +++ b/src/widgets/VisualCaptcha.php @@ -42,9 +42,8 @@ class VisualCaptcha extends InputWidget /** * {@inheritdoc} - * @throws \ReflectionException */ - public function init() + public function init(): void { parent::init(); @@ -100,7 +99,7 @@ public function init() /** * {@inheritdoc} */ - public function run() + public function run(): string { parent::run(); @@ -112,4 +111,4 @@ public function run() return $html; } -} \ No newline at end of file +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 68f8f2d..1401d83 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -23,7 +23,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase * @param string $expected * @param string $actual */ - public function assertEqualsWithoutLE($expected, $actual) + public function assertEqualsWithoutLE(string $expected, string $actual) { $expected = str_replace("\r\n", "\n", $expected); $actual = str_replace("\r\n", "\n", $actual); @@ -31,7 +31,7 @@ public function assertEqualsWithoutLE($expected, $actual) $this->assertEquals($expected, $actual); } - protected function setUp() + protected function setUp(): void { parent::setUp(); $this->mockWebApplication(); @@ -41,7 +41,7 @@ protected function setUp() * @param array $config * @param string $appClass */ - protected function mockWebApplication($config = [], $appClass = '\yii\web\Application') + protected function mockWebApplication(array $config = [], string $appClass = '\yii\web\Application') { new $appClass(ArrayHelper::merge([ 'id' => 'testapp', @@ -65,7 +65,7 @@ protected function mockWebApplication($config = [], $appClass = '\yii\web\Applic * Clean up after test. * By default the application created with [[mockApplication]] will be destroyed. */ - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); $this->destroyApplication(); @@ -79,4 +79,4 @@ protected function destroyApplication() Yii::$app = null; Yii::$container = new Container(); } -} \ No newline at end of file +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index caad603..91c2623 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,8 +2,8 @@ // ensure we get report on all possible php errors error_reporting(-1); -define('YII_ENABLE_ERROR_HANDLER', false); -define('YII_DEBUG', true); +const YII_ENABLE_ERROR_HANDLER = false; +const YII_DEBUG = true; $_SERVER['SCRIPT_NAME'] = '/' . __DIR__; $_SERVER['SCRIPT_FILENAME'] = __FILE__; @@ -13,5 +13,3 @@ Yii::setAlias('@yiiunit/extensions/visualcaptcha', __DIR__); Yii::setAlias('@simialbi/yii2/visualcaptcha', dirname(__DIR__) . '/src'); - -require_once(__DIR__ . '/compatibility.php'); \ No newline at end of file diff --git a/tests/compatibility.php b/tests/compatibility.php deleted file mode 100644 index 89fdda7..0000000 --- a/tests/compatibility.php +++ /dev/null @@ -1,36 +0,0 @@ -setExpectedException($exception); - } - - /** - * @param string $message - */ - public function expectExceptionMessage($message) - { - $this->setExpectedException($this->getExpectedException(), $message); - } - } - } -} \ No newline at end of file