From 2a104052ac1b09786a0dd20d3cae113d16269a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Felix=20=C5=A0ulc?= Date: Wed, 15 Nov 2023 12:50:11 +0100 Subject: [PATCH] Code: add types --- src/DI/FormBinder.php | 4 +--- src/DI/WordchaExtension.php | 7 +++---- src/DataSource/Pair.php | 6 ++---- src/DataSource/QuestionDataSource.php | 7 ++++--- src/Form/WordchaContainer.php | 29 ++++++++++++++++++--------- src/Generator/Security.php | 6 ++---- src/Generator/WordchaGenerator.php | 6 ++---- src/Validator/WordchaValidator.php | 3 +-- src/WordchaFactory.php | 3 +-- src/WordchaUniqueFactory.php | 3 +-- 10 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/DI/FormBinder.php b/src/DI/FormBinder.php index 4ee84e0..c03f8ae 100644 --- a/src/DI/FormBinder.php +++ b/src/DI/FormBinder.php @@ -13,9 +13,7 @@ public static function bind(Factory $factory): void { Container::extensionMethod( 'addWordcha', - function (Container $container, string $name = 'captcha', string $label = 'Captcha') use ($factory): WordchaContainer { - return $container[$name] = new WordchaContainer($factory); - } + fn (Container $container, string $name = 'captcha', string $label = 'Captcha'): WordchaContainer => $container[$name] = new WordchaContainer($factory) // @phpcs:ignore ); } diff --git a/src/DI/WordchaExtension.php b/src/DI/WordchaExtension.php index f7ded0a..821f020 100644 --- a/src/DI/WordchaExtension.php +++ b/src/DI/WordchaExtension.php @@ -10,7 +10,7 @@ use Contributte\Wordcha\WordchaUniqueFactory; use Nette\DI\CompilerExtension; use Nette\PhpGenerator\ClassType; -use Nette\PhpGenerator\PhpLiteral; +use Nette\PhpGenerator\Literal; use Nette\Schema\Expect; use Nette\Schema\Schema; use Nette\Utils\AssertionException; @@ -30,8 +30,7 @@ final class WordchaExtension extends CompilerExtension self::DATASOURCE_QUESTIONS, ]; - /** @var bool */ - private $debugMode; + private bool $debugMode; public function __construct(bool $debugMode = false) { @@ -85,7 +84,7 @@ public function afterCompile(ClassType $class): void $method->addBody( '?::bind($this->getService(?));', [ - new PhpLiteral(FormBinder::class), + new Literal(FormBinder::class), $this->prefix('factory'), ] ); diff --git a/src/DataSource/Pair.php b/src/DataSource/Pair.php index 243ec29..85820d6 100644 --- a/src/DataSource/Pair.php +++ b/src/DataSource/Pair.php @@ -5,11 +5,9 @@ class Pair { - /** @var string */ - private $question; + private string $question; - /** @var string */ - private $answer; + private string $answer; public function __construct(string $question, string $answer) { diff --git a/src/DataSource/QuestionDataSource.php b/src/DataSource/QuestionDataSource.php index 8068a25..9d4fd22 100644 --- a/src/DataSource/QuestionDataSource.php +++ b/src/DataSource/QuestionDataSource.php @@ -8,11 +8,11 @@ class QuestionDataSource implements DataSource { - /** @var string[] Pairs of question:answer */ - private $questions; + /** @var array Pairs of question:answer */ + private array $questions; /** - * @param string[] $questions + * @param array $questions */ public function __construct(array $questions) { @@ -30,6 +30,7 @@ public function get(): Pair $question = array_rand($this->questions); $answer = $this->questions[$question]; + return new Pair($question, $answer); } diff --git a/src/Form/WordchaContainer.php b/src/Form/WordchaContainer.php index abe307b..29cb228 100644 --- a/src/Form/WordchaContainer.php +++ b/src/Form/WordchaContainer.php @@ -8,16 +8,15 @@ use Nette\Forms\Container; use Nette\Forms\Controls\HiddenField; use Nette\Forms\Controls\TextInput; +use Nette\Forms\Form; use Nette\Utils\Strings; class WordchaContainer extends Container { - /** @var Validator */ - private $validator; + private Validator $validator; - /** @var Generator */ - private $generator; + private Generator $generator; public function __construct(Factory $factory) { @@ -37,19 +36,31 @@ public function __construct(Factory $factory) public function getQuestion(): TextInput { - return $this['question']; + $control = $this->getComponent('question'); + assert($control instanceof TextInput); + + return $control; } public function getHash(): HiddenField { - return $this['hash']; + $control = $this->getComponent('hash'); + assert($control instanceof HiddenField); + + return $control; } public function verify(): bool { - $form = $this->getForm(true); - $hash = $form->getHttpData($form::DATA_LINE, $this->getHash()->getHtmlName()); - $answer = $form->getHttpData($form::DATA_LINE, $this->getQuestion()->getHtmlName()); + /** @var Form $form */ + $form = $this->getForm(); + + /** @var string $hash */ + $hash = $form->getHttpData(Form::DataLine, $this->getHash()->getHtmlName()); + + /** @var string $answer */ + $answer = $form->getHttpData(Form::DataLine, $this->getQuestion()->getHtmlName()); + $answer = Strings::lower($answer); return $this->validator->validate($answer, $hash); diff --git a/src/Generator/Security.php b/src/Generator/Security.php index 2aa3c0b..0f5c2d2 100644 --- a/src/Generator/Security.php +++ b/src/Generator/Security.php @@ -5,11 +5,9 @@ class Security { - /** @var string */ - private $question; + private string $question; - /** @var string */ - private $hash; + private string $hash; public function __construct(string $question, string $hash) { diff --git a/src/Generator/WordchaGenerator.php b/src/Generator/WordchaGenerator.php index a6e0893..e504394 100644 --- a/src/Generator/WordchaGenerator.php +++ b/src/Generator/WordchaGenerator.php @@ -7,11 +7,9 @@ class WordchaGenerator implements Generator { - /** @var DataSource */ - private $dataSource; + private DataSource $dataSource; - /** @var string|null */ - private $uniqueKey; + private ?string $uniqueKey = null; public function __construct(DataSource $dataSource) { diff --git a/src/Validator/WordchaValidator.php b/src/Validator/WordchaValidator.php index ed8e823..55a5946 100644 --- a/src/Validator/WordchaValidator.php +++ b/src/Validator/WordchaValidator.php @@ -7,8 +7,7 @@ class WordchaValidator implements Validator { - /** @var Generator */ - private $generator; + private Generator $generator; public function __construct(Generator $generator) { diff --git a/src/WordchaFactory.php b/src/WordchaFactory.php index 5e2e13d..8410db4 100644 --- a/src/WordchaFactory.php +++ b/src/WordchaFactory.php @@ -11,8 +11,7 @@ class WordchaFactory implements Factory { - /** @var DataSource */ - private $dataSource; + private DataSource $dataSource; public function __construct(DataSource $dataSource) { diff --git a/src/WordchaUniqueFactory.php b/src/WordchaUniqueFactory.php index 0e6346b..b17b4b9 100644 --- a/src/WordchaUniqueFactory.php +++ b/src/WordchaUniqueFactory.php @@ -9,8 +9,7 @@ class WordchaUniqueFactory extends WordchaFactory { - /** @var string */ - private $uniqueKey; + private string $uniqueKey; public function __construct(DataSource $dataSource, string $uniqueKey) {