Skip to content

Commit

Permalink
Bump phpstan level
Browse files Browse the repository at this point in the history
  • Loading branch information
odolbeau committed Dec 4, 2023
1 parent b32eff1 commit 1d248a4
Show file tree
Hide file tree
Showing 23 changed files with 462 additions and 462 deletions.
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
Expand Up @@ -19,17 +19,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
20 changes: 12 additions & 8 deletions src/Form/DataTransformer/PhoneNumberToArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,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 +56,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 +71,7 @@ public function reverseTransform($value): ?PhoneNumber
throw new TransformationFailedException('Expected an array.');
}

/* @phpstan-ignore-next-line */
if ('' === trim($value['number'] ?? '')) {
return null;
}
Expand Down
29 changes: 6 additions & 23 deletions src/Form/DataTransformer/PhoneNumberToStringTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,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;
private string $defaultRegion;
private int $format;

/**
* Display format.
*
* @var int
*/
private $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 +40,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
12 changes: 11 additions & 1 deletion src/Form/Type/PhoneNumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,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
45 changes: 18 additions & 27 deletions src/Serializer/Normalizer/PhoneNumberNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,46 @@
*/
class PhoneNumberNormalizer implements NormalizerInterface, DenormalizerInterface
{
/**
* Region code.
*
* @var string
*/
private $region;

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

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

/**
* 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 +77,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
14 changes: 2 additions & 12 deletions src/Templating/Helper/PhoneNumberHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,8 @@
*/
class PhoneNumberHelper
{
/**
* Phone number utility.
*
* @var PhoneNumberUtil
*/
protected $phoneNumberUtil;
protected PhoneNumberUtil $phoneNumberUtil;

/**
* Constructor.
*
* @param PhoneNumberUtil $phoneNumberUtil phone number utility
*/
public function __construct(PhoneNumberUtil $phoneNumberUtil)
{
$this->phoneNumberUtil = $phoneNumberUtil;
Expand Down Expand Up @@ -62,7 +52,7 @@ public function format(PhoneNumber|string $phoneNumber, string|int $format = Pho
* @param PhoneNumber|string $phoneNumber phone number
* @param string|null $regionCode The ISO 3166-1 alpha-2 country code
*/
public function formatOutOfCountryCallingNumber($phoneNumber, $regionCode): string
public function formatOutOfCountryCallingNumber(PhoneNumber|string $phoneNumber, ?string $regionCode): string
{
$phoneNumber = $this->getPhoneNumber($phoneNumber);

Expand Down
32 changes: 22 additions & 10 deletions src/Validator/Constraints/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,28 @@ class PhoneNumber extends Constraint

/**
* @deprecated since PhoneNumberBundle 3.6, use const ERROR_NAMES instead
*
* @var string[]
*/
protected static $errorNames = self::ERROR_NAMES;
protected static array $errorNames = self::ERROR_NAMES;

public $message;
public $type = self::ANY;
public $defaultRegion;
public $regionPath;
public $format;
public ?string $message = null;
/**
* @var string|string[]
*/
public string|array $type = self::ANY;
public ?string $defaultRegion = null;
public ?string $regionPath = null;
public ?int $format = null;

/**
* @param int|array|null $format Specify the format (\libphonenumber\PhoneNumberFormat::*)
* or options (an associative array)
* @param string|array|null $type
* @param int|array<mixed>|null $format Specify the format (\libphonenumber\PhoneNumberFormat::*)
* or options (an associative array)
* @param string|string[]|null $type
* @param array<mixed> $options
*/
#[HasNamedArguments]
public function __construct($format = null, $type = null, string $defaultRegion = null, string $regionPath = null, string $message = null, array $groups = null, $payload = null, array $options = [])
public function __construct(int|array $format = null, string|array $type = null, string $defaultRegion = null, string $regionPath = null, string $message = null, array $groups = null, $payload = null, array $options = [])
{
if (\is_array($format)) {
@trigger_error('Usage of the argument $format to specify options is deprecated and will be removed in 4.0. Use "$option" argument instead.', \E_USER_DEPRECATED);
Expand Down Expand Up @@ -89,6 +95,9 @@ public function getType(): ?string
return reset($types);
}

/**
* @return string[]
*/
public function getTypes(): array
{
if (\is_array($this->type)) {
Expand All @@ -114,6 +123,9 @@ public function getMessage(): string
return 'This value is not a valid phone number.';
}

/**
* @return string[]
*/
public function getTypeNames(): array
{
$types = \is_array($this->type) ? $this->type : [$this->type];
Expand Down
Loading

0 comments on commit 1d248a4

Please sign in to comment.