wp-parsely
wp-parsely copied to clipboard
Consider a String class
Is your feature request related to a problem?
I noticed a few strings and string comparisons being done within various parts of the codebase and wondered if we could improve the code readability / maintainability.
Describe the solution you'd like
I started experimenting with a new Parsely\String
utility class:
<?php
namespace Parsely;
class String {
public static function is_true( $string ) {
return 'true' === $string;
}
public static function is_false( $string ) {
return 'false' === $string;
}
public static function is_true_or_false( $string ) {
return self::is_true( $string ) || self::is_false( $string );
}
public static function is_not_true_or_false( $string ) {
return ! self::is_true_or_false( $string );
}
public static function contains_a_space( $string ) {
return strpos( $string, ' ' ) !== false;
}
public static function contains_a_period( $string ) {
return strpos( $string, '.' ) !== false;
}
}
<?php
namespace Parsely\Tests;
use PHPUnit\Framework\TestCase;
use Parsely\String as ParselyString;
/**
* Sample test case.
*
* @category Class
* @package SampleTest
*/
final class StringTest extends TesCase {
/**
* Data provider for test_string_is_true().
*/
protected function string_is_true_provider() {
return array(
array( 'true', true ),
array( 'truex', false ),
array( '.true', false ),
array( 1, false ),
array( 2, false ),
array( -3, false ),
array( array( 'true' ), false ),
);
}
/**
* Test the string is identical to 'true'.
*
* @dataProvider data_string_is_true
*
* @param string $string String to be tested.
* @param string $expected Expected assertion result.
*/
public function test_string_is_true( $string, $expected ) {
if ( $expected ) {
self::assertTrue( ParselyString::is_true( $string ) );
} else {
self::assertFalse( ParselyString::is_true( $string ) );
}
}
/**
* Data provider for test_string_is_false().
*/
protected function string_is_false_provider() {
return array(
array( 'false', true ),
array( 'falsex', false ),
array( '.false', false ),
array( 1, false ),
array( 2, false ),
array( -3, false ),
array( array( 'false' ), false ),
);
}
/**
* Test the string is identical to 'false'.
*
* @dataProvider data_string_is_false
*
* @param string $string String to be tested.
* @param string $expected Expected assertion result.
*/
public function test_string_is_false( $string, $expected ) {
if ( $expected ) {
self::assertTrue( ParselyString::is_false( $string ) );
} else {
self::assertFalse( ParselyString::is_false( $string ) );
}
}
/**
* Data provider for test_string_is_true().
*/
protected function string_is_true_or_false_provider() {
return array(
array( 'true', true ),
array( 'truex', false ),
array( '.true', false ),
array( 1, false ),
array( 2, false ),
array( -3, false ),
array( array( 'true' ), false ),
array( 'false', true ),
array( 'falsex', false ),
array( '.false', false ),
array( 1, false ),
array( 2, false ),
array( -3, false ),
array( array( 'false' ), false ),
);
}
/**
* Test the string is identical to 'true'.
*
* @dataProvider data_string_is_true
*
* @param string $string String to be tested.
* @param string $expected Expected assertion result.
*/
public function test_string_is_true( $string, $expected ) {
if ( $expected ) {
self::assertTrue( ParselyString::is_true_or_false( $string ) );
} else {
self::assertFalse( ParselyString::is_true_or_false( $string ) );
}
}
}
Additional context
No change needed at this point - I just wanted to share it before I lost the work locally :-)
Notes from #766:
- https://github.com/Parsely/wp-parsely/pull/766#discussion_r848546763