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

Comma Separated String of IDs used as an Array #482

Open
ThomasAFink opened this issue Jul 4, 2024 · 2 comments
Open

Comma Separated String of IDs used as an Array #482

ThomasAFink opened this issue Jul 4, 2024 · 2 comments

Comments

@ThomasAFink
Copy link
Member

ThomasAFink commented Jul 4, 2024

The variable $excludedIds is used to hold IDs that are excluded from the appointment roll call by the clerk. Currently, this variable is being handled as a comma-separated string, which is then split into an array using the explode function. This approach can lead to potential issues and reduced code clarity. $excludedIds should probably just be an array of ids and not a comma separated string.

In the getNextProcess method:

$excludedIds = (0 < $queueList->count()) ? $excludedIds : '';

should be refactored to ensure $excludedIds is always an array.

In the WorkstationProcessPreCall class:

$excludedIds = $validator->getParameter('exclude')->isString()->setDefault('')->getValue();
if ($excludedIds) {
    $exclude = explode(',', $excludedIds);
}
$exclude[] = $process->toQueue(\App::$now)->number;

should be refactored to handle $excludedIds as an array directly.

@ThomasAFink
Copy link
Member Author

@coderabbitai $excludedIds is used in several zmsadmin classes and in one zmsclient class.

Relevant files:

How would you refactor it from a comma separated string of ids to an array?

Copy link
Contributor

coderabbitai bot commented Dec 6, 2024

Tip

For best results, initiate chat on the files or code changes.

Based on the code analysis, I can now provide a comprehensive refactoring strategy.

@ThomasAFink Here's how I would refactor the $excludedIds handling from a comma-separated string to an array:

  1. First, modify the parameter handling in all controllers to accept an array:
// In all relevant controllers (WorkstationProcessCalled, WorkstationProcessCancelNext, WorkstationProcessNext, WorkstationProcessPreCall)
$excludedIds = $validator->getParameter('exclude')
    ->isArray()  // Change from isString to isArray
    ->setDefault([])  // Change from '' to []
    ->getValue();
  1. Update the ClusterHelper's getNextProcess method:
public static function getNextProcess(array $excludedIds)  // Type hint as array
{
    $queueList = static::getProcessList(\App::$now->format('Y-m-d'))
        ->toQueueList(\App::$now)
        ->withoutStatus(['fake','missed', 'parked']);
    $excludedIds = (0 < $queueList->count()) ? $excludedIds : [];  // Change from '' to []

    if (1 > $queueList->count()) {
        return new \BO\Zmsentities\Process();
    }
    
    // Convert array to comma-separated string only for API call
    $excludedIdsString = implode(',', $excludedIds);
    
    if (static::isClusterEnabled()) {
        $nextProcess = \App::$http->readGetResult(
            '/cluster/'. static::$cluster['id'] .'/queue/next/',
            [
                'exclude' => $excludedIdsString,
                'allowClusterWideCall' => \App::$allowClusterWideCall
            ]
        )->getEntity();
    } else {
        $nextProcess = \App::$http->readGetResult(
            '/scope/'. static::$workstation->scope['id'] .'/queue/next/',
            ['exclude' => $excludedIdsString]
        )->getEntity();
    }
    
    return ($nextProcess) ? $nextProcess : new \BO\Zmsentities\Process();
}
  1. Update the WorkstationRequests class similarly:
public function readNextProcess(array $excludedIds)  // Type hint as array
{
    $excludedIdsString = implode(',', $excludedIds);  // Convert to string only for API call
    
    if ($this->workstation->isClusterEnabled()) {
        $process = $this->http
            ->readGetResult('/cluster/'. $this->cluster['id'] .'/queue/next/', ['exclude' => $excludedIdsString])
            ->getEntity();
    } else {
        $process = $this->http
            ->readGetResult(
                '/scope/'. $this->scope['id'] .'/queue/next/',
                ['exclude' => $excludedIdsString]
            )
            ->getEntity();
    }
    return $process;
}
  1. Update the template rendering in controllers to handle arrays:
// In WorkstationProcessCalled and WorkstationProcessPreCall
$exclude = is_array($excludedIds) ? $excludedIds : [];
$exclude[] = $process->toQueue(\App::$now)->number;

return \BO\Slim\Render::withHtml(
    $response,
    'template.twig',
    array(
        // ...other parameters...
        'exclude' => implode(',', $exclude),  // Convert to string only for template
    )
);
  1. Update the JavaScript code (if any) that handles these parameters in AJAX calls to send the excluded IDs as an array parameter. The server will automatically handle array parameters in the query string format like exclude[]=1&exclude[]=2.

This refactoring:

  • Makes the code more type-safe by using arrays throughout the PHP code
  • Only converts to comma-separated strings at the boundaries (API calls and template rendering)
  • Maintains backward compatibility with the API endpoints that expect comma-separated strings
  • Makes the code more maintainable and clearer in its intent

Would you like me to provide more specific details about any part of this refactoring strategy?

🔗 Analysis chain

Let me gather the necessary information about how $excludedIds is being used in these files.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Let's check how $excludedIds is used in these files
echo "=== WorkstationProcessCalled.php ==="
cat "zmsadmin/src/Zmsadmin/WorkstationProcessCalled.php"

echo -e "\n=== WorkstationProcessCancelNext.php ==="
cat "zmsadmin/src/Zmsadmin/WorkstationProcessCancelNext.php"

echo -e "\n=== WorkstationProcessNext.php ==="
cat "zmsadmin/src/Zmsadmin/WorkstationProcessNext.php"

echo -e "\n=== WorkstationProcessPreCall.php ==="
cat "zmsadmin/src/Zmsadmin/WorkstationProcessPreCall.php"

echo -e "\n=== ClusterHelper.php ==="
cat "zmsadmin/src/Zmsadmin/Helper/ClusterHelper.php"

echo -e "\n=== WorkstationRequests.php ==="
cat "zmsclient/src/Zmsclient/WorkstationRequests.php"

Length of output: 16564


✏️ Learnings added
Learnt from: ThomasAFink
URL: https://github.com/it-at-m/eappointment/issues/482

Timestamp: 2024-12-06T16:10:43.031Z
Learning: In the eappointment system, when handling excluded IDs for queue management, maintain them as arrays throughout the PHP code and only convert to comma-separated strings at the boundaries (API calls and template rendering) for better type safety and maintainability.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant