Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@nicknisi
Copy link
Member

@nicknisi nicknisi commented Jul 7, 2025

Summary

  • Add private static getEnvVariable method with fallback priority: getenv() -> $_ENV -> $_SERVER
  • Update getApiKey and getClientId methods to use the utility
  • Fixes issue where Laravel config caching prevents getenv() from accessing .env values

@nicknisi nicknisi marked this pull request as ready for review July 7, 2025 15:31
@nicknisi nicknisi requested a review from a team as a code owner July 7, 2025 15:31
@nicknisi nicknisi requested a review from jonatascastro12 July 7, 2025 15:31
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

Introduces a new environment variable handling utility in WorkOS PHP SDK to fix Laravel config caching compatibility issues. The change improves reliability of configuration retrieval.

  • Added new private static getEnvVariable() method in lib/WorkOS.php with cascading fallback (getenv -> $_ENV -> $_SERVER)
  • Modified getApiKey() and getClientId() methods to use the new utility for consistent environment variable handling
  • Added comprehensive test coverage in tests/WorkOS/WorkOSTest.php verifying Laravel config cache compatibility
  • Ensures compatibility with Laravel's config caching by properly handling $_ENV superglobal access

2 files reviewed, 4 comments
Edit PR Review Bot Settings | Greptile

* Get environment variable with fallback to cached config sources
*
* @param string $key Environment variable name
* @return string|false The environment variable value or false if not found
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Return type hint could be more specific: '@return string|false The value or false. Empty string is never returned.'

Comment on lines +14 to +20
putenv("WORKOS_API_KEY=");
putenv("WORKOS_CLIENT_ID=");

unset($_ENV['WORKOS_API_KEY']);
unset($_ENV['WORKOS_CLIENT_ID']);
unset($_SERVER['WORKOS_API_KEY']);
unset($_SERVER['WORKOS_CLIENT_ID']);
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Redundant cleanup of environment variables across different sources. Consider extracting this to a reusable helper method to avoid duplication with tearDown

Suggested change
putenv("WORKOS_API_KEY=");
putenv("WORKOS_CLIENT_ID=");
unset($_ENV['WORKOS_API_KEY']);
unset($_ENV['WORKOS_CLIENT_ID']);
unset($_SERVER['WORKOS_API_KEY']);
unset($_SERVER['WORKOS_CLIENT_ID']);
$this->cleanupWorkOSEnvironment();

Comment on lines +51 to +58
public function testLaravelConfigCachingScenario()
{
$_ENV['WORKOS_API_KEY'] = "pk_test_laravel_cached";
$_ENV['WORKOS_CLIENT_ID'] = "client_test_laravel_cached";

$this->assertEquals("pk_test_laravel_cached", WorkOS::getApiKey());
$this->assertEquals("client_test_laravel_cached", WorkOS::getClientId());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Test could be split into two separate test cases for API key and client ID to follow single responsibility principle

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
@nicknisi nicknisi merged commit 6fcfacd into main Jul 11, 2025
5 checks passed
@nicknisi nicknisi deleted the nicknisi/env-variable-utility branch July 11, 2025 14:24
@csrbarber csrbarber mentioned this pull request Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants