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

Increase PHPStan level #162

Merged
merged 3 commits into from
Dec 6, 2023
Merged
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
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'declare_strict_types' => true,
])
->setFinder($finder)
;
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ help:

cs-lint: ## Verify check styles
composer install --working-dir=tools/php-cs-fixer
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff --config=.php_cs.dist.php
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff

cs-fix: ## Apply Check styles
composer install --working-dir=tools/php-cs-fixer
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --diff --config=.php_cs.dist.php
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --diff

phpstan: ## Run PHPStan
composer install --working-dir=tools/phpstan
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#2 \\$regionCallingFrom of method libphonenumber\\\\PhoneNumberUtil\\:\\:formatOutOfCountryCallingNumber\\(\\) expects string, string\\|null given\\.$#"
count: 1
path: src/Templating/Helper/PhoneNumberHelper.php
10 changes: 8 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
includes:
- phpstan-baseline.neon
- tools/phpstan/vendor/jangregor/phpstan-prophecy/extension.neon

parameters:
level: 1
level: 8
paths:
- src
- tests
inferPrivatePropertyTypeFromConstructor: true

ignoreErrors:
- '#Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition::children\(\).#'
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony2 PhoneNumberBundle.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of the file header update?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea!

I discovered recently that's is something supported by php-cs : https://github.com/symfony/symfony/blob/7.1/.php-cs-fixer.dist.php#L16

Maybe we can open an issue / PR in order to decide of a header we want to have in all files?

*
Expand All @@ -19,17 +21,20 @@
*/
class FormTwigTemplateCompilerPass implements CompilerPassInterface
{
private $phoneNumberLayout = '@MisdPhoneNumber/Form/phone_number.html.twig';
private $phoneNumberBootstrapLayout = '@MisdPhoneNumber/Form/phone_number_bootstrap.html.twig';
private $phoneNumberBootstrap4Layout = '@MisdPhoneNumber/Form/phone_number_bootstrap_4.html.twig';
private $phoneNumberBootstrap5Layout = '@MisdPhoneNumber/Form/phone_number_bootstrap_5.html.twig';
private string $phoneNumberLayout = '@MisdPhoneNumber/Form/phone_number.html.twig';
private string $phoneNumberBootstrapLayout = '@MisdPhoneNumber/Form/phone_number_bootstrap.html.twig';
private string $phoneNumberBootstrap4Layout = '@MisdPhoneNumber/Form/phone_number_bootstrap_4.html.twig';
private string $phoneNumberBootstrap5Layout = '@MisdPhoneNumber/Form/phone_number_bootstrap_5.html.twig';

public function process(ContainerBuilder $container): void
{
if (false === $container->hasParameter('twig.form.resources')) {
return;
}

/**
* @var string[] $parameter
*/
$parameter = $container->getParameter('twig.form.resources');

if (\in_array($this->phoneNumberLayout, $parameter)) {
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony2 PhoneNumberBundle.
*
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/MisdPhoneNumberExtension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony2 PhoneNumberBundle.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Doctrine/DBAL/Types/PhoneNumberType.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony2 PhoneNumberBundle.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony2 PhoneNumberBundle.
*
Expand Down
22 changes: 14 additions & 8 deletions src/Form/DataTransformer/PhoneNumberToArrayTransformer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony2 PhoneNumberBundle.
*
Expand All @@ -19,30 +21,33 @@
use Symfony\Component\Form\Exception\TransformationFailedException;

/**
* Phone number to array transformer.
* @implements DataTransformerInterface<PhoneNumber, array{country: string, number: string}>
*/
class PhoneNumberToArrayTransformer implements DataTransformerInterface
{
/**
* @var array
* @var string[]
*/
private $countryChoices;
private array $countryChoices;

/**
* Constructor.
* @param string[] $countryChoices
*/
public function __construct(array $countryChoices)
{
$this->countryChoices = $countryChoices;
}

public function transform($value): array
/**
* @return array{country: string, number: string}
*/
public function transform(mixed $value): array
{
if (null === $value) {
return ['country' => '', 'number' => ''];
}

if (false === $value instanceof PhoneNumber) {
if (!$value instanceof PhoneNumber) {
throw new TransformationFailedException('Expected a \libphonenumber\PhoneNumber.');
}

Expand All @@ -53,12 +58,12 @@ public function transform($value): array
}

return [
'country' => $util->getRegionCodeForNumber($value),
'country' => (string) $util->getRegionCodeForNumber($value),
'number' => $util->format($value, PhoneNumberFormat::NATIONAL),
];
}

public function reverseTransform($value): ?PhoneNumber
public function reverseTransform(mixed $value): ?PhoneNumber
{
if (!$value) {
return null;
Expand All @@ -68,6 +73,7 @@ public function reverseTransform($value): ?PhoneNumber
throw new TransformationFailedException('Expected an array.');
}

/* @phpstan-ignore-next-line */
if ('' === trim($value['number'] ?? '')) {
return null;
}
Expand Down
31 changes: 8 additions & 23 deletions src/Form/DataTransformer/PhoneNumberToStringTransformer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony2 PhoneNumberBundle.
*
Expand All @@ -19,33 +21,16 @@
use Symfony\Component\Form\Exception\TransformationFailedException;

/**
* Phone number to string transformer.
* @implements DataTransformerInterface<PhoneNumber, string>
*/
class PhoneNumberToStringTransformer implements DataTransformerInterface
{
/**
* Default region code.
*
* @var string
*/
private $defaultRegion;

/**
* Display format.
*
* @var int
*/
private $format;
private string $defaultRegion;
private int $format;

/**
* Constructor.
*
* @param string $defaultRegion default region code
* @param int $format display format
*/
public function __construct(
$defaultRegion = PhoneNumberUtil::UNKNOWN_REGION,
$format = PhoneNumberFormat::INTERNATIONAL
string $defaultRegion = PhoneNumberUtil::UNKNOWN_REGION,
int $format = PhoneNumberFormat::INTERNATIONAL
) {
$this->defaultRegion = $defaultRegion;
$this->format = $format;
Expand All @@ -57,7 +42,7 @@ public function transform($value): string
return '';
}

if (false === $value instanceof PhoneNumber) {
if (!$value instanceof PhoneNumber) {
throw new TransformationFailedException('Expected a \libphonenumber\PhoneNumber.');
}

Expand Down
14 changes: 13 additions & 1 deletion src/Form/Type/PhoneNumberType.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony2 PhoneNumberBundle.
*
Expand Down Expand Up @@ -67,10 +69,20 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
continue;
}

$label = $this->formatDisplayChoice($options['country_display_type'], $intlCountries[$regionCode], $regionCode, $countryCode, $options['country_display_emoji_flag']);
$label = $this->formatDisplayChoice(
$options['country_display_type'],
$intlCountries[$regionCode],
(string) $regionCode,
(string) $countryCode,
$options['country_display_emoji_flag']
);

$countryChoices[$label] = $regionCode;
}

/**
* @var string[] $transformerChoices
*/
$transformerChoices = array_values($countryChoices);

$countryOptions = array_replace([
Expand Down
2 changes: 2 additions & 0 deletions src/MisdPhoneNumberBundle.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony2 PhoneNumberBundle.
*
Expand Down
47 changes: 20 additions & 27 deletions src/Serializer/Normalizer/PhoneNumberNormalizer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony2 PhoneNumberBundle.
*
Expand All @@ -25,58 +27,46 @@
*/
class PhoneNumberNormalizer implements NormalizerInterface, DenormalizerInterface
{
/**
* Region code.
*
* @var string
*/
private $region;

/**
* Display format.
*
* @var int
*/
private $format;
private PhoneNumberUtil $phoneNumberUtil;
private string $region;
private int $format;

/**
* Display format.
*
* @var PhoneNumberUtil
*/
private $phoneNumberUtil;

/**
* Constructor.
*
* @param PhoneNumberUtil $phoneNumberUtil phone number utility
* @param string $region region code
* @param int $format display format
*/
public function __construct(PhoneNumberUtil $phoneNumberUtil, $region = PhoneNumberUtil::UNKNOWN_REGION, $format = PhoneNumberFormat::E164)
public function __construct(PhoneNumberUtil $phoneNumberUtil, string $region = PhoneNumberUtil::UNKNOWN_REGION, int $format = PhoneNumberFormat::E164)
{
$this->phoneNumberUtil = $phoneNumberUtil;
$this->region = $region;
$this->format = $format;
}

/**
* @param array<mixed> $context
*
* @throws InvalidArgumentException
*/
public function normalize($object, $format = null, array $context = []): string
public function normalize(mixed $object, string $format = null, array $context = []): string
{
return $this->phoneNumberUtil->format($object, $this->format);
}

public function supportsNormalization($data, $format = null, array $context = []): bool
/**
* @param array<mixed> $context
*/
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
{
return $data instanceof PhoneNumber;
}

/**
* @param array<mixed> $context
*
* @throws UnexpectedValueException
*/
public function denormalize($data, $type, $format = null, array $context = []): ?PhoneNumber
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): ?PhoneNumber
{
if (null === $data) {
return null;
Expand All @@ -89,7 +79,10 @@ public function denormalize($data, $type, $format = null, array $context = []):
}
}

public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
/**
* @param array<mixed> $context
*/
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
{
return PhoneNumber::class === $type && \is_string($data);
}
Expand Down
Loading