From 747bac236b1510bb11440c8234f6f6bce2ad9589 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Sun, 24 May 2026 18:57:36 +0200 Subject: [PATCH 1/7] Apply conservative Rector cleanup --- src/Controller/Admin/CaptchaController.php | 6 +++--- src/Controller/Admin/IpsController.php | 4 ++-- src/Controller/CaptchaController.php | 2 +- src/Engine/Math/SimpleMath.php | 6 +----- src/Engine/MathEngine.php | 15 +++++---------- src/Model/Behavior/CaptchaBehavior.php | 14 +++----------- src/Model/Behavior/PassiveCaptchaBehavior.php | 2 +- src/View/Helper/CaptchaHelper.php | 3 +-- tests/bootstrap.php | 2 +- tests/schema.php | 2 +- 10 files changed, 19 insertions(+), 37 deletions(-) diff --git a/src/Controller/Admin/CaptchaController.php b/src/Controller/Admin/CaptchaController.php index 27269b7..bd2e412 100644 --- a/src/Controller/Admin/CaptchaController.php +++ b/src/Controller/Admin/CaptchaController.php @@ -44,7 +44,7 @@ public function index(): void { $heatmap = $this->buildHeatmap(); $snapshot = $this->buildSnapshot(); - $this->set(compact('tiles24h', 'tiles7d', 'throttledIps', 'heatmap', 'snapshot')); + $this->set(['tiles24h' => $tiles24h, 'tiles7d' => $tiles7d, 'throttledIps' => $throttledIps, 'heatmap' => $heatmap, 'snapshot' => $snapshot]); } /** @@ -53,7 +53,7 @@ public function index(): void { public function config(): void { $config = (array)Configure::read('Captcha'); - $this->set(compact('config')); + $this->set(['config' => $config]); } /** @@ -63,7 +63,7 @@ public function engine(): void { $engines = $this->knownEngines(); $activeClass = (string)Configure::read('Captcha.engine', MathEngine::class); - $this->set(compact('engines', 'activeClass')); + $this->set(['engines' => $engines, 'activeClass' => $activeClass]); } /** diff --git a/src/Controller/Admin/IpsController.php b/src/Controller/Admin/IpsController.php index 9db6c95..025975b 100644 --- a/src/Controller/Admin/IpsController.php +++ b/src/Controller/Admin/IpsController.php @@ -46,7 +46,7 @@ public function index(): void { $failed = $this->topIps($since, false); $throttled = $this->throttledIps(); - $this->set(compact('issued', 'solved', 'failed', 'throttled', 'window')); + $this->set(['issued' => $issued, 'solved' => $solved, 'failed' => $failed, 'throttled' => $throttled, 'window' => $window]); } /** @@ -78,7 +78,7 @@ public function view(?string $ip = null): void { } } - $this->set(compact('ip', 'captchas', 'summary')); + $this->set(['ip' => $ip, 'captchas' => $captchas, 'summary' => $summary]); } /** diff --git a/src/Controller/CaptchaController.php b/src/Controller/CaptchaController.php index 75f7c91..a17b99e 100644 --- a/src/Controller/CaptchaController.php +++ b/src/Controller/CaptchaController.php @@ -63,7 +63,7 @@ public function display($id = null) { } $captcha = $this->Preparer->prepare($captcha); - $this->set(compact('captcha')); + $this->set(['captcha' => $captcha]); $this->viewBuilder()->setClassName('Captcha.Captcha'); $this->viewBuilder()->setTemplatePath('Captcha'); diff --git a/src/Engine/Math/SimpleMath.php b/src/Engine/Math/SimpleMath.php index a875ff2..f09550f 100644 --- a/src/Engine/Math/SimpleMath.php +++ b/src/Engine/Math/SimpleMath.php @@ -57,11 +57,7 @@ public function getExpression(): string { public function getValue(): string { $operator = $this->data[1]; - if ($operator === '-') { - $value = $this->data[0] - $this->data[2]; - } else { - $value = $this->data[0] + $this->data[2]; - } + $value = $operator === '-' ? $this->data[0] - $this->data[2] : $this->data[0] + $this->data[2]; return (string)$value; } diff --git a/src/Engine/MathEngine.php b/src/Engine/MathEngine.php index 4d5ed27..17680f5 100644 --- a/src/Engine/MathEngine.php +++ b/src/Engine/MathEngine.php @@ -83,16 +83,11 @@ protected function render($expression) { $formula = new expression_math(tableau_expression($expression)); $formula->dessine($this->_config['size']); ob_start(); - switch ($this->_config['imageFormat']) { - case static::FORMAT_JPEG: - imagejpeg($formula->image); - - break; - case static::FORMAT_PNG: - imagepng($formula->image); - - break; - } + match ($this->_config['imageFormat']) { + static::FORMAT_JPEG => imagejpeg($formula->image), + static::FORMAT_PNG => imagepng($formula->image), + default => ob_get_clean() ?: '', + }; return ob_get_clean() ?: ''; } diff --git a/src/Model/Behavior/CaptchaBehavior.php b/src/Model/Behavior/CaptchaBehavior.php index 51d8620..e007c91 100644 --- a/src/Model/Behavior/CaptchaBehavior.php +++ b/src/Model/Behavior/CaptchaBehavior.php @@ -197,11 +197,7 @@ public function validateCaptchaMinTime($value, $context) { if (!$captcha) { return false; } - if ($captcha->created >= new DateTime('- ' . $this->getConfig('minTime') . ' seconds')) { - return false; - } - - return true; + return $captcha->created < new DateTime('- ' . $this->getConfig('minTime') . ' seconds'); } /** @@ -215,11 +211,7 @@ public function validateCaptchaMaxTime($value, $context) { if (!$captcha) { return false; } - if ($captcha->created <= new DateTime('- ' . $this->getConfig('maxTime') . ' seconds')) { - return false; - } - - return true; + return $captcha->created > new DateTime('- ' . $this->getConfig('maxTime') . ' seconds'); } /** @@ -259,7 +251,7 @@ public function validateCaptchaResult($value, $context) { * @return \Captcha\Model\Entity\Captcha|null */ protected function _getCaptcha(array $data) { - $uuid = !empty($data['captcha_uuid']) ? (string)$data['captcha_uuid'] : null; + $uuid = empty($data['captcha_uuid']) ? null : (string)$data['captcha_uuid']; if ($uuid && array_key_exists($uuid, $this->_captchas)) { $captcha = $this->_captchas[$uuid]; diff --git a/src/Model/Behavior/PassiveCaptchaBehavior.php b/src/Model/Behavior/PassiveCaptchaBehavior.php index 049c5ea..4c41c2a 100644 --- a/src/Model/Behavior/PassiveCaptchaBehavior.php +++ b/src/Model/Behavior/PassiveCaptchaBehavior.php @@ -62,7 +62,7 @@ public function addPassiveCaptchaValidation(Validator $validator): void { 'rule' => function ($value) { $ok = $value === ''; if (!$ok && $this->_config['log']) { - Log::write('info', 'PassiveCaptcha trigger, field value `' . (string)$value . '`'); + Log::write('info', 'PassiveCaptcha trigger, field value `' . $value . '`'); } return $ok; diff --git a/src/View/Helper/CaptchaHelper.php b/src/View/Helper/CaptchaHelper.php index 8cf4563..38b45c3 100644 --- a/src/View/Helper/CaptchaHelper.php +++ b/src/View/Helper/CaptchaHelper.php @@ -87,9 +87,8 @@ public function render(array $options = []) { $html = $this->control($options); } $html .= $this->Form->control('captcha_uuid', ['type' => 'hidden', 'value' => $id ?? '']); - $html .= $this->passive(); - return $html; + return $html . $this->passive(); } /** diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d7d26b3..bb89dfb 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -14,7 +14,7 @@ define('DS', DIRECTORY_SEPARATOR); } if (!defined('WINDOWS')) { - if (DS === '\\' || substr(PHP_OS, 0, 3) === 'WIN') { + if (DS === '\\' || str_starts_with(PHP_OS, 'WIN')) { define('WINDOWS', true); } else { define('WINDOWS', false); diff --git a/tests/schema.php b/tests/schema.php index 5ec5ed6..3352fa7 100644 --- a/tests/schema.php +++ b/tests/schema.php @@ -21,7 +21,7 @@ $tableObject = (new ReflectionClass($class))->getProperty('table'); $tableName = $tableObject->getDefaultValue(); - } catch (ReflectionException $e) { + } catch (ReflectionException) { continue; } From 12b7bbe9179f79ef3a7d95bc91ae8b9213819588 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Sun, 24 May 2026 19:08:56 +0200 Subject: [PATCH 2/7] Run PHPCS cleanup --- src/Engine/MathEngine.php | 8 ++++---- src/Model/Behavior/CaptchaBehavior.php | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Engine/MathEngine.php b/src/Engine/MathEngine.php index 17680f5..1294cd5 100644 --- a/src/Engine/MathEngine.php +++ b/src/Engine/MathEngine.php @@ -84,10 +84,10 @@ protected function render($expression) { $formula->dessine($this->_config['size']); ob_start(); match ($this->_config['imageFormat']) { - static::FORMAT_JPEG => imagejpeg($formula->image), - static::FORMAT_PNG => imagepng($formula->image), - default => ob_get_clean() ?: '', - }; + static::FORMAT_JPEG => imagejpeg($formula->image), + static::FORMAT_PNG => imagepng($formula->image), + default => ob_get_clean() ?: '', + }; return ob_get_clean() ?: ''; } diff --git a/src/Model/Behavior/CaptchaBehavior.php b/src/Model/Behavior/CaptchaBehavior.php index e007c91..e8e4f85 100644 --- a/src/Model/Behavior/CaptchaBehavior.php +++ b/src/Model/Behavior/CaptchaBehavior.php @@ -197,7 +197,8 @@ public function validateCaptchaMinTime($value, $context) { if (!$captcha) { return false; } - return $captcha->created < new DateTime('- ' . $this->getConfig('minTime') . ' seconds'); + + return $captcha->created < new DateTime('- ' . $this->getConfig('minTime') . ' seconds'); } /** @@ -211,7 +212,8 @@ public function validateCaptchaMaxTime($value, $context) { if (!$captcha) { return false; } - return $captcha->created > new DateTime('- ' . $this->getConfig('maxTime') . ' seconds'); + + return $captcha->created > new DateTime('- ' . $this->getConfig('maxTime') . ' seconds'); } /** From c7974b69017f840f337244d289953229685f58f4 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Sun, 24 May 2026 19:51:19 +0200 Subject: [PATCH 3/7] Fix output buffer handling --- src/Engine/MathEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engine/MathEngine.php b/src/Engine/MathEngine.php index 1294cd5..b4a5059 100644 --- a/src/Engine/MathEngine.php +++ b/src/Engine/MathEngine.php @@ -86,7 +86,7 @@ protected function render($expression) { match ($this->_config['imageFormat']) { static::FORMAT_JPEG => imagejpeg($formula->image), static::FORMAT_PNG => imagepng($formula->image), - default => ob_get_clean() ?: '', + default => null, }; return ob_get_clean() ?: ''; From 6885c72e4c1082417faefadf7ad432c644f4d22e Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Mon, 25 May 2026 04:12:03 +0200 Subject: [PATCH 4/7] Restore compact() for matching view vars --- src/Controller/Admin/CaptchaController.php | 2 +- src/Controller/CaptchaController.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controller/Admin/CaptchaController.php b/src/Controller/Admin/CaptchaController.php index bd2e412..fd9dd0d 100644 --- a/src/Controller/Admin/CaptchaController.php +++ b/src/Controller/Admin/CaptchaController.php @@ -53,7 +53,7 @@ public function index(): void { public function config(): void { $config = (array)Configure::read('Captcha'); - $this->set(['config' => $config]); + $this->set(compact('config')); } /** diff --git a/src/Controller/CaptchaController.php b/src/Controller/CaptchaController.php index a17b99e..75f7c91 100644 --- a/src/Controller/CaptchaController.php +++ b/src/Controller/CaptchaController.php @@ -63,7 +63,7 @@ public function display($id = null) { } $captcha = $this->Preparer->prepare($captcha); - $this->set(['captcha' => $captcha]); + $this->set(compact('captcha')); $this->viewBuilder()->setClassName('Captcha.Captcha'); $this->viewBuilder()->setTemplatePath('Captcha'); From b08ac53c4416660509b9e194fa22f0da10dea0d8 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Mon, 25 May 2026 05:05:07 +0200 Subject: [PATCH 5/7] Restore compact() for matching view vars --- src/Controller/Admin/CaptchaController.php | 4 ++-- src/Controller/Admin/IpsController.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Controller/Admin/CaptchaController.php b/src/Controller/Admin/CaptchaController.php index fd9dd0d..27269b7 100644 --- a/src/Controller/Admin/CaptchaController.php +++ b/src/Controller/Admin/CaptchaController.php @@ -44,7 +44,7 @@ public function index(): void { $heatmap = $this->buildHeatmap(); $snapshot = $this->buildSnapshot(); - $this->set(['tiles24h' => $tiles24h, 'tiles7d' => $tiles7d, 'throttledIps' => $throttledIps, 'heatmap' => $heatmap, 'snapshot' => $snapshot]); + $this->set(compact('tiles24h', 'tiles7d', 'throttledIps', 'heatmap', 'snapshot')); } /** @@ -63,7 +63,7 @@ public function engine(): void { $engines = $this->knownEngines(); $activeClass = (string)Configure::read('Captcha.engine', MathEngine::class); - $this->set(['engines' => $engines, 'activeClass' => $activeClass]); + $this->set(compact('engines', 'activeClass')); } /** diff --git a/src/Controller/Admin/IpsController.php b/src/Controller/Admin/IpsController.php index 025975b..9db6c95 100644 --- a/src/Controller/Admin/IpsController.php +++ b/src/Controller/Admin/IpsController.php @@ -46,7 +46,7 @@ public function index(): void { $failed = $this->topIps($since, false); $throttled = $this->throttledIps(); - $this->set(['issued' => $issued, 'solved' => $solved, 'failed' => $failed, 'throttled' => $throttled, 'window' => $window]); + $this->set(compact('issued', 'solved', 'failed', 'throttled', 'window')); } /** @@ -78,7 +78,7 @@ public function view(?string $ip = null): void { } } - $this->set(['ip' => $ip, 'captchas' => $captchas, 'summary' => $summary]); + $this->set(compact('ip', 'captchas', 'summary')); } /** From ff0d3445b9c09ca66b9a21980cf20ee08d4a9ea9 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Mon, 25 May 2026 05:14:59 +0200 Subject: [PATCH 6/7] Fix cleanup branch static analysis issues --- src/Controller/CaptchaController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Controller/CaptchaController.php b/src/Controller/CaptchaController.php index 75f7c91..3f788ef 100644 --- a/src/Controller/CaptchaController.php +++ b/src/Controller/CaptchaController.php @@ -59,6 +59,7 @@ public function display($id = null) { if ($id === null) { $captcha = new Captcha(); } else { + /** @var \Captcha\Model\Entity\Captcha $captcha */ $captcha = $this->Captchas->find()->where(['uuid' => (string)$id])->firstOrFail(); } $captcha = $this->Preparer->prepare($captcha); From 2bb20a671ecbf3d645566a04214be1d8a4cd380d Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Mon, 25 May 2026 14:42:31 +0200 Subject: [PATCH 7/7] Stringify passive captcha log value --- src/Model/Behavior/PassiveCaptchaBehavior.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Behavior/PassiveCaptchaBehavior.php b/src/Model/Behavior/PassiveCaptchaBehavior.php index 4c41c2a..049c5ea 100644 --- a/src/Model/Behavior/PassiveCaptchaBehavior.php +++ b/src/Model/Behavior/PassiveCaptchaBehavior.php @@ -62,7 +62,7 @@ public function addPassiveCaptchaValidation(Validator $validator): void { 'rule' => function ($value) { $ok = $value === ''; if (!$ok && $this->_config['log']) { - Log::write('info', 'PassiveCaptcha trigger, field value `' . $value . '`'); + Log::write('info', 'PassiveCaptcha trigger, field value `' . (string)$value . '`'); } return $ok;