Skip to content

Instantly share code, notes, and snippets.

@MuhammadQuran17
Created November 12, 2025 07:09
Show Gist options
  • Select an option

  • Save MuhammadQuran17/7b426e2e6b0fae864e1110fae3fe068e to your computer and use it in GitHub Desktop.

Select an option

Save MuhammadQuran17/7b426e2e6b0fae864e1110fae3fe068e to your computer and use it in GitHub Desktop.
php reafactoring task 2 DRY

How to prevent DRY in PHP. Refactoring task

class ManageableMapper extends BaseMapper
{
    public function map(array $data): array
    {
        return array_map(function ($row) {
            Arr::set(
                $row,
                'readonly',
                in_array($row['readonly'], config('User::user-management.roles.readonly'))
            );

            return $row;
        }, $data);
    }
}

and

class ManageableUserMapper extends BaseMapper
{
    /**
     * @param array $data
     *
     * @return array
     */
    public function map(array $data): array
    {
        return array_map(function ($row) {
            Arr::set(
                $row,
                'readonly',
                in_array($row['readonly'], config('User::user-management.roles.readonly')) || $row['super']
            );

            return $row;
        }, $data);
    }
}

Notice in this 2 classes the difference lies only in || $row['super']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment