Skip to content

Commit 3371142

Browse files
authored
Rector improvements
1 parent 26c520a commit 3371142

File tree

13 files changed

+72
-50
lines changed

13 files changed

+72
-50
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ See [GitHub releases](https://github.com/mll-lab/php-utils/releases).
99

1010
## Unreleased
1111

12+
## v5.6.0
13+
14+
### Added
15+
16+
- Improve code quality with automatic type declarations via Rector
17+
- Improve code quality with automatic rector rules for PHPUnit
18+
1219
## v5.5.2
1320

1421
### Fixed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
"require-dev": {
2727
"ergebnis/composer-normalize": "^2",
2828
"jangregor/phpstan-prophecy": "^1",
29+
"larastan/larastan": "^1 || ^2",
2930
"mll-lab/graphql-php-scalars": "^6.3",
3031
"mll-lab/php-cs-fixer-config": "^5",
32+
"orchestra/testbench": "^6 || ^7 || ^8 || ^9",
3133
"phpstan/extension-installer": "^1",
3234
"phpstan/phpstan": "^1",
3335
"phpstan/phpstan-deprecation-rules": "^1",

rector.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,37 @@
22

33
use Rector\CodeQuality\Rector\Concat\JoinStringConcatRector;
44
use Rector\Config\RectorConfig;
5-
use Rector\PHPUnit\Rector\Class_\PreferPHPUnitSelfCallRector;
5+
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitSelfCallRector;
6+
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
7+
use Rector\PHPUnit\Set\PHPUnitSetList;
68
use Rector\Set\ValueObject\SetList;
79

8-
return static function (RectorConfig $rectorConfig): void {
9-
$rectorConfig->sets([
10-
SetList::PHP_71,
11-
SetList::PHP_72,
12-
SetList::PHP_73,
13-
SetList::PHP_74,
10+
return RectorConfig::configure()
11+
->withSets([
1412
SetList::CODE_QUALITY,
15-
]);
16-
$rectorConfig->skip([
13+
SetList::TYPE_DECLARATION,
14+
SetList::RECTOR_PRESET,
15+
PHPUnitSetList::PHPUNIT_40,
16+
PHPUnitSetList::PHPUNIT_50,
17+
PHPUnitSetList::PHPUNIT_60,
18+
PHPUnitSetList::PHPUNIT_70,
19+
PHPUnitSetList::PHPUNIT_80,
20+
PHPUnitSetList::PHPUNIT_90,
21+
PHPUnitSetList::PHPUNIT_100,
22+
PHPUnitSetList::PHPUNIT_110,
23+
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
24+
])
25+
->withPhpSets()
26+
->withRules([PreferPHPUnitSelfCallRector::class])
27+
->withSkip([
28+
PreferPHPUnitThisCallRector::class, // breaks tests
1729
JoinStringConcatRector::class => [
1830
__DIR__ . '/tests/CSVArrayTest.php', // keep `\r\n` for readability
1931
],
20-
]);
21-
22-
$rectorConfig->rule(PreferPHPUnitSelfCallRector::class);
23-
24-
$rectorConfig->paths([__DIR__ . '/src', __DIR__ . '/tests']);
25-
$rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon');
26-
};
32+
])
33+
->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
34+
->withBootstrapFiles([
35+
// Rector uses PHPStan internally, which in turn requires Larastan to be set up correctly
36+
__DIR__ . '/vendor/larastan/larastan/bootstrap.php',
37+
])
38+
->withPHPStanConfigs([__DIR__ . '/phpstan.neon']);

src/IlluminaSampleSheet/V1/DataSection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public function convertSectionToString(): string
5353
protected function validateDuplicatedSampleIDs(): void
5454
{
5555
$groups = $this->rows
56-
->groupBy(fn (Row $row) => $row->sampleID);
56+
->groupBy(fn (Row $row): string => $row->sampleID);
5757

5858
$duplicates = $groups
59-
->filter(fn ($group) => count($group) > 1)
59+
->filter(fn ($group): bool => count($group) > 1)
6060
->keys();
6161
$duplicateIDsAsString = $duplicates->implode(', ');
6262

src/IlluminaSampleSheet/V2/BclConvert/DataSection.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ public function convertSectionToString(): string
2626
{
2727
$this->assertNotEmpty();
2828

29+
$object = $this->dataRows[0];
30+
if (! is_object($object)) {
31+
throw new IlluminaSampleSheetException('Trying to convert empty data section to string.');
32+
}
2933
/** @var array<string> $samplePropertiesOfFirstSample */
30-
$samplePropertiesOfFirstSample = array_keys(get_object_vars($this->dataRows[0]));
34+
$samplePropertiesOfFirstSample = array_keys(get_object_vars($object));
3135
foreach ($this->dataRows as $sample) {
3236
$actualProperties = array_keys(get_object_vars($sample));
3337
if ($samplePropertiesOfFirstSample !== $actualProperties) {
@@ -52,10 +56,10 @@ public function convertSectionToString(): string
5256
/** @param array<string> $samplePropertiesOfFirstSample */
5357
protected function generateDataHeaderByProperties(array $samplePropertiesOfFirstSample): string
5458
{
55-
$samplePropertiesOfFirstSample = array_filter($samplePropertiesOfFirstSample, fn (string $value) // @phpstan-ignore-next-line Variable property access on a non-object required here
59+
$samplePropertiesOfFirstSample = array_filter($samplePropertiesOfFirstSample, fn (string $value): bool // @phpstan-ignore-next-line Variable property access on a non-object required here
5660
=> $this->dataRows[0]->$value !== null);
5761

58-
$samplePropertiesOfFirstSample = array_map(fn (string $value) => ucfirst($value), $samplePropertiesOfFirstSample);
62+
$samplePropertiesOfFirstSample = array_map(fn (string $value): string => ucfirst($value), $samplePropertiesOfFirstSample);
5963

6064
return implode(',', $samplePropertiesOfFirstSample);
6165
}

src/IlluminaSampleSheet/V2/BclConvert/OverrideCycles.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function makeOverrideCycle(string $cycleString): OverrideCycle
5353

5454
return new OverrideCycle(
5555
array_map(
56-
fn (array $match) => new CycleTypeWithCount(new CycleType($match[1]), (int) $match[2]),
56+
fn (array $match): CycleTypeWithCount => new CycleTypeWithCount(new CycleType($match[1]), (int) $match[2]),
5757
$matches
5858
)
5959
);

src/Microplate/AbstractMicroplate.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ function ($content, string $key) use ($flowDirection): string {
7070
/** @return Collection<string, null> */
7171
public function freeWells(): Collection
7272
{
73-
// @phpstan-ignore-next-line Only recognized to be correct with larastan
7473
return $this->wells()->filter(
7574
/** @param TWell $content */
7675
static fn ($content): bool => $content === self::EMPTY_WELL
@@ -80,7 +79,6 @@ public function freeWells(): Collection
8079
/** @return Collection<string, TWell> */
8180
public function filledWells(): Collection
8281
{
83-
// @phpstan-ignore-next-line Only recognized to be correct with larastan
8482
return $this->wells()->filter(
8583
/** @param TWell $content */
8684
static fn ($content): bool => $content !== self::EMPTY_WELL

src/Microplate/Coordinates.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct(string $row, int $column, CoordinateSystem $coordina
5252
*
5353
* @return static<TCoord>
5454
*/
55-
public static function fromArray(array $coordinates, CoordinateSystem $coordinateSystem)
55+
public static function fromArray(array $coordinates, CoordinateSystem $coordinateSystem): self
5656
{
5757
return new self($coordinates['row'], $coordinates['column'], $coordinateSystem);
5858
}

src/Tecan/Rack/BaseRack.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ abstract class BaseRack implements Rack
1818
public function __construct(CoordinateSystem $coordinateSystem)
1919
{
2020
$this->coordinateSystem = $coordinateSystem;
21+
/** @phpstan-ignore-next-line types are correct, but phpstan doesn't understand it */
2122
$this->positions = Collection::times($this->positionCount(), fn () => self::EMPTY_POSITION)
2223
->mapWithKeys(fn ($content, int $position): array => [$position + 1 => $content]);
2324
}

tests/DnaSequenceTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ final class DnaSequenceTest extends TestCase
1010
public function testReverse(): void
1111
{
1212
$dnaSequence = new DnaSequence('ATGC');
13-
self::assertEquals('CGTA', $dnaSequence->reverse());
13+
self::assertSame('CGTA', $dnaSequence->reverse());
1414
}
1515

1616
public function testComplement(): void
1717
{
1818
$dnaSequence = new DnaSequence('ATGC');
19-
self::assertEquals('TACG', $dnaSequence->complement());
19+
self::assertSame('TACG', $dnaSequence->complement());
2020
}
2121

2222
public function testReverseComplement(): void
2323
{
2424
$dnaSequence = new DnaSequence('ATGC');
25-
self::assertEquals('GCAT', $dnaSequence->reverseComplement());
25+
self::assertSame('GCAT', $dnaSequence->reverseComplement());
2626
}
2727

2828
public function testInvalidSequenceThrowsException(): void
@@ -34,8 +34,8 @@ public function testInvalidSequenceThrowsException(): void
3434
public function testEmptySequence(): void
3535
{
3636
$dnaSequence = new DnaSequence('');
37-
self::assertEquals('', $dnaSequence->reverse());
38-
self::assertEquals('', $dnaSequence->complement());
39-
self::assertEquals('', $dnaSequence->reverseComplement());
37+
self::assertSame('', $dnaSequence->reverse());
38+
self::assertSame('', $dnaSequence->complement());
39+
self::assertSame('', $dnaSequence->reverseComplement());
4040
}
4141
}

0 commit comments

Comments
 (0)