Skip to content

Commit

Permalink
PHPStan 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
curry684 committed Jan 16, 2025
1 parent 92b6c67 commit 3c8182c
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 18 deletions.
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
"symfony/yaml": "^6.3|^7.0"
},
"require-dev": {
"ekino/phpstan-banned-code": "^2.1",
"friendsofphp/php-cs-fixer": "^3.65.0",
"infection/infection": "^0.29.6",
"ekino/phpstan-banned-code": "^3.0",
"friendsofphp/php-cs-fixer": "^3.68.0",
"infection/infection": "^0.29.10",
"phpstan/extension-installer": "^1.4.3",
"phpstan/phpstan": "^1.12.13",
"phpstan/phpstan-phpunit": "^1.4.2",
"phpstan/phpstan-symfony": "^1.4.12",
"phpunit/phpunit": "^10.5.15 || ^11.5.1",
"phpstan/phpstan": "^2.1.1",
"phpstan/phpstan-phpunit": "^2.0.3",
"phpstan/phpstan-symfony": "^2.0.1",
"phpunit/phpunit": "^10.5.15 || ^11.5.3",
"symfony/browser-kit": "^6.3|^7.2.0",
"symfony/css-selector": "^6.3|^7.2.0",
"symfony/debug-bundle": "^6.3|^7.2.0",
Expand All @@ -43,7 +43,7 @@
"symfony/routing": "^6.3|^7.2.0",
"symfony/runtime": "^6.3|^7.2.0",
"symfony/twig-bundle": "^6.3|^7.2.0",
"symfony/web-profiler-bundle": "^6.3|^7.2.0"
"symfony/web-profiler-bundle": "^6.3|^7.2.2"
},
"conflict": {
"monolog/monolog": "<3",
Expand Down
5 changes: 2 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ parameters:
level: max
paths:
- src
- tests
excludePaths:
- tests/Fixture/var
- tests/Fixture/vendor
# Obsolete code
- src/Command/StatisticsCommand.php
4 changes: 2 additions & 2 deletions src/AntiSpam.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public function __construct(
public function getProfile(string $name): Profile
{
$id = "antispam.profile.$name";
if (!$this->profiles->has($id) || (!($profile = $this->profiles->get($id)) instanceof Profile)) {
if (!$this->profiles->has($id)) {
throw new InvalidProfileException(sprintf('There is no antispam profile "%s" defined, did you use the correct profile name from your antispam.yaml configuration file?', $name));
}

return $profile;
return $this->profiles->get($id);
}

public function disable(): void
Expand Down
5 changes: 3 additions & 2 deletions src/DependencyInjection/AntiSpamExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
use Symfony\Component\DependencyInjection\Reference;

/**
* @phpstan-type AntiSpamProfile array{passive: ?bool, banned_markup: array<string, mixed>, url_count: array<string, mixed>, banned_phrases: array<string, mixed>, banned_scripts: array<string, mixed>}
* @phpstan-type AntiSpamConfiguration array{enabled: bool, passive: bool, profiles: array<string, AntiSpamProfile>}
* @infection-ignore-all As infection cannot clear caches reliably mutating the extension has no effect
*/
class AntiSpamExtension extends Extension implements PrependExtensionInterface
{
/**
* Note that constraints are added in order of increasing cost/effectiveness balance so the resulting array
* can be used Sequentially efficiently. Iow: add new costly ones at the end, cheap ones up front.
*
* @var array<string, class-string>
*/
public const CONFIG_KEY_TO_VALIDATOR_MAPPING = [
'banned_markup' => BannedMarkup::class,
Expand All @@ -50,6 +50,7 @@ public function load(array $configs, ContainerBuilder $container): void
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$loader->load('services.yaml');

/** @var AntiSpamConfiguration $mergedConfig */
$mergedConfig = $this->processConfiguration(new Configuration(), $configs);
$container->setParameter('antispam.enabled', $mergedConfig['enabled']);
foreach ($mergedConfig['profiles'] as $name => $profile) {
Expand Down
4 changes: 4 additions & 0 deletions src/EventSubscriber/FormProfileSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function onPreSetData(FormEvent $event): void
$type = $config->getType();
while (null !== $type) {
if ($type->getInnerType() instanceof TextType) {
/** @var array{constraints: Constraint[], ...} $options */
$options = $config->getOptions();
$this->applyTextTypeProfile($options);
$event->getForm()->add($name, $config->getType()->getInnerType()::class, $options);
Expand Down Expand Up @@ -154,6 +155,9 @@ protected function applyTextTypeProfile(array &$options): void
}
}

/**
* @param FormInterface<mixed> $form
*/
private static function uniqueFieldName(FormInterface $form, string $basename): string
{
$field = $basename;
Expand Down
13 changes: 11 additions & 2 deletions src/Form/AntiSpamFormResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@

class AntiSpamFormResult
{
/** @var array<FormError> */
/** @var FormError[] */
private array $antiSpamErrors = [];

/** @var array<FormError> */
/** @var FormError[] */
private array $formErrors = [];

/**
* @param FormInterface<mixed> $form
*/
public function __construct(
private readonly FormInterface $form,
private readonly ?Request $request = null,
Expand All @@ -50,6 +53,9 @@ public function clearAntiSpamErrors(): void
$this->recursiveClearAntiSpamErrors($this->form);
}

/**
* @param FormInterface<mixed> $form
*/
private function recursiveClearAntiSpamErrors(FormInterface $form): void
{
if ($form instanceof ClearableErrorsInterface) {
Expand All @@ -66,6 +72,9 @@ private function recursiveClearAntiSpamErrors(FormInterface $form): void
}
}

/**
* @return FormInterface<mixed>
*/
public function getForm(): FormInterface
{
return $this->form;
Expand Down
8 changes: 8 additions & 0 deletions src/Form/Type/AbstractAntiSpamType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
use Omines\AntiSpamBundle\Form\AntiSpamFormError;
use Omines\AntiSpamBundle\Profile;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @extends AbstractType<FormType>
*/
abstract class AbstractAntiSpamType extends AbstractType
{
protected AntiSpam $antiSpam;
Expand All @@ -47,6 +51,7 @@ public function setTranslator(TranslatorInterface $translator): void
}

/**
* @param FormInterface<mixed> $form
* @param array<string, mixed> $parameters
*/
protected function createFormError(FormInterface $form, string $template, array $parameters = [], ?string $cause = null): void
Expand All @@ -60,6 +65,9 @@ protected function createFormError(FormInterface $form, string $template, array
$form->addError(new AntiSpamFormError($message, $template, $parameters, cause: $cause));
}

/**
* @param FormInterface<mixed> $form
*/
private static function getProfile(FormInterface $form): ?Profile
{
do {
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Type/SubmitTimerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
}

$currentIp = $this->requestStack->getMainRequest()?->getClientIp() ?? self::NO_IP;
if (null !== $currentIp && $ip !== $currentIp) {
if ($ip !== $currentIp) {
$this->createFormError($form, 'form.timer.mismatch_ip', cause: "The client IP address changed from $ip to $currentIp");
}

Expand Down
4 changes: 4 additions & 0 deletions src/Validator/Constraints/BannedScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class BannedScripts extends AntiSpamConstraint
{
/**
* @var string
* @todo Make a typed constant when PHP <8.3 support is dropped
*/
public const MINIMUM_PCRE_VERSION = '10.40';

/** @var Script[] */
Expand Down

0 comments on commit 3c8182c

Please sign in to comment.