Skip to content

remove support php74 and phpstan level 9 #18

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

Merged
merged 6 commits into from
Jan 6, 2023
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [8.2, 8.1, 8.0, 7.4]
php: [8.2, 8.1, 8.0]

name: P${{ matrix.php }}

Expand All @@ -25,6 +25,6 @@ jobs:
- name: Install dependencies
run: |
composer update --prefer-dist --no-interaction

- name: Execute tests
run: composer test
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"license": "MIT",
"require": {
"php": "^7.4 || ^8.0"
"php": "^8.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ parameters:
- tests

# The level 9 is the highest level
level: 5
level: 9
6 changes: 5 additions & 1 deletion src/Compress/Gz.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public function handler(string $filename): string
}

while (!feof($fd)) {
gzwrite($gz, fread($fd, 1024 * 512));
$data = fread($fd, 1024 * 512);

$data = $data === false ? '' : $data;

gzwrite($gz, $data);
}

gzclose($gz);
Expand Down
6 changes: 3 additions & 3 deletions src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait ErrorHandler
* The first argument will be the name of the destination file and
* the second the name of the rotated file.
*/
public function then(callable $callable): self
public function then(Closure $callable): self
{
$this->thenCallback = $callable;

Expand All @@ -31,7 +31,7 @@ public function then(callable $callable): self
/**
* Call function if roteted catch any Exception.
*/
public function catch(callable $callable): self
public function catch(Closure $callable): self
{
$this->catchCallable = $callable;

Expand All @@ -41,7 +41,7 @@ public function catch(callable $callable): self
/**
* Function that will be executed when the process was finished.
*/
public function finally(callable $callable): self
public function finally(Closure $callable): self
{
$this->finallyCallback = $callable;

Expand Down
12 changes: 9 additions & 3 deletions src/Optionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

trait Optionable
{
private $validMethods = [];
/**
* @var string[]
*/
private array $validMethods = [];

/**
* Set options
*
* @param array $options
* @param mixed[] $options
* @throws LogicException
* @return self
*/
Expand All @@ -24,14 +27,17 @@ public function options(array $options): self
return $this;
}

/**
* @param string[] $methods
*/
protected function methodsOptionables(array $methods): self
{
$this->validMethods = $methods;

return $this;
}

private function setMethod($key, $value)
private function setMethod(string $key, mixed $value): void
{
$method = $this->convert($key);

Expand Down
2 changes: 1 addition & 1 deletion src/Processors/AbstractProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function removeExtention(string $extension): void
$this->extension = str_replace('.'.$extension, '', $this->extension);
}

public function setFilenameSource($filenameSource): self
public function setFilenameSource(string $filenameSource): self
{
$this->filenameSource = $filenameSource;

Expand Down
2 changes: 1 addition & 1 deletion src/Processors/RotativeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class RotativeProcessor extends AbstractProcessor
{
private $maxFiles = 366;
private int $maxFiles = 366;

/**
* Log files are rotated count times before being removed.
Expand Down
70 changes: 55 additions & 15 deletions src/Rotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Rotation

private bool $_truncate = false;

/**
* @param mixed[] $options
*/
public function __construct(array $options = [])
{
$this->processor = new RotativeProcessor();
Expand Down Expand Up @@ -199,25 +202,15 @@ private function copyAndTruncate(string $filename): ?string
{
clearstatcache();

$filenameTarget = tempnam(dirname($filename), 'LOG');

$fd = fopen($filename, 'r+');

if ($fd === false) {
$this->exception(
new Exception(sprintf('the file %s not can open.', $filename), 20)
);
$filenameTarget = $this->getTempFilename(dirname($filename));

if (!$filenameTarget) {
return null;
}

if (!flock($fd, LOCK_EX)) {
fclose($fd);

$this->exception(
new Exception(sprintf('the file %s not can lock.', $filename), 21)
);
$fd = $this->openFileWithLock($filename);

if (!$fd) {
return null;
}

Expand Down Expand Up @@ -259,7 +252,11 @@ private function move(string $filename): ?string
{
clearstatcache();

$filenameTarget = tempnam(dirname($filename), 'LOG');
$filenameTarget = $this->getTempFilename(dirname($filename));

if (!$filenameTarget) {
return null;
}

if (!rename($filename, $filenameTarget)) {
$this->exception(
Expand All @@ -274,4 +271,47 @@ private function move(string $filename): ?string

return $filenameTarget;
}

private function getTempFilename(string $path): ?string
{
$filename = tempnam($path, 'LOG');

if ($filename === false) {
$this->exception(
new Exception(sprintf('the file %s not can create temp file.', $path), 19)
);

return null;
}

return $filename;
}

/**
* @return null|resource
*/
private function openFileWithLock(string $filename)
{
$fd = fopen($filename, 'r+');

if ($fd === false) {
$this->exception(
new Exception(sprintf('the file %s not can open.', $filename), 20)
);

return null;
}

if (!flock($fd, LOCK_EX)) {
fclose($fd);

$this->exception(
new Exception(sprintf('the file %s not can lock.', $filename), 21)
);

return null;
}

return $fd;
}
}
4 changes: 2 additions & 2 deletions tests/Compress/GzTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class GzTest extends TestCase
{
public function testRotationProcessorWithGzProcessor()
public function testRotationProcessorWithGzProcessor(): void
{
$rotation = new Rotation();

Expand All @@ -31,6 +31,6 @@ public function testRotationProcessorWithGzProcessor()

$this->assertFileExists(self::DIR_WORK.'file.log.1.gz');

$this->assertEquals($content, implode('', gzfile(self::DIR_WORK.'file.log.1.gz')));
$this->assertEquals($content, implode('', (array)gzfile(self::DIR_WORK.'file.log.1.gz')));
}
}
14 changes: 7 additions & 7 deletions tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class ErrorHandlerTest extends TestCase
{
public function testCallThenIfRotateWasSucessfull()
public function testCallThenIfRotateWasSuccessful(): void
{
file_put_contents(self::DIR_WORK.'file.log', microtime(true));

Expand All @@ -22,7 +22,7 @@ public function testCallThenIfRotateWasSucessfull()
$this->assertTrue($thenCalled);
}

public function testNotCallThenIfRotateNotWasSucessfull()
public function testNotCallThenIfRotateNotWasSuccessful(): void
{
$rotation = new Rotation();

Expand All @@ -35,7 +35,7 @@ public function testNotCallThenIfRotateNotWasSucessfull()
$this->assertFalse($thenCalled);
}

public function testThrowsException()
public function testThrowsException(): void
{
$this->expectException(RotationFailed::class);

Expand All @@ -49,7 +49,7 @@ public function testThrowsException()
$this->assertFalse($result);
}

public function testCatchException()
public function testCatchException(): void
{
$rotation = new Rotation();

Expand All @@ -68,7 +68,7 @@ public function testCatchException()
$this->assertFalse($result);
}

public function testCallFinallyIfRotateWasSucessfull()
public function testCallFinallyIfRotateWasSuccessful(): void
{
file_put_contents(self::DIR_WORK.'file.log', microtime(true));

Expand All @@ -83,7 +83,7 @@ public function testCallFinallyIfRotateWasSucessfull()
$this->assertTrue($finallyCalled);
}

public function testCallFinallyIfFileDontExists()
public function testCallFinallyIfFileDontExists(): void
{
$rotation = new Rotation();

Expand All @@ -96,7 +96,7 @@ public function testCallFinallyIfFileDontExists()
$this->assertTrue($finallyCalled);
}

public function testCallFinallyIfThrowException()
public function testCallFinallyIfThrowException(): void
{
$this->expectException(RotationFailed::class);

Expand Down
4 changes: 2 additions & 2 deletions tests/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class OptionTest extends TestCase
{
public function testPassOptions()
public function testPassOptions(): void
{
$rotation = new Rotation([
'files' => 1,
Expand All @@ -21,7 +21,7 @@ public function testPassOptions()
$this->assertNotNull($rotation);
}

public function testCatchExceptioIfMethodIsNotPermited()
public function testCatchExceptionIfMethodIsNotPermitted(): void
{
$this->expectException(\LogicException::class);

Expand Down
4 changes: 2 additions & 2 deletions tests/Processors/RotativeProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class RotativeProcessorTest extends TestCase
{
public function testRotationProcessor()
public function testRotationProcessor(): void
{
$maxFiles = 5;

Expand All @@ -29,7 +29,7 @@ public function testRotationProcessor()
$this->assertFalse(is_file(self::DIR_WORK.'file.log.'.($maxFiles + 1)));
}

public function testRotationProcessorWithGzProcessor()
public function testRotationProcessorWithGzProcessor(): void
{
$maxFiles = 5;

Expand Down
Loading