Skip to content

Commit

Permalink
Make helper functions available as static function of Helpers class
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Oct 20, 2023
1 parent 5602f2e commit 987e62e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 27 deletions.
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
"autoload": {
"psr-4": {
"Studiometa\\WPToolkit\\": "src/"
},
"files": [
"src/helpers.php"
]
}
},
"scripts": {
"phpcs": "phpcs",
Expand Down
63 changes: 63 additions & 0 deletions src/Helpers.php
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;

Check warning on line 58 in src/Helpers.php

View check run for this annotation

Codecov / codecov/patch

src/Helpers.php#L58

Added line #L58 was not covered by tests
}

// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.ValidatedSanitizedInput.MissingUnslash
return $_SERVER[ $key ] ?? null;
}
23 changes: 0 additions & 23 deletions src/helpers.php

This file was deleted.

23 changes: 23 additions & 0 deletions tests/HelpersTest.php
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' );
}
}

0 comments on commit 987e62e

Please sign in to comment.