Skip to content

Commit

Permalink
build(code quality): add rector package
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot committed Apr 14, 2024
1 parent 22e3a38 commit ee15ed3
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 96 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ name: PHPStan

on:
push:
branches: [main]
paths:
- '**.php'
- 'ruleset-phpstan.neon'
pull_request:
branches: [main]
paths:
- '**.php'
- 'ruleset-phpstan.neon'


jobs:
phpstan:
Expand All @@ -16,7 +23,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
php-version: 8.2
coverage: none

- name: Install composer dependencies
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/rector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Rector

on:
push:
branches: [main]
pull_request:
branches: [main]


jobs:
rector:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Rector Cache
uses: actions/cache@v3
with:
path: /tmp/rector
key: ${{ runner.os }}-rector-${{ github.run_id }}
restore-keys: ${{ runner.os }}-rector-

- run: mkdir -p /tmp/rector

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v3

- name: Rector Dry Run
run: php vendor/bin/rector process --dry-run --config=rector.php


3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^3",
"larastan/larastan": "^2",
"orchestra/testbench": ">=9"
"orchestra/testbench": ">=9",
"rector/rector": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
39 changes: 39 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

use Rector\Caching\ValueObject\Storage\FileCacheStorage;
use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;

return RectorConfig::configure()
->withParallel()
->withPaths([
__DIR__.'/config',
__DIR__.'/resources',
__DIR__.'/routes',
__DIR__.'/src',
// __DIR__.'/tests',
])
->withPhpSets()
->withPreparedSets(
deadCode: true,
codeQuality: true,
// codingStyle: true,
typeDeclarations: true,
privatization: true,
naming: true,
// instanceof: true,
earlyReturn: true,
// strictBooleans: true,
)
->withRules([
AddVoidReturnTypeWhereNoReturnRector::class,
])
->withCache(
// ensure file system caching is used instead of in-memory
cacheClass: FileCacheStorage::class,

// specify a path that works locally as well as on CI job runners
cacheDirectory: '/tmp/rector'
);
4 changes: 2 additions & 2 deletions routes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Route::prefix(config('env-editor.route.prefix'))
->middleware(config('env-editor.route.middleware'))
->group(function () use ($routeMainName) {
->group(function () use ($routeMainName): void {
Route::get('/', [EnvController::class, 'index'])->name($routeMainName.'.index');

Route::post('key', [EnvController::class, 'addKey'])->name($routeMainName.'.key');
Expand All @@ -17,7 +17,7 @@

Route::delete('clear-cache', [EnvController::class, 'clearConfigCache'])->name($routeMainName.'.clearConfigCache');

Route::prefix('files')->group(function () use ($routeMainName) {
Route::prefix('files')->group(function () use ($routeMainName): void {
Route::get('/', [EnvController::class, 'getBackupFiles'])
->name($routeMainName.'.getBackups');
Route::post('create-backup', [EnvController::class, 'createBackup'])
Expand Down
16 changes: 8 additions & 8 deletions src/Controllers/EnvController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public function __construct(
*/
public function index(Request $request)
{
$envValues = $this->envEditor->getEnvFileContent();
$envFileContent = $this->envEditor->getEnvFileContent();
if ($request->wantsJson()) {
return $this->returnGenericResponse(true, ['items' => $envValues]);
return $this->returnGenericResponse(true, ['items' => $envFileContent]);
}

return view(ServiceProvider::PACKAGE.'::index', compact('envValues'));
return view(ServiceProvider::PACKAGE.'::index', ['envValues' => $envFileContent]);
}

/**
Expand Down Expand Up @@ -73,12 +73,12 @@ public function deleteKey(Request $request): JsonResponse
*/
public function getBackupFiles(Request $request): View|JsonResponse
{
$backUpFiles = $this->envEditor->getAllBackUps();
$allBackUps = $this->envEditor->getAllBackUps();
if ($request->wantsJson()) {
return $this->returnGenericResponse(true, ['items' => $backUpFiles]);
return $this->returnGenericResponse(true, ['items' => $allBackUps]);
}

return view(ServiceProvider::PACKAGE.'::index', compact('backUpFiles'));
return view(ServiceProvider::PACKAGE.'::index', ['backUpFiles' => $allBackUps]);
}

/**
Expand Down Expand Up @@ -157,10 +157,10 @@ public function clearConfigCache(): JsonResponse
protected function returnGenericResponse(
bool $success,
array $data = [],
string $translationWord = '',
?string $translationWord = null,
string $keyName = ''
): JsonResponse {
if (!empty($translationWord) && $success) {
if ($translationWord && $success) {
$data = array_merge($data, [
'message' => __(
ServiceProvider::TRANSLATE_PREFIX."controllerMessages.$translationWord",
Expand Down
7 changes: 2 additions & 5 deletions src/EnvEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@
*/
class EnvEditor
{
protected Repository $config;

protected EnvKeysManager $keysManager;

protected EnvFilesManager $filesManager;

protected EnvFileContentManager $fileContentManager;

public function __construct(Repository $config, Filesystem $filesystem)
public function __construct(protected Repository $configRepository, Filesystem $filesystem)
{
$this->config = $config;
$this->keysManager = new EnvKeysManager($this);
$this->filesManager = new EnvFilesManager($this, $filesystem);
$this->fileContentManager = new EnvFileContentManager($this, $filesystem);
Expand Down Expand Up @@ -157,7 +154,7 @@ public function restoreBackUp(string $fileName): bool

public function config(string $key, mixed $default = null): mixed
{
return $this->config->get($key, $default);
return $this->configRepository->get($key, $default);
}

public function getKeysManager(): EnvKeysManager
Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/EnvException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class EnvException extends \Exception
{
public function __toString()
public function __toString(): string
{
return __CLASS__.":[{$this->code}]: {$this->message}\n";
return self::class.":[{$this->code}]: {$this->message}\n";
}
}
20 changes: 1 addition & 19 deletions src/Helpers/EntryObj.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,11 @@

class EntryObj implements \JsonSerializable
{
public string $key;

/**
* @var int|string|null
*/
protected mixed $value;

public int $group = 0;

public int $index = 0;

protected bool $isSeparator = false;

/**
* @param int|string|null $value
*/
public function __construct(string $key, mixed $value, int $group, int $index, bool $isSeparator = false)
public function __construct(public string $key, protected mixed $value, public int $group, public int $index, protected bool $isSeparator = false)
{
$this->key = $key;
$this->value = $value;
$this->group = $group;
$this->index = $index;
$this->isSeparator = $isSeparator;
}

public static function parseEnvLine(string $line, int $group, int $index): self
Expand Down
14 changes: 4 additions & 10 deletions src/Helpers/EnvFileContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@

class EnvFileContentManager
{
protected EnvEditor $envEditor;

protected Filesystem $filesystem;

public function __construct(EnvEditor $envEditor, Filesystem $filesystem)
public function __construct(protected EnvEditor $envEditor, protected Filesystem $filesystem)
{
$this->envEditor = $envEditor;
$this->filesystem = $filesystem;
}

/**
Expand Down Expand Up @@ -62,7 +56,7 @@ protected function getFileContents(string $file = ''): string

try {
return $this->filesystem->get($envFile);
} catch (\Exception $e) {
} catch (\Exception) {
throw new EnvException(__(ServiceProvider::TRANSLATE_PREFIX.'exceptions.fileNotExists', ['name' => $envFile]), 2);
}
}
Expand All @@ -77,8 +71,8 @@ protected function getFileContents(string $file = ''): string
public function save(Collection $envValues, string $fileName = ''): bool
{
$env = $envValues
->sortBy(fn (EntryObj $item) => $item->index)
->map(fn (EntryObj $item) => $item->getAsEnvLine());
->sortBy(fn (EntryObj $entryObj): int => $entryObj->index)
->map(fn (EntryObj $entryObj): string => $entryObj->getAsEnvLine());

$content = implode(PHP_EOL, $env->toArray());

Expand Down
18 changes: 6 additions & 12 deletions src/Helpers/EnvFilesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@

class EnvFilesManager
{
protected EnvEditor $envEditor;

protected Filesystem $filesystem;

public function __construct(EnvEditor $envEditor, Filesystem $filesystem)
public function __construct(protected EnvEditor $envEditor, protected Filesystem $filesystem)
{
$this->envEditor = $envEditor;
$this->filesystem = $filesystem;
$this->makeBackupsDirectory();
}

Expand Down Expand Up @@ -74,7 +68,7 @@ public function backUpCurrentEnv(): bool
*/
public function restoreBackup(string $fileName): bool
{
if (empty($fileName)) {
if ('' === $fileName) {
throw new EnvException(__(ServiceProvider::TRANSLATE_PREFIX.'exceptions.provideFileName'), 1);
}
$file = $this->getBackupsDir($fileName);
Expand All @@ -99,7 +93,7 @@ public function upload(UploadedFile $uploadedFile, bool $replaceCurrentEnv): Fil
*/
public function deleteBackup(string $fileName): bool
{
if (empty($fileName)) {
if ('' === $fileName) {
throw new EnvException(__(ServiceProvider::TRANSLATE_PREFIX.'exceptions.provideFileName'), 1);
}
$file = $this->getFilePath($fileName);
Expand All @@ -114,7 +108,7 @@ public function deleteBackup(string $fileName): bool
*/
public function getFilePath(string $fileName = ''): string
{
$path = (empty($fileName))
$path = ('' === $fileName)
? $this->getEnvFileName()
: $this->getBackupsDir($fileName);

Expand All @@ -141,12 +135,12 @@ protected function getEnvFileName(): string
return app()->environmentFilePath();
}

public function getBackupsDir(string $path = ''): string
public function getBackupsDir(?string $path = null): string
{
return $this->envEditor->config('paths.backupDirectory').($path ? DIRECTORY_SEPARATOR.$path : $path);
}

public function getEnvDir(string $path = ''): string
public function getEnvDir(?string $path = null): string
{
return dirname($this->getEnvFileName()).($path ? DIRECTORY_SEPARATOR.$path : $path);
}
Expand Down
Loading

0 comments on commit ee15ed3

Please sign in to comment.