Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasReker committed Mar 26, 2023
1 parent 6537e87 commit a39793a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 67 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ jobs:
run: composer install --optimize-autoloader --prefer-dist

- name: Run PHPUnit tests
run: composer run test
run: composer test

- name: Run PHP CS fixer
run: composer run cs-check
run: composer cs-check

- name: Run phpstan
run: composer run phpstan
run: composer phpstan

- name: Run ergebnis/composer-normalize
run: composer normalize --dry-run --no-check-lock
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ functions.
### Requirements

- `PHP` >= 8.0
- `ext-mbstring`
- php-extension `ext-mbstring`

### Installation

Expand All @@ -39,17 +39,17 @@ functions.

## ✅ mb_count_chars

mb_count_chars — Return information about characters used in a string
mb_count_chars — Return information about characters used in a string.

```
```php
mb_count_chars(string $string, int $mode = 0, string $encoding = 'UTF-8'): array|string
```

## ✅ mb_ucwords

mb_ucwords — Uppercase the first character of each word in a string.

```
```php
mb_ucwords(
string $string,
string $separators = " \t\r\n\f\v",
Expand All @@ -61,15 +61,15 @@ mb_ucwords(

mb_strrev — Reverse a string.

```
```php
mb_strrev(string $string, string $encoding = 'UTF-8'): string
```

## ✅ mb_str_pad

mb_str_pad — Pad a string to a certain length with another string.

```
```php
mb_str_pad(
string $string,
int $length,
Expand All @@ -83,7 +83,7 @@ mb_str_pad(

mb_ucfirst — Make a string's first character uppercase.

```
```php
mb_ucfirst(string $string, string $encoding = 'UTF-8'): string
```

Expand Down Expand Up @@ -117,37 +117,37 @@ docker exec -it php-mbstring-extension bash
PHP Coding Standards Fixer:

```bash
composer run cs-fix
composer cs-fix
```

PHP Coding Standards Checker:

```bash
composer run cs-check
composer cs-check
```

Rector Fixer:

```bash
composer run rector-fix
composer rector-fix
```

Rector Checker:

```bash
composer run rector-check
composer rector-check
```

PHP Stan:

```bash
composer run phpstan
composer phpstan
```

Unit tests:

```bash
composer run test
composer test
```

### License
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "3"
services:
app:
build: .
image: php-mbstring-extension:1.0.0
image: php-mbstring-extension:1.0.1
container_name: "php-mbstring-extension"
volumes:
- ./:/app
101 changes: 51 additions & 50 deletions src/Helper/multibyteString.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@
*
* @see https://github.com/MathiasReker/php-mbstring-extension
*
* @param string $string
* The input string
* @param string $encoding [optional]
* @param string $string The input string to modify.
* @param string $encoding [optional] The character encoding. Defaults to 'UTF-8'.
*
* @return string the modified string
* @return string The modified string.
*/
function mb_ucwords(string $string, string $encoding = 'UTF-8'): string
{
$result = '';
$previousCharacter = ' ';
$previous_character = ' ';

for ($i = 0; $i < mb_strlen($string, $encoding); ++$i) {
$currentCharacter = mb_substr($string, $i, 1, $encoding);
$length = mb_strlen($string, $encoding);
for ($i = 0; $i < $length; ++$i) {
$current_character = mb_substr($string, $i, 1, $encoding);

if (' ' === $previousCharacter) {
$currentCharacter = mb_strtoupper($currentCharacter, $encoding);
if (' ' === $previous_character) {
$current_character = mb_strtoupper($current_character, $encoding);
}

$result .= $currentCharacter;
$previousCharacter = $currentCharacter;
$result .= $current_character;
$previous_character = $current_character;
}

return $result;
Expand All @@ -42,22 +42,26 @@ function mb_ucwords(string $string, string $encoding = 'UTF-8'): string

if (!function_exists('mb_ucfirst')) {
/**
* Make a string's first character uppercase.
* Make the first character of a string uppercase.
*
* @see https://github.com/MathiasReker/php-mbstring-extension
*
* @param string $string
* The input string
* @param string $encoding [optional]
* @param string $string The input string.
* @param string $encoding [optional] The character encoding. Defaults to 'UTF-8'.
*
* @return string the resulting string
* @return string The resulting string.
*/
function mb_ucfirst(string $string, string $encoding = 'UTF-8'): string
{
$firstChar = mb_substr($string, 0, 1, $encoding);
$rest = mb_substr($string, 1, null, $encoding);

return mb_strtoupper($firstChar, $encoding) . $rest;
$lower_firstChar = mb_strtolower($firstChar, $encoding);
if ($firstChar === $lower_firstChar) {
$firstChar = mb_strtoupper($firstChar, $encoding);
}

return $firstChar . $rest;
}
}

Expand All @@ -67,9 +71,8 @@ function mb_ucfirst(string $string, string $encoding = 'UTF-8'): string
*
* @see https://github.com/MathiasReker/php-mbstring-extension
*
* @param string $string
* The string to be reversed
* @param string $encoding [optional]
* @param string $string The string to be reversed
* @param string $encoding [optional] The character encoding. Defaults to 'UTF-8'.
*
* @return string the reversed string
*/
Expand All @@ -92,19 +95,18 @@ function mb_strrev(string $string, string $encoding = 'UTF-8'): string
*
* @see https://github.com/MathiasReker/php-mbstring-extension
*
* @param string $pad_string [optional]
* The pad_string may be truncated if the
* required number of padding characters can't be evenly divided by the
* pad_string's length
* @param int $pad_type [optional]
* Optional argument pad_type can be
* STR_PAD_RIGHT, STR_PAD_LEFT,
* or STR_PAD_BOTH. If
* pad_type is not specified it is assumed to be
* STR_PAD_RIGHT.
* @param string $encoding [optional]
*
* @return string the padded string
* @param string $input The string to pad
* @param int $pad_length The length of the resulting padded string
* @param string $pad_string [optional] The string to use for padding, defaults to ' '
* The pad_string may be truncated if the required number of padding
* characters can't be evenly divided by the pad_string's length.
* @param int $pad_type [optional] The type of padding to apply, defaults to STR_PAD_RIGHT
* Can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH.
* @param string $encoding [optional] The character encoding. Defaults to 'UTF-8'
*
* @return string The padded string
*
* @throws ValueError If $pad_type is not STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH
*/
function mb_str_pad(string $input, int $pad_length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, string $encoding = 'UTF-8'): string
{
Expand All @@ -118,26 +120,25 @@ function mb_str_pad(string $input, int $pad_length, string $pad_string = ' ', in

if (!function_exists('mb_count_chars')) {
/**
* Return information about characters used in a string.
* Returns information about characters used in a string.
*
* @see https://github.com/MathiasReker/php-mbstring-extension
*
* @param string $string
* The examined string
* @param int $mode
* See return values
* @param string $encoding [optional]
*
* @return array<int<0, max>>|string Depending on mode
* count_chars returns one of the following:
* 0 - an array with the byte-value as key and the frequency of
* every byte as value.
* 1 - same as 0 but only byte-values with a frequency greater
* than zero are listed.
* 2 - same as 0 but only byte-values with a frequency equal to
* zero are listed.
* 3 - a string containing all unique characters is returned.
* 4 - a string containing all not used characters is returned.
* @param string $string The string to be examined.
* @param int $mode Specifies what information to return.
* - 0: Returns an array with the byte-value as key and the frequency of
* every byte as value.
* - 1: Same as 0 but only byte-values with a frequency greater than zero are listed.
* - 2: Same as 0 but only byte-values with a frequency equal to zero are listed.
* - 3: Returns a string containing all unique characters in the string.
* - 4: Returns a string containing all characters in the string that are not used.
* @param string $encoding [optional] The character encoding. Defaults to 'UTF-8'.
*
* @return int[]|string Returns the information requested based on the mode parameter:
* - Mode 0, 1, or 2: returns an array with byte-values as keys and frequencies as values.
* - Mode 3 or 4: returns a string with unique characters or unused characters.
*
* @throws ValueError if the mode parameter is not between 0 and 4 (inclusive).
*/
function mb_count_chars(string $string, int $mode, string $encoding = 'UTF-8'): array|string
{
Expand Down
5 changes: 5 additions & 0 deletions test/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"mathiasreker/php-mbstring-extension": "^1.0"
}
}

0 comments on commit a39793a

Please sign in to comment.