Consider a String class
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 66
- Forks
- 36
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 16
Description
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 :-)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No source file, entry point, or runnable test path is named; the issue only shares an experimental Parsely\String class and sample tests. First clarify whether the utility should be implemented, which existing string comparisons it should replace, and what the accepted API and tests would be before starting.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100