Skip to content

Commit

Permalink
Add Rector
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Mar 27, 2024
1 parent c20dfe4 commit e740353
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 208 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
tags:
- '*'
schedule:
- cron: 0 13 * * MON,TUE
- cron: 0 13 * * MON,THU

jobs:
coding-style:
Expand All @@ -30,10 +30,13 @@ jobs:
- name: Install the dependencies
run: composer install --no-interaction --no-suggest

- name: Check the coding style
- name: Run Rector
run: vendor/bin/rector --dry-run --no-progress-bar --ansi

- name: Run ECS
run: vendor/bin/ecs check --no-progress-bar --ansi

- name: Analyze the code
- name: Run PHPStan
run: vendor/bin/phpstan analyse --no-progress

tests:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# Monorepo Tools
/.monorepo-split-cache/
/.monorepo-merge-cache/

# PhpUnit
/.phpunit.result.cache
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@
},
"scripts": {
"all": [
"@rector",
"@ecs",
"@unit-tests",
"@phpstan"
],
"ecs": "@php vendor/bin/ecs check --fix --ansi",
"phpstan": "@php vendor/bin/phpstan analyze --ansi",
"rector": "@php vendor/bin/rector --ansi",
"unit-tests": "@php vendor/bin/phpunit --colors=always"
}
}
2 changes: 1 addition & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
->withParallel()
->withSpacing(Option::INDENTATION_SPACES, "\n")
->withConfiguredRule(HeaderCommentFixer::class, ['header' => "This file is part of Contao.\n\n(c) Leo Feyer\n\n@license LGPL-3.0-or-later"])
->withCache(sys_get_temp_dir().'/monorepo_cache')
->withCache(sys_get_temp_dir().'/ecs_monorepo_cache')
;
23 changes: 23 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Rector\CodingStyle\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector;
use Rector\Config\RectorConfig;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;

return RectorConfig::configure()
->withSets([__DIR__.'/vendor/contao/code-quality/config/rector.php'])
->withPaths([
__DIR__.'/src',
__DIR__.'/tests',
])
->withSkip([
ArraySpreadInsteadOfArrayMergeRector::class,
JsonThrowOnErrorRector::class,
NullToStrictStringFuncCallArgRector::class,
])
->withParallel()
->withCache(sys_get_temp_dir().'/rector_monorepo_cache')
;
55 changes: 19 additions & 36 deletions src/Command/ComposerJsonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,20 @@

class ComposerJsonCommand extends Command
{
/**
* @var string
*/
private $rootDir;

/**
* @var array
*/
private $config;
private array $config;

/**
* @var array<string, array<string, string>>
*/
private $replacedPackages = [];
private array $replacedPackages = [];

/**
* @var array<string, string>
*/
private $prettyVersionConstraints = [];
private array $prettyVersionConstraints = [];

public function __construct(string $rootDir)
public function __construct(private readonly string $rootDir)
{
$this->rootDir = $rootDir;

parent::__construct();
}

Expand Down Expand Up @@ -83,7 +73,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($input->getOption('validate')) {
$invalid = $this->validateJsons($rootJson, $splitJsons);

if (0 === \count($invalid)) {
if ([] === $invalid) {
$io->success('All composer.json files are up to date.');

return 0;
Expand All @@ -108,7 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$updated = $this->updateJsons($rootJson, $splitJsons);

if (0 === \count($updated)) {
if ([] === $updated) {
$io->success('All composer.json files are up to date.');

return 0;
Expand Down Expand Up @@ -224,13 +214,11 @@ function ($folder) {
array_values($rootJson['require-dev'] ?? []),
array_values($rootJson['conflict'] ?? []),
array_merge(...array_values(array_map(
static function ($json) {
return array_merge(
array_values($json['require'] ?? []),
array_values($json['require-dev'] ?? []),
array_values($json['conflict'] ?? []),
);
},
static fn ($json) => array_merge(
array_values($json['require'] ?? []),
array_values($json['require-dev'] ?? []),
array_values($json['conflict'] ?? []),
),
$jsons,
))),
));
Expand Down Expand Up @@ -277,7 +265,6 @@ static function ($json) {
$rootJson['require-dev'][$packageName],
$versionConstraint,
],
$packageName,
);

unset($rootJson['require-dev'][$packageName]);
Expand Down Expand Up @@ -329,7 +316,7 @@ static function ($json) {

// Remove empty arrays
foreach ($rootJson as $key => $value) {
if (\is_array($value) && 0 === \count($value)) {
if (\is_array($value) && [] === $value) {
unset($rootJson[$key]);
}
}
Expand Down Expand Up @@ -366,7 +353,7 @@ private function combineDependecies(array $requireArrays, array $ignorePackages
}

foreach ($requires as $packageName => $constraints) {
$requires[$packageName] = $this->combineConstraints($constraints, $packageName, $conjunctive);
$requires[$packageName] = $this->combineConstraints($constraints, $conjunctive);
}

uksort(
Expand All @@ -381,15 +368,15 @@ static function ($a, $b) {
}

if (
(0 === strncmp($a, 'ext-', 4) && 0 !== strncmp($b, 'ext-', 4))
|| (0 === strncmp($a, 'lib-', 4) && 0 !== strncmp($b, 'lib-', 4))
(str_starts_with($a, 'ext-') && !str_starts_with($b, 'ext-'))
|| (str_starts_with($a, 'lib-') && !str_starts_with($b, 'lib-'))
) {
return -1;
}

if (
(0 !== strncmp($a, 'ext-', 4) && 0 === strncmp($b, 'ext-', 4))
|| (0 !== strncmp($a, 'lib-', 4) && 0 === strncmp($b, 'lib-', 4))
(!str_starts_with($a, 'ext-') && str_starts_with($b, 'ext-'))
|| (!str_starts_with($a, 'lib-') && str_starts_with($b, 'lib-'))
) {
return 1;
}
Expand All @@ -409,7 +396,7 @@ static function ($a, $b) {
return $requires;
}

private function combineConstraints(array $constraints, string $name, bool $conjunctive = true): string
private function combineConstraints(array $constraints, bool $conjunctive = true): string
{
$parsedConstraints = [];
$versionParser = new VersionParser();
Expand All @@ -420,11 +407,7 @@ private function combineConstraints(array $constraints, string $name, bool $conj

$compact = (string) Intervals::compactConstraint(MultiConstraint::create($parsedConstraints, $conjunctive));

if (isset($this->prettyVersionConstraints[$compact])) {
return $this->prettyVersionConstraints[$compact];
}

return str_replace(['[', ']'], '', $compact);
return $this->prettyVersionConstraints[$compact] ?? str_replace(['[', ']'], '', $compact);
}

/**
Expand Down
9 changes: 1 addition & 8 deletions src/Command/MergeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,8 @@

class MergeCommand extends Command
{
/**
* @var string
*/
private $rootDir;

public function __construct(string $rootDir)
public function __construct(private readonly string $rootDir)
{
$this->rootDir = $rootDir;

parent::__construct();
}

Expand Down
11 changes: 2 additions & 9 deletions src/Command/SplitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@

class SplitCommand extends Command
{
/**
* @var string
*/
private $rootDir;

public function __construct(string $rootDir)
public function __construct(private readonly string $rootDir)
{
$this->rootDir = $rootDir;

parent::__construct();
}

Expand Down Expand Up @@ -81,7 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function addAuthToken($repoUrl): string
{
if (($token = getenv('GITHUB_TOKEN')) && 0 === strncmp($repoUrl, 'https://github.com/', 19)) {
if (($token = getenv('GITHUB_TOKEN')) && str_starts_with($repoUrl, 'https://github.com/')) {
return 'https://'.$token.'@github.com/'.substr($repoUrl, 19);
}

Expand Down
28 changes: 11 additions & 17 deletions src/Git/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@

class Commit extends GitObject
{
/**
* @var string
*/
private $tree;
private string $tree;

/**
* @var array
*/
private $parents = [];
private array $parents = [];

public function __construct(string $rawCommit)
{
Expand All @@ -33,9 +27,9 @@ public function __construct(string $rawCommit)
break;
}

if (0 === strncmp($line, 'tree ', 5)) {
if (str_starts_with($line, 'tree ')) {
$this->tree = substr($line, 5, 40);
} elseif (0 === strncmp($line, 'parent ', 7)) {
} elseif (str_starts_with($line, 'parent ')) {
$this->parents[] = substr($line, 7, 40);
}
}
Expand All @@ -61,7 +55,7 @@ public function getCommitterDate(): \DateTime
break;
}

if (0 === strncmp($line, 'committer ', 10)) {
if (str_starts_with($line, 'committer ')) {
$parts = explode(' ', $line);

$date = \DateTime::createFromFormat(
Expand Down Expand Up @@ -99,7 +93,7 @@ public function hasGpgSignature(): bool
break;
}

if (0 === strncmp($line, 'gpgsig ', 7)) {
if (str_starts_with($line, 'gpgsig ')) {
return true;
}
}
Expand Down Expand Up @@ -128,7 +122,7 @@ public function withTree(string $hash): self
break;
}

if (0 === strncmp($line, 'tree ', 5)) {
if (str_starts_with($line, 'tree ')) {
$raw[$num] = 'tree '.$hash;
break;
}
Expand All @@ -146,11 +140,11 @@ public function withParents(array $hashes): self
break;
}

if (0 === strncmp($line, 'tree ', 5)) {
if (\count($hashes)) {
if (str_starts_with($line, 'tree ')) {
if ([] !== $hashes) {
$raw[$num] .= "\nparent ".implode("\nparent ", $hashes);
}
} elseif (0 === strncmp($line, 'parent ', 7)) {
} elseif (str_starts_with($line, 'parent ')) {
unset($raw[$num]);
}
}
Expand Down Expand Up @@ -179,7 +173,7 @@ public function withoutGpgSignature(): self

$signatureStartFound = false;

if (0 === strncmp($line, 'gpgsig ', 7)) {
if (str_starts_with($line, 'gpgsig ')) {
$signatureStartFound = true;
unset($raw[$num]);
}
Expand Down
8 changes: 1 addition & 7 deletions src/Git/GitObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@

abstract class GitObject
{
/**
* @var string
*/
private $raw;

public function __construct(string $rawData)
public function __construct(private readonly string $raw)
{
$this->raw = $rawData;
}

public function getHash(): string
Expand Down
Loading

0 comments on commit e740353

Please sign in to comment.