Skip to content

Commit

Permalink
🎨 add phpstan config
Browse files Browse the repository at this point in the history
  • Loading branch information
garak committed Dec 12, 2021
1 parent 63460c9 commit f083bb9
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 18 deletions.
13 changes: 7 additions & 6 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.git* export-ignore
.php-cs-fixer.* export-ignore
docs/ export-ignore
phpunit.xml.dist export-ignore
README.md export-ignore
tests/ export-ignore
/.git* export-ignore
/.php-cs-fixer.* export-ignore
/docs/ export-ignore
/phpstan* export-ignore
/phpunit.xml.dist export-ignore
/README.md export-ignore
/tests/ export-ignore
12 changes: 12 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:children\\(\\)\\.$#"
count: 1
path: src/DependencyInjection/Configuration.php

-
message: "#^Else branch is unreachable because previous condition is always true\\.$#"
count: 1
path: src/Recaptcha/RecaptchaVerifier.php

7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: 6
paths:
- src
- tests
includes:
- phpstan-baseline.neon
2 changes: 1 addition & 1 deletion src/DependencyInjection/BeelabRecaptcha2Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('services.xml');
}

private function getRequestMethod($requestMethod): string
private function getRequestMethod(string $requestMethod): string
{
switch ($requestMethod) {
case 'curl_post':
Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/Compiler/TwigFormPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public function process(ContainerBuilder $container): void
}

$reflection = new \ReflectionClass(BeelabRecaptcha2Bundle::class);
$path = \dirname($reflection->getFileName()).'/../templates';
$path = \dirname((string) $reflection->getFileName()).'/../templates';
$loaderDefinition->addMethodCall('addPath', [$path]);

$container->setParameter('twig.form.resources', \array_merge(
['form_fields.html.twig'],
$container->getParameter('twig.form.resources')
(array) $container->getParameter('twig.form.resources')
));
}
}
5 changes: 5 additions & 0 deletions src/Recaptcha/RecaptchaVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Beelab\Recaptcha2Bundle\Recaptcha;

use ReCaptcha\ReCaptcha;
use ReCaptcha\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

Expand Down Expand Up @@ -50,6 +51,10 @@ public function verify(?string $recaptchaValue = null): void
$recaptchaValue = $request->request->get(self::GOOGLE_DEFAULT_INPUT);
}

if (null === $recaptchaValue || !is_string($recaptchaValue)) {
throw new RecaptchaException(new Response(false));
}

$response = $this->reCaptcha->verify($recaptchaValue, $request->getClientIp());
if (!$response->isSuccess()) {
throw new RecaptchaException($response);
Expand Down
5 changes: 4 additions & 1 deletion src/Validator/Constraints/Recaptcha2Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ public function __construct(RecaptchaVerifier $verifier)
$this->verifier = $verifier;
}

/**
* @param string|null $value
* @param Recaptcha2 $constraint
*/
public function validate($value, Constraint $constraint): void
{
try {
$this->verifier->verify($value);
} catch (RecaptchaException $e) {
/* @var Recaptcha2 $constraint */
$this->context->addViolation($constraint->message);
}
}
Expand Down
16 changes: 8 additions & 8 deletions tests/DependencyInjection/BeelabRecaptcha2ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public function testLoadSetParameters(): void

$extension = new BeelabRecaptcha2Extension();
$configs = [
['request_method' => 'curl_post'],
['site_key' => 'foo'],
['secret' => 'bar'],
['enabled' => true],
'one' => ['request_method' => 'curl_post'],
'two' => ['site_key' => 'foo'],
'three' => ['secret' => 'bar'],
'four' => ['enabled' => true],
];
$extension->load($configs, $container);
self::assertInstanceOf(BeelabRecaptcha2Extension::class, $extension);
Expand All @@ -45,10 +45,10 @@ public function testLoadSetParametersPost(): void

$extension = new BeelabRecaptcha2Extension();
$configs = [
['request_method' => 'post'],
['site_key' => 'foo'],
['secret' => 'bar'],
['enabled' => true],
'one' => ['request_method' => 'post'],
'two' => ['site_key' => 'foo'],
'three' => ['secret' => 'bar'],
'four' => ['enabled' => true],
];
$extension->load($configs, $container);
self::assertInstanceOf(BeelabRecaptcha2Extension::class, $extension);
Expand Down
3 changes: 3 additions & 0 deletions tests/Recaptcha/RecaptchaVerifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@

final class RecaptchaVerifierTest extends TestCase
{
/** @var \PHPUnit\Framework\MockObject\MockObject|ReCaptcha */
protected $recaptcha;

/** @var \PHPUnit\Framework\MockObject\MockObject|Request */
protected $request;

/** @var \PHPUnit\Framework\MockObject\MockObject|RequestStack */
protected $stack;

protected function setUp(): void
Expand Down

0 comments on commit f083bb9

Please sign in to comment.