Narrowing return types (#18009)

| Q               | A
|-----------------|-----
| Branch?         | 2.1 <!-- see the comment below -->
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | #17963
| License         | MIT

Removing types that are impossible to reach from the return type or the
documentation.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Bug Fixes**
- Improved consistency in return values for locale and payment-related
methods, ensuring they always return the expected data type and never
null where previously possible.
- **Refactor**
- Updated method signatures to provide stricter and more predictable
return types for locale conversion, payment provision, and data
transformation logic.
- **Chores**
- Adjusted static analysis configuration to report unused return type
issues, enhancing code quality checks.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Grzegorz Sadowski 2025-05-21 16:00:59 +02:00 committed by GitHub
commit e1b9c654fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 4 additions and 7 deletions

View file

@ -38,7 +38,6 @@ parameters:
ignoreErrors:
- identifier: missingType.generics # This need to be fixed, too many classes are using generics without PHPDoc type
- identifier: varTag.nativeType
- identifier: return.unusedType
- '/(Interface|Class) [a-zA-Z\\]+ specifies template type (\w+) of interface [a-zA-Z\\]+ as [a-zA-Z\\]+ (of [a-zA-Z\\]+ )?but it''s already specified as/' # turns off a weird generics check behavior, we are basing on Psalm for generics checks - '/Method Sylius\\Component\\(\w+)\\Model\\(\w+)\:\:getId\(\) has no return type specified./'
- '/Method Sylius\\Bundle\\(Admin|Shop)Bundle\\Twig\\Component\\[a-zA-Z\\]+\:\:getDataModelValue\(\) is unused./'
- '/Method Sylius\\Component\\(\w+)\\Model\\(\w+)\:\:getId\(\) has no return type specified./'

View file

@ -35,7 +35,7 @@ final class LocaleExtension extends AbstractExtension
];
}
public function convertCodeToName(string $code, ?string $localeCode = null): ?string
public function convertCodeToName(string $code, ?string $localeCode = null): string
{
try {
return $this->localeConverter->convertCodeToName($code, $this->getLocaleCode($localeCode));

View file

@ -17,7 +17,7 @@ use Symfony\Component\Form\Extension\Core\DataTransformer\MoneyToLocalizedString
final class MoneyIntToLocalizedStringTransformer extends MoneyToLocalizedStringTransformer
{
public function reverseTransform(mixed $value): float|int|null
public function reverseTransform(mixed $value): ?int
{
if (!is_numeric($value)) {
return null;

View file

@ -23,12 +23,10 @@ final class PercentFloatToLocalizedStringTransformer extends PercentToLocalizedS
*
* @param string $value Percentage value
*
* @return float Normalized value
*
* @throws TransformationFailedException if the given value is not a string or
* if the value could not be transformed
*/
public function reverseTransform(mixed $value): float|int|null
public function reverseTransform(mixed $value): ?float
{
if (!is_numeric($value)) {
return null;

View file

@ -33,7 +33,7 @@ final class OrderPaymentProvider implements OrderPaymentProviderInterface
) {
}
public function provideOrderPayment(OrderInterface $order, string $targetState): ?PaymentInterface
public function provideOrderPayment(OrderInterface $order, string $targetState): PaymentInterface
{
/** @var PaymentInterface $payment */
$payment = $this->paymentFactory->createWithAmountAndCurrencyCode(