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] #30

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/rector.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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
with:
os: >-
['ubuntu-latest']
php: >-
['8.0']
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.14.3",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.18"
Expand Down
22 changes: 22 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
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,
]);
};
2 changes: 0 additions & 2 deletions src/ContentDispositionHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public static function name(): string
* @param string|null $fileName The file name.
*
* @throws InvalidArgumentException if `$type` is incorrect.
*
* @return string
*/
public static function value(string $type, ?string $fileName = null): string
{
Expand Down
28 changes: 9 additions & 19 deletions src/HeaderValueHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
use function reset;
use function rtrim;
use function strtolower;
use function strpos;
use function substr;
use function trim;
use function usort;

Expand Down Expand Up @@ -171,7 +169,7 @@ static function (array $matches) use (&$output, $lowerCaseParameter, $lowerCaseP
* @see getValueAndParameters
*/
public static function getSortedValueAndParameters(
$values,
mixed $values,
bool $lowerCaseValue = true,
bool $lowerCaseParameter = true,
bool $lowerCaseParameterValue = true
Expand Down Expand Up @@ -210,10 +208,7 @@ public static function getSortedValueAndParameters(
usort($output, static function (array $a, array $b) {
$a = $a['q'];
$b = $b['q'];
if ($a === $b) {
return 0;
}
return $a > $b ? -1 : 1;
return $b <=> $a;
});

return $output;
Expand All @@ -230,7 +225,7 @@ public static function getSortedValueAndParameters(
* @link https://tools.ietf.org/html/rfc7231#section-5.3.2
* @link https://www.ietf.org/rfc/rfc2045.html#section-2
*/
public static function getSortedAcceptTypes($values): array
public static function getSortedAcceptTypes(string|array $values): array
{
$output = self::getSortedValueAndParameters($values);

Expand All @@ -243,34 +238,29 @@ public static function getSortedAcceptTypes($values): array
$typeA = reset($a);
$typeB = reset($b);

if (strpos($typeA, '*') === false && strpos($typeB, '*') === false) {
if (!str_contains($typeA, '*') && !str_contains($typeB, '*')) {
$countA = count($a);
$countB = count($b);
if ($countA === $countB) {
// They are equivalent for the same parameter number
return 0;
}
// No wildcard character, higher parameter number wins
return $countA > $countB ? -1 : 1;
return $countB <=> $countA;
}

$endWildcardA = substr($typeA, -1, 1) === '*';
$endWildcardB = substr($typeB, -1, 1) === '*';
$endWildcardA = str_ends_with($typeA, '*');
$endWildcardB = str_ends_with($typeB, '*');

if (($endWildcardA && !$endWildcardB) || (!$endWildcardA && $endWildcardB)) {
// The wildcard ends is the loser.
return $endWildcardA ? 1 : -1;
}

// The wildcard starts is the loser.
return strpos($typeA, '*') === 0 ? 1 : -1;
return str_starts_with($typeA, '*') ? 1 : -1;
});

foreach ($output as $key => $value) {
$type = array_shift($value);
unset($value['q']);

if (count($value) === 0) {
if ((is_countable($value) ? count($value) : 0) === 0) {
$output[$key] = $type;
continue;
}
Expand Down
4 changes: 0 additions & 4 deletions tests/ContentDispositionHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ public function dataValue(): array

/**
* @dataProvider dataValue
*
* @param string $type
* @param string|null $fileName
* @param string $expected
*/
public function testValue(string $type, ?string $fileName, string $expected): void
{
Expand Down