Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints to the functions #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"no-captcha",
"captcha",
"laravel",
"laravel4",
"laravel5",
"laravel6"
"laravel8",
"laravel9",
"laravel10"
],
"license": "MIT",
"authors": [
Expand All @@ -18,12 +18,12 @@
}
],
"require": {
"php": ">=5.5.5",
"illuminate/support": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0",
"guzzlehttp/guzzle": "^6.2|^7.0"
"php": ">=8.0.0",
"illuminate/support": "^8.0|^9.0|^10.0",
"guzzlehttp/guzzle": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "~4.8|^9.5.10"
"phpunit/phpunit": "^9.5.10"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 1 addition & 3 deletions src/Facades/NoCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ class NoCaptcha extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
protected static function getFacadeAccessor(): string
{
return 'captcha';
}
Expand Down
85 changes: 17 additions & 68 deletions src/NoCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,28 @@ class NoCaptcha

/**
* The recaptcha secret key.
*
* @var string
*/
protected $secret;
protected string $secret;

/**
* The recaptcha sitekey key.
*
* @var string
*/
protected $sitekey;
protected string $sitekey;

/**
* @var \GuzzleHttp\Client
* GuzzleHttp client
*/
protected $http;
protected Client $http;

/**
* The cached verified responses.
*
* @var array
*/
protected $verifiedResponses = [];
protected array $verifiedResponses = [];

/**
* NoCaptcha.
*
* @param string $secret
* @param string $sitekey
* @param array $options
*/
public function __construct($secret, $sitekey, $options = [])
public function __construct(string $secret, string $sitekey, array $options = [])
{
$this->secret = $secret;
$this->sitekey = $sitekey;
Expand All @@ -52,12 +42,8 @@ public function __construct($secret, $sitekey, $options = [])

/**
* Render HTML captcha.
*
* @param array $attributes
*
* @return string
*/
public function display($attributes = [])
public function display(array $attributes = []): string
{
$attributes = $this->prepareAttributes($attributes);
return '<div' . $this->buildAttributes($attributes) . '></div>';
Expand All @@ -66,7 +52,7 @@ public function display($attributes = [])
/**
* @see display()
*/
public function displayWidget($attributes = [])
public function displayWidget(array $attributes = []): string
{
return $this->display($attributes);
}
Expand All @@ -77,10 +63,8 @@ public function displayWidget($attributes = [])
* @param string $formIdentifier the html ID of the form that should be submitted.
* @param string $text the text inside the form button
* @param array $attributes array of additional html elements
*
* @return string
*/
public function displaySubmit($formIdentifier, $text = 'submit', $attributes = [])
public function displaySubmit(string $formIdentifier, string $text = 'submit', array $attributes = []): string
{
$javascript = '';
if (!isset($attributes['data-callback'])) {
Expand All @@ -102,26 +86,16 @@ public function displaySubmit($formIdentifier, $text = 'submit', $attributes = [

/**
* Render js source
*
* @param null $lang
* @param bool $callback
* @param string $onLoadClass
* @return string
*/
public function renderJs($lang = null, $callback = false, $onLoadClass = 'onloadCallBack')
public function renderJs(?string $lang = null, bool $callback = false, string $onLoadClass = 'onloadCallBack'): string
{
return '<script src="'.$this->getJsLink($lang, $callback, $onLoadClass).'" async defer></script>'."\n";
}

/**
* Verify no-captcha response.
*
* @param string $response
* @param string $clientIp
*
* @return bool
*/
public function verifyResponse($response, $clientIp = null)
public function verifyResponse(string $response, ?string $clientIp = null): bool
{
if (empty($response)) {
return false;
Expand Down Expand Up @@ -150,12 +124,8 @@ public function verifyResponse($response, $clientIp = null)

/**
* Verify no-captcha response by Symfony Request.
*
* @param Request $request
*
* @return bool
*/
public function verifyRequest(Request $request)
public function verifyRequest(Request $request): bool
{
return $this->verifyResponse(
$request->get('g-recaptcha-response'),
Expand All @@ -165,13 +135,8 @@ public function verifyRequest(Request $request)

/**
* Get recaptcha js link.
*
* @param string $lang
* @param boolean $callback
* @param string $onLoadClass
* @return string
*/
public function getJsLink($lang = null, $callback = false, $onLoadClass = 'onloadCallBack')
public function getJsLink(?string $lang = null, bool $callback = false, string $onLoadClass = 'onloadCallBack'): string
{
$client_api = static::CLIENT_API;
$params = [];
Expand All @@ -182,24 +147,16 @@ public function getJsLink($lang = null, $callback = false, $onLoadClass = 'onloa
return $client_api . '?'. http_build_query($params);
}

/**
* @param $params
* @param $onLoadClass
*/
protected function setCallBackParams(&$params, $onLoadClass)
protected function setCallBackParams(array &$params, string $onLoadClass): void
{
$params['render'] = 'explicit';
$params['onload'] = $onLoadClass;
}

/**
* Send verify request.
*
* @param array $query
*
* @return array
*/
protected function sendRequestVerify(array $query = [])
protected function sendRequestVerify(array $query = []): array
{
$response = $this->http->request('POST', static::VERIFY_URL, [
'form_params' => $query,
Expand All @@ -210,12 +167,8 @@ protected function sendRequestVerify(array $query = [])

/**
* Prepare HTML attributes and assure that the correct classes and attributes for captcha are inserted.
*
* @param array $attributes
*
* @return array
*/
protected function prepareAttributes(array $attributes)
protected function prepareAttributes(array $attributes): array
{
$attributes['data-sitekey'] = $this->sitekey;
if (!isset($attributes['class'])) {
Expand All @@ -228,12 +181,8 @@ protected function prepareAttributes(array $attributes)

/**
* Build HTML attributes.
*
* @param array $attributes
*
* @return string
*/
protected function buildAttributes(array $attributes)
protected function buildAttributes(array $attributes): string
{
$html = [];

Expand Down
10 changes: 4 additions & 6 deletions src/NoCaptchaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class NoCaptchaServiceProvider extends ServiceProvider
/**
* Bootstrap the application events.
*/
public function boot()
public function boot(): void
{
$app = $this->app;

Expand All @@ -36,7 +36,7 @@ public function boot()
/**
* Booting configure.
*/
protected function bootConfig()
protected function bootConfig(): void
{
$path = __DIR__.'/config/captcha.php';

Expand All @@ -50,7 +50,7 @@ protected function bootConfig()
/**
* Register the service provider.
*/
public function register()
public function register(): void
{
$this->app->singleton('captcha', function ($app) {
return new NoCaptcha(
Expand All @@ -63,10 +63,8 @@ public function register()

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
public function provides(): array
{
return ['captcha'];
}
Expand Down
12 changes: 6 additions & 6 deletions tests/NoCaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ class NoCaptchaTest extends PHPUnit_Framework_TestCase
*/
private $captcha;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->captcha = new NoCaptcha('{secret-key}', '{site-key}');
}

public function testRequestShouldWorks()
public function testRequestShouldWorks(): void
{
$response = $this->captcha->verifyResponse('should_false');
}

public function testJsLink()
public function testJsLink(): void
{
$this->assertTrue($this->captcha instanceof NoCaptcha);

Expand All @@ -33,7 +33,7 @@ public function testJsLink()
$this->assertEquals($withCallback, $this->captcha->renderJs(null, true, 'myOnloadCallback'));
}

public function testDisplay()
public function testDisplay(): void
{
$this->assertTrue($this->captcha instanceof NoCaptcha);

Expand All @@ -44,7 +44,7 @@ public function testDisplay()
$this->assertEquals($withAttrs, $this->captcha->display(['data-theme' => 'light']));
}

public function testdisplaySubmit()
public function testdisplaySubmit(): void
{
$this->assertTrue($this->captcha instanceof NoCaptcha);

Expand All @@ -57,7 +57,7 @@ public function testdisplaySubmit()
$this->assertEquals($withAttrs . $javascript, $withAttrsResult);
}

public function testdisplaySubmitWithCustomCallback()
public function testdisplaySubmitWithCustomCallback(): void
{
$this->assertTrue($this->captcha instanceof NoCaptcha);

Expand Down