Skip to content

Commit

Permalink
Code: add types
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Nov 15, 2023
1 parent 285c97c commit 2a10405
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 37 deletions.
4 changes: 1 addition & 3 deletions src/DI/FormBinder.php
Expand Up @@ -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
);
}

Expand Down
7 changes: 3 additions & 4 deletions src/DI/WordchaExtension.php
Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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'),
]
);
Expand Down
6 changes: 2 additions & 4 deletions src/DataSource/Pair.php
Expand Up @@ -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)
{
Expand Down
7 changes: 4 additions & 3 deletions src/DataSource/QuestionDataSource.php
Expand Up @@ -8,11 +8,11 @@
class QuestionDataSource implements DataSource
{

/** @var string[] Pairs of question:answer */
private $questions;
/** @var array<string, string> Pairs of question:answer */
private array $questions;

/**
* @param string[] $questions
* @param array<string, string> $questions
*/
public function __construct(array $questions)
{
Expand All @@ -30,6 +30,7 @@ public function get(): Pair

$question = array_rand($this->questions);
$answer = $this->questions[$question];

return new Pair($question, $answer);
}

Expand Down
29 changes: 20 additions & 9 deletions src/Form/WordchaContainer.php
Expand Up @@ -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)
{
Expand All @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions src/Generator/Security.php
Expand Up @@ -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)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Generator/WordchaGenerator.php
Expand Up @@ -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)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Validator/WordchaValidator.php
Expand Up @@ -7,8 +7,7 @@
class WordchaValidator implements Validator
{

/** @var Generator */
private $generator;
private Generator $generator;

public function __construct(Generator $generator)
{
Expand Down
3 changes: 1 addition & 2 deletions src/WordchaFactory.php
Expand Up @@ -11,8 +11,7 @@
class WordchaFactory implements Factory
{

/** @var DataSource */
private $dataSource;
private DataSource $dataSource;

public function __construct(DataSource $dataSource)
{
Expand Down
3 changes: 1 addition & 2 deletions src/WordchaUniqueFactory.php
Expand Up @@ -9,8 +9,7 @@
class WordchaUniqueFactory extends WordchaFactory
{

/** @var string */
private $uniqueKey;
private string $uniqueKey;

public function __construct(DataSource $dataSource, string $uniqueKey)
{
Expand Down

0 comments on commit 2a10405

Please sign in to comment.