Skip to content

Commit

Permalink
Add rector [batch] (#47)
Browse files Browse the repository at this point in the history
* Add rector files yiisoft/yii-dev-tool#232

* Add rector/rector dependecy

* [rector] Apply fixes

* Apply fixes from StyleCI

* Use predefined rector action

* fix

* Apply fixes from StyleCI

Co-authored-by: rector-bot <rector@yiiframework.com>
Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Sergei Predvoditelev <sergei@predvoditelev.ru>
  • Loading branch information
4 people committed Dec 17, 2022
1 parent f0f5706 commit bafa061
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 63 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/rector.yml
@@ -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']
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,7 @@

## 2.0.2 under development

- Enh: Add composer require checker into CI
- no changes in this release.

## 2.0.1 July 25, 2022

Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -27,6 +27,7 @@
"require-dev": {
"maglnet/composer-require-checker": "^4.2",
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.15.1",
"roave/infection-static-analysis-plugin": "^1.25",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.2",
Expand Down
22 changes: 22 additions & 0 deletions rector.php
@@ -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,
]);
};
24 changes: 5 additions & 19 deletions src/FileRotator.php
Expand Up @@ -54,13 +54,6 @@ final class FileRotator implements FileRotatorInterface
*/
private int $maxFiles;

/**
* @var int|null The permission to be set for newly created files.
* This value will be used by PHP chmod() function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
*/
private ?int $fileMode;

/**
* @var bool Whether to compress rotated files with gzip. Defaults to `false`.
*
Expand All @@ -69,23 +62,24 @@ final class FileRotator implements FileRotatorInterface
private bool $compressRotatedFiles;

/**
* @param int $maxFileSize The maximum file size, in kilo-bytes. Defaults to 10240, meaning 10MB.
* @param int $maxFileSize The maximum file size, in kilobytes. Defaults to 10240, meaning 10MB.
* @param int $maxFiles The number of files used for rotation. Defaults to 5.
* @param int|null $fileMode The permission to be set for newly created files.
* @param int|null $fileMode The permission to be set for newly created files. This value will be used by PHP
* `chmod()` function. No umask will be applied. If not set, the permission will be determined by the current
* environment.
* @param bool $compressRotatedFiles Whether to compress rotated files with gzip.
*/
public function __construct(
int $maxFileSize = 10240,
int $maxFiles = 5,
int $fileMode = null,
private ?int $fileMode = null,
bool $compressRotatedFiles = false
) {
$this->checkCannotBeLowerThanOne($maxFileSize, '$maxFileSize');
$this->checkCannotBeLowerThanOne($maxFiles, '$maxFiles');

$this->maxFileSize = $maxFileSize;
$this->maxFiles = $maxFiles;
$this->fileMode = $fileMode;

if ($compressRotatedFiles && !extension_loaded('zlib')) {
throw new RuntimeException(sprintf(
Expand Down Expand Up @@ -154,8 +148,6 @@ private function rotate(string $rotateFile, string $newFile): void

/**
* Compresses a file with gzip and renames it by appending `.gz` to the file.
*
* @param string $file
*/
private function compress(string $file): void
{
Expand Down Expand Up @@ -191,8 +183,6 @@ private function clear(string $file): void

/**
* Checks the existence of file and removes it.
*
* @param string $file
*/
private function safeRemove(string $file): void
{
Expand All @@ -203,10 +193,6 @@ private function safeRemove(string $file): void

/**
* Whether the file is compressed.
*
* @param string $file
*
* @return bool
*/
private function isCompressed(string $file): bool
{
Expand Down
46 changes: 12 additions & 34 deletions src/FileTarget.php
Expand Up @@ -38,44 +38,22 @@
final class FileTarget extends Target
{
/**
* @var string The log file path. If not set, it will use the "/tmp/app.log" file.
* The directory containing the log files will be automatically created if not existing.
*/
private string $logFile;

/**
* @var int The permission to be set for newly created directories.
* This value will be used by PHP chmod() function. No umask will be applied.
* Defaults to 0775, meaning the directory is read-writable by owner and group,
* but read-only for other users.
*/
private int $dirMode;

/**
* @var int|null The permission to be set for newly created log files.
* This value will be used by PHP chmod() function. No umask will be applied.
* If not set, the permission will be determined by the current environment.
*/
private ?int $fileMode;

private ?FileRotatorInterface $rotator;

/**
* @param string $logFile The log file path. If not set, it will use the "/tmp/app.log" file.
* @param string $logFile The log file path. If not set, it will use the "/tmp/app.log" file. The directory
* containing the log files will be automatically created if not existing.
* @param FileRotatorInterface|null $rotator The instance that takes care of rotating files.
* @param int $dirMode The permission to be set for newly created directories.
* @param int|null $fileMode The permission to be set for newly created log files.
* @param int $dirMode The permission to be set for newly created directories. This value will be used by PHP
* `chmod()` function. No umask will be applied. Defaults to 0775, meaning the directory is read-writable by owner
* and group, but read-only for other users.
* @param int|null $fileMode The permission to be set for newly created log files. This value will be used by PHP
* `chmod()` function. No umask will be applied. If not set, the permission will be determined by the current
* environment.
*/
public function __construct(
string $logFile = '/tmp/app.log',
FileRotatorInterface $rotator = null,
int $dirMode = 0775,
int $fileMode = null
private string $logFile = '/tmp/app.log',
private ?FileRotatorInterface $rotator = null,
private int $dirMode = 0775,
private ?int $fileMode = null
) {
$this->logFile = $logFile;
$this->rotator = $rotator;
$this->dirMode = $dirMode;
$this->fileMode = $fileMode;
parent::__construct();
}

Expand Down
6 changes: 0 additions & 6 deletions tests/FileRotatorTest.php
Expand Up @@ -42,8 +42,6 @@ public function rotateDataProvider(): array

/**
* @dataProvider rotateDataProvider
*
* @param bool $compress
*/
public function testRotate(bool $compress): void
{
Expand Down Expand Up @@ -75,8 +73,6 @@ public function testRotate(bool $compress): void

/**
* @dataProvider rotateDataProvider
*
* @param bool $compress
*/
public function testRotateMaxFiles(bool $compress): void
{
Expand Down Expand Up @@ -108,8 +104,6 @@ public function testRotateMaxFiles(bool $compress): void

/**
* @dataProvider rotateDataProvider
*
* @param bool $compress
*/
public function testRotateMaxFileSize(bool $compress): void
{
Expand Down
6 changes: 3 additions & 3 deletions tests/FileTargetTest.php
Expand Up @@ -56,9 +56,9 @@ public function testExportMessagesWithSetFormat(): void
{
$logFile = $this->getLogFilePath();
$target = new FileTarget($logFile, null, 0777, 0777);
$target->setFormat(function (Message $message) {
return "[{$message->level()}][{$message->context('category')}] {$message->message()}";
});
$target->setFormat(
fn (Message $message) => "[{$message->level()}][{$message->context('category')}] {$message->message()}"
);
$target->collect(
[
new Message(LogLevel::INFO, 'text-1', ['category' => 'category-1']),
Expand Down

0 comments on commit bafa061

Please sign in to comment.