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

Modernization: Replace Deprecated each() Function #1910

Open
wants to merge 1 commit into
base: 2.0
Choose a base branch
from

Conversation

GabrielBragaGit
Copy link

@GabrielBragaGit GabrielBragaGit commented Dec 30, 2024

Description

Updated tln_fixatts method to use modern PHP iteration syntax, removing deprecated each() function.

Details

  • Replaced each() with foreach loop
  • Added type safety for input array
  • Maintained original method logic
  • Ensures compatibility with PHP 7.2+ and PHP 8.0+
  • Improved loop control with continue 2

Code Change

// Before
while ([$attname, $attvalue] = each($attary)) {
    // Processing logic
}

// After
foreach ($attary as $attname => $attvalue) {
    // Processing logic with continue 2
}

Rationale

  • each() function is deprecated and removed in newer PHP versions

  • foreach provides a more modern and readable way to iterate arrays

  • continue 2 ensures proper loop control when removing attributes

    • Immediately exits both inner and outer loops
    • Prevents unnecessary iteration after attribute removal
  • Improves code maintainability and compatibility

Loop Control Explanation

  • Simple continue would only exit the inner loop
  • continue 2 completely exits nested loops
  • Ensures we move to the next attribute after removal
  • Prevents potential side effects of partial loop execution

#### Description
Updated `tln_fixatts` method to use modern PHP iteration syntax, removing deprecated `each()` function.

#### Details
- Replaced `each()` with `foreach` loop
- Added type safety for input array
- Maintained original method logic
- Ensures compatibility with PHP 7.2+ and PHP 8.0+
- Improved loop control with `continue 2`

#### Code Change
```php
// Before
while ([$attname, $attvalue] = each($attary)) {
    // Processing logic
}

// After
foreach ($attary as $attname => $attvalue) {
    // Processing logic with continue 2
}

### Rationale

- each() function is deprecated and removed in newer PHP versions
- foreach provides a more modern and readable way to iterate arrays
- continue 2 ensures proper loop control when removing attributes

   - Immediately exits both inner and outer loops
   - Prevents unnecessary iteration after attribute removal


- Improves code maintainability and compatibility

### Loop Control Explanation

- Simple continue would only exit the inner loop
- continue 2 completely exits nested loops
- Ensures we move to the next attribute after removal
- Prevents potential side effects of partial loop execution
Comment on lines +563 to +568
/**
* Convert to array if is not
*/
$attary = is_array($attary) ? $attary : [];

foreach ($attary as $attname => $attvalue) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@GabrielBragaGit ,

Please run the command to fix the failing Pint test cases and then push the updated code.

vendor/bin/pint

@amit-webkul amit-webkul removed their assignment Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants