• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / reCAPTCHA / 5447406801

pending completion
5447406801

push

github

f3l1x
Phpstan: fixes

14 of 14 new or added lines in 3 files covered. (100.0%)

115 of 132 relevant lines covered (87.12%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.38
/src/Forms/ReCaptchaField.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\ReCaptcha\Forms;
4

5
use Contributte\ReCaptcha\ReCaptchaProvider;
6
use Nette\Forms\Controls\TextInput;
7
use Nette\Forms\Form;
8
use Nette\Forms\Rules;
9
use Nette\Utils\Html;
10

11
class ReCaptchaField extends TextInput
12
{
13

14
        private ReCaptchaProvider $provider;
15

16
        private bool $configured = false;
17

18
        private ?string $message = null;
19

20
        public function __construct(ReCaptchaProvider $provider, ?string $label = null, ?string $message = null)
1✔
21
        {
22
                parent::__construct($label);
1✔
23

24
                $this->provider = $provider;
1✔
25

26
                $this->setOmitted(true);
1✔
27
                $this->control = Html::el('div');
1✔
28
                $this->control->addClass('g-recaptcha');
1✔
29

30
                $this->message = $message;
1✔
31
        }
1✔
32

33
        public function loadHttpData(): void
34
        {
35
                $form = $this->getForm();
1✔
36
                assert($form !== null);
1✔
37
                $this->setValue($form->getHttpData(Form::DATA_TEXT, ReCaptchaProvider::FORM_PARAMETER));
1✔
38
        }
1✔
39

40
        public function setMessage(string $message): self
41
        {
42
                $this->message = $message;
×
43

44
                return $this;
×
45
        }
46

47
        public function validate(): void
48
        {
49
                $this->configureValidation();
×
50

51
                parent::validate();
×
52
        }
53

54
        public function getRules(): Rules
55
        {
56
                $this->configureValidation();
1✔
57

58
                return parent::getRules();
1✔
59
        }
60

61
        public function verify(): bool
62
        {
63
                return $this->provider->validateControl($this) === true;
×
64
        }
65

66
        public function getControl(): Html
67
        {
68
                $this->configureValidation();
1✔
69

70
                $el = parent::getControl();
1✔
71
                $el->addAttributes([
1✔
72
                        'id' => $this->getHtmlId(),
1✔
73
                        'name' => $this->getHtmlName(),
1✔
74
                        'data-sitekey' => $this->provider->getSiteKey(),
1✔
75
                ]);
76

77
                return $el;
1✔
78
        }
79

80
        private function configureValidation(): void
81
        {
82
                if ($this->configured) {
1✔
83
                        return;
1✔
84
                }
85

86
                $this->configured = true;
1✔
87
                $message = $this->message ?? 'Are you a bot?';
1✔
88
                $this->addRule(fn ($code): bool => $this->verify() === true, $message);
1✔
89
        }
1✔
90

91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc