Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rector [batch] #83

Merged
merged 17 commits into from
Sep 17, 2023
23 changes: 23 additions & 0 deletions .github/workflows/rector.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
on:
pull_request:
paths-ignore:
- 'docs/**'
- 'README.md'
- 'CHANGELOG.md'
- '.gitignore'
- '.gitattributes'
- 'infection.json.dist'
- 'psalm.xml'

name: rector

jobs:
rector:
uses: yiisoft/actions/.github/workflows/rector.yml@master
secrets:
token: ${{ secrets.YIISOFT_GITHUB_TOKEN }}
with:
os: >-
['ubuntu-latest']
php: >-
['8.2']
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- New #102, #106: Add `CombinedRegexp` class (@xepozz, @vjik)
- Enh #106: Using fully-qualified function calls to improve performance (@vjik)
- New #104: Add methods `StringHelper::trim()`, `StringHelper::ltrim()`, `StringHelper::rtrim()` (@olegbaturin)
- Enh #83: Make minor refactoring with Rector help (@vjik)

## 2.1.2 July 27, 2023

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.18.3",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.8"
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MixedAssignment errorLevel="suppress" />
</issueHandlers>
</psalm>
29 changes: 29 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
]);

$rectorConfig->skip([
ClosureToArrowFunctionRector::class,
JsonThrowOnErrorRector::class,
]);
};
12 changes: 6 additions & 6 deletions src/Inflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Strings;

use Transliterator;

use function extension_loaded;

/**
Expand Down Expand Up @@ -283,13 +285,13 @@ final class Inflector
];

/**
* @var string|\Transliterator Either a {@see \Transliterator}, or a string from which a {@see \Transliterator}
* @var string|Transliterator Either a {@see Transliterator}, or a string from which a {@see Transliterator}
* can be built for transliteration. Used by {@see toTransliterated()} when intl is available.
* Defaults to {@see TRANSLITERATE_LOOSE}.
*
* @see https://secure.php.net/manual/en/transliterator.transliterate.php
*/
private $transliterator = self::TRANSLITERATE_LOOSE;
private string|Transliterator $transliterator = self::TRANSLITERATE_LOOSE;

private bool $withoutIntl = false;

Expand Down Expand Up @@ -366,7 +368,7 @@ public function getSpecialRules(): array
}

/**
* @param string|\Transliterator $transliterator Either a {@see \Transliterator}, or a string from which
* @param string|Transliterator $transliterator Either a {@see \Transliterator}, or a string from which
* a {@see \Transliterator} can be built for transliteration. Used by {@see toTransliterated()} when intl is available.
* Defaults to {@see TRANSLITERATE_LOOSE}.
*
Expand Down Expand Up @@ -595,8 +597,6 @@ public function classToTable(string $className): string
*
* For example, converts "cars" to "Car", "people" to "Person", and "action_log" to "ActionLog".
*
* @param string $tableName
*
* @return string
*/
public function tableToClass(string $tableName): string
Expand Down Expand Up @@ -638,7 +638,7 @@ public function toSlug(string $input, string $replacement = '-', bool $lowercase
* @noinspection PhpComposerExtensionStubsInspection
*
* @param string $input Input string.
* @param string|\Transliterator|null $transliterator either a {@see \Transliterator} or a string
* @param string|Transliterator|null $transliterator either a {@see \Transliterator} or a string
* from which a {@see \Transliterator} can be built. If null, value set with {@see withTransliterator()}
* or {@see TRANSLITERATE_LOOSE} is used.
*
Expand Down
30 changes: 9 additions & 21 deletions src/NumericHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ final class NumericHelper
* Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd etc.
*
* @param float|int|string $value The number to get its ordinal value.
*
* @return string
*/
public static function toOrdinal($value): string
public static function toOrdinal(mixed $value): string
{
if (!is_numeric($value)) {
$type = gettype($value);
Expand All @@ -36,16 +34,12 @@ public static function toOrdinal($value): string
if (in_array($value % 100, [11, 12, 13], true)) {
return $value . 'th';
}
switch ($value % 10) {
case 1:
return $value . 'st';
case 2:
return $value . 'nd';
case 3:
return $value . 'rd';
default:
return $value . 'th';
}
return match ($value % 10) {
1 => $value . 'st',
2 => $value . 'nd',
3 => $value . 'rd',
default => $value . 'th',
};
}

/**
Expand All @@ -54,10 +48,8 @@ public static function toOrdinal($value): string
* @param bool|float|int|string $value
*
* @throws InvalidArgumentException if value is not scalar.
*
* @return string
*/
public static function normalize($value): string
public static function normalize(mixed $value): string
{
/** @psalm-suppress DocblockTypeContradiction */
if (!is_scalar($value)) {
Expand All @@ -76,12 +68,8 @@ public static function normalize($value): string

/**
* Checks whether the given string is an integer number.
*
* @param mixed $value
*
* @return bool
*/
public static function isInteger($value): bool
public static function isInteger(mixed $value): bool
{
return filter_var($value, FILTER_VALIDATE_INT) !== false;
}
Expand Down
61 changes: 21 additions & 40 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ public static function directoryName(string $path): string
* @param string $encoding The encoding to use, defaults to "UTF-8".
*
* @see https://php.net/manual/en/function.mb-substr.php
*
* @return string
*/
public static function substring(string $string, int $start, int $length = null, string $encoding = 'UTF-8'): string
{
Expand All @@ -141,8 +139,6 @@ public static function substring(string $string, int $start, int $length = null,
* If it is not given, then it will default to the length of the string; i.e. end the replacing at the end of string.
* If length is zero then this function will have the effect of inserting replacement into string at the given start offset.
* @param string $encoding The encoding to use, defaults to "UTF-8".
*
* @return string
*/
public static function replaceSubstring(string $string, string $replacement, int $start, ?int $length = null, string $encoding = 'UTF-8'): string
{
Expand Down Expand Up @@ -277,8 +273,6 @@ public static function endsWithIgnoringCase(string $input, ?string $with): bool
* @param int $length Maximum length of the truncated string including trim marker.
* @param string $trimMarker String to append to the beginning.
* @param string $encoding The encoding to use, defaults to "UTF-8".
*
* @return string
*/
public static function truncateBegin(string $input, int $length, string $trimMarker = '…', string $encoding = 'UTF-8'): string
{
Expand Down Expand Up @@ -351,9 +345,9 @@ public static function truncateEnd(string $input, int $length, string $trimMarke
*/
public static function truncateWords(string $input, int $count, string $trimMarker = '…'): string
{
/** @psalm-var list<string> $words */
$words = preg_split('/(\s+)/u', trim($input), -1, PREG_SPLIT_DELIM_CAPTURE);
if (count($words) / 2 > $count) {
/** @var string[] $words */
$words = array_slice($words, 0, ($count * 2) - 1);
return implode('', $words) . $trimMarker;
}
Expand All @@ -368,8 +362,6 @@ public static function truncateWords(string $input, int $count, string $trimMark
* @param string $encoding The encoding to use, defaults to "UTF-8".
*
* @see https://php.net/manual/en/function.mb-strlen.php
*
* @return int
*/
public static function length(string $string, string $encoding = 'UTF-8'): int
{
Expand All @@ -378,14 +370,12 @@ public static function length(string $string, string $encoding = 'UTF-8'): int

/**
* Counts words in a string.
*
* @param string $input
*
* @return int
*/
public static function countWords(string $input): int
{
return count(preg_split('/\s+/u', $input, -1, PREG_SPLIT_NO_EMPTY));
/** @var array $words */
$words = preg_split('/\s+/u', $input, -1, PREG_SPLIT_NO_EMPTY);
return count($words);
}

/**
Expand All @@ -395,8 +385,6 @@ public static function countWords(string $input): int
* @param string $encoding The encoding to use, defaults to "UTF-8".
*
* @see https://php.net/manual/en/function.mb-strtolower.php
*
* @return string
*/
public static function lowercase(string $string, string $encoding = 'UTF-8'): string
{
Expand All @@ -410,8 +398,6 @@ public static function lowercase(string $string, string $encoding = 'UTF-8'): st
* @param string $encoding The encoding to use, defaults to "UTF-8".
*
* @see https://php.net/manual/en/function.mb-strtoupper.php
*
* @return string
*/
public static function uppercase(string $string, string $encoding = 'UTF-8'): string
{
Expand All @@ -424,8 +410,6 @@ public static function uppercase(string $string, string $encoding = 'UTF-8'): st
* @param string $string The string to be processed.
* @param string $encoding The encoding to use, defaults to "UTF-8".
*
* @return string
*
* @see https://php.net/manual/en/function.ucfirst.php
*/
public static function uppercaseFirstCharacter(string $string, string $encoding = 'UTF-8'): string
Expand All @@ -443,16 +427,17 @@ public static function uppercaseFirstCharacter(string $string, string $encoding
* @param string $encoding The encoding to use, defaults to "UTF-8".
*
* @see https://php.net/manual/en/function.ucwords.php
*
* @return string
*/
public static function uppercaseFirstCharacterInEachWord(string $string, string $encoding = 'UTF-8'): string
{
$words = preg_split('/\s/u', $string, -1, PREG_SPLIT_NO_EMPTY);

$wordsWithUppercaseFirstCharacter = array_map(static function (string $word) use ($encoding) {
return self::uppercaseFirstCharacter($word, $encoding);
}, $words);
$wordsWithUppercaseFirstCharacter = array_map(
static function (string $word) use ($encoding) {
return self::uppercaseFirstCharacter($word, $encoding);
},
$words
);

return implode(' ', $wordsWithUppercaseFirstCharacter);
}
Expand Down Expand Up @@ -495,8 +480,6 @@ public static function base64UrlDecode(string $input): string
* @param string $string The input string.
* @param string $separator The boundary string. It is a part of regular expression
* so should be taken into account or properly escaped with {@see preg_quote()}.
*
* @return array
*/
public static function split(string $string, string $separator = '\R'): array
{
Expand Down Expand Up @@ -565,19 +548,17 @@ public static function parsePath(
}

return array_map(
static function (string $key) use ($delimiter, $escapeCharacter): string {
return str_replace(
[
$escapeCharacter . $escapeCharacter,
$escapeCharacter . $delimiter,
],
[
$escapeCharacter,
$delimiter,
],
$key
);
},
static fn (string $key): string => str_replace(
[
$escapeCharacter . $escapeCharacter,
$escapeCharacter . $delimiter,
],
[
$escapeCharacter,
$delimiter,
],
$key
),
$result
);
}
Expand Down
18 changes: 4 additions & 14 deletions src/WildcardPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,15 @@
final class WildcardPattern
{
private bool $ignoreCase = false;
private string $pattern;

/**
* @var string[]
*/
private array $delimiters;

/**
* @param string $pattern The shell wildcard pattern to match against.
* @param string[] $delimiters Delimiters to consider for "*" (`/` and `\` by default).
*/
public function __construct(string $pattern, array $delimiters = ['\\\\', '/'])
{
$this->pattern = $pattern;
$this->delimiters = $delimiters;
public function __construct(
private string $pattern,
private array $delimiters = ['\\\\', '/'],
) {
}

/**
Expand Down Expand Up @@ -93,10 +87,6 @@ public function match(string $string): bool

/**
* Make pattern case insensitive.
*
* @param bool $flag
*
* @return self
*/
public function ignoreCase(bool $flag = true): self
{
Expand Down
4 changes: 1 addition & 3 deletions tests/InflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,9 @@ public function testImmutability(): void
/**
* Asserts that value is one of expected values.
*
* @param mixed $actual
* @param array $expected
* @param string $message
*/
private function assertIsOneOf($actual, array $expected, $message = ''): void
private function assertIsOneOf(mixed $actual, array $expected, $message = ''): void
{
self::assertThat($actual, new IsOneOfAssert($expected), $message);
}
Expand Down
Loading