robertogallea / laravel-codicefiscale

Codice fiscale validation for php/laravel
MIT License
42 stars 16 forks source link

Undefined array key "G" #56

Closed caiojhonny closed 1 year ago

caiojhonny commented 1 year ago

Prevent warning: Undefined array key "G" in vendor/robertogallea/laravel-codicefiscale/src/CodiceFiscale.php on line 179.

Codice Fiscale Value used for tests: GNIWCF85G08P870X

Description

Preventing warning "Undefined array key" using value: GNIWCF85G08P870X

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

caiojhonny commented 1 year ago

Why are you assigning the result of array_key_exists() to month field?

Because there is no verification of the key into tabDecodeMonths and a warning is generated: Undefined array key "G" in vendor/robertogallea/laravel-codicefiscale/src/CodiceFiscale.php on line 179.

Can you try to reproduce the same error using this codice fiscale: GNIWCF85G08P870X?

Thanks

robertogallea commented 1 year ago

Ok, but since array_key_exists() always returns a boolean, the right term of null coalescing operator will never be evaluated and month will always be assigned with either true or false, never a number. Am I missing something?

Il mar 13 giu 2023, 17:34 Caio Jhonny @.***> ha scritto:

Why are you assigning the result of array_key_exists() to month field?

Because there is no verification of the key into tabDecodeMonths and a warning is generated: Undefined array key "G" in vendor/robertogallea/laravel-codicefiscale/src/CodiceFiscale.php on line 179.

Can you try to reproduce the same error using this codice fiscale: GNIWCF85G08P870X?

Thanks

— Reply to this email directly, view it on GitHub https://github.com/robertogallea/laravel-codicefiscale/pull/56#issuecomment-1589553800, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEUDEDWGBCH2KQGLGD2J5C3XLCCB5ANCNFSM6AAAAAAZDLF6DI . You are receiving this because you commented.Message ID: @.***>

caiojhonny commented 1 year ago

You right, sorry for that.

I corrected my file.

The logic is: If the key exists on array "$this->tabDecodeMonths", proceed, if not is null.

This prevents the warning.

Thanks

robertogallea commented 1 year ago

Sure, I got that, but was wondering if I was missing something in your syntax. 😉

Thanks!

caiojhonny commented 1 year ago

Would you mind extracting a variable for the repetition in substr(...)?

Thanks!

Done. Thanks!

robertogallea commented 1 year ago

That's it! Thank you!