-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make helper functions available as static function of Helpers class
- Loading branch information
1 parent
5602f2e
commit 987e62e
Showing
4 changed files
with
87 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Helpers | ||
* | ||
* @package Studiometa | ||
*/ | ||
|
||
namespace Studiometa\WPToolkit; | ||
|
||
/** Class. */ | ||
class Helpers { | ||
/** | ||
* Get an environment variable value. | ||
* | ||
* @param string $key The variable name. | ||
* @return string | ||
*/ | ||
public static function env( string $key ): string { | ||
return env( $key ); | ||
} | ||
|
||
/** | ||
* Get or set a server value. | ||
* | ||
* @param string $key The property name. | ||
* @param string|null $value The value to set. | ||
* @return string|null | ||
*/ | ||
public static function server( string $key, ?string $value = null ): ?string { | ||
return server( $key, $value ); | ||
} | ||
} | ||
|
||
/** | ||
* Get an environment variable value. | ||
* | ||
* @param string $key The variable name. | ||
* @return string | ||
*/ | ||
function env( string $key ): string { | ||
// phpcs:ignore | ||
/** @var array<string, string> Good type. */ | ||
$env = $_ENV; | ||
// In some environment, values are not available in the `$_ENV` variables, | ||
// so we use `getenv` as a fallback to try and get the value. | ||
return $env[ $key ] ?? (string) getenv( $key ); | ||
} | ||
|
||
/** | ||
* Get or set a server value. | ||
* | ||
* @param string $key The property name. | ||
* @param string|null $value The value to set. | ||
* @return string|null | ||
*/ | ||
function server( string $key, ?string $value = null ): ?string { | ||
if ( ! is_null( $value ) ) { | ||
$_SERVER[ $key ] = $value; | ||
} | ||
|
||
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.ValidatedSanitizedInput.MissingUnslash | ||
return $_SERVER[ $key ] ?? null; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Studiometa\WPToolkit\Helpers; | ||
|
||
/** | ||
* Helper functions tests. | ||
*/ | ||
class HelpersTest extends TestCase { | ||
public function test_get_env() { | ||
$_ENV['BAR'] = 'bar'; | ||
|
||
$this->assertEquals( Helpers::env( 'FOO' ), '' ); | ||
$this->assertEquals( Helpers::env( 'BAR' ), 'bar' ); | ||
} | ||
|
||
public function test_get_server() { | ||
$_SERVER['BAR'] = 'bar'; | ||
|
||
$this->assertNull( Helpers::server( 'FOO' ) ); | ||
$this->assertEquals( Helpers::server( 'BAR' ), 'bar' ); | ||
} | ||
} |