Skip to content

Commit 35250de

Browse files
authored
remove support php74 and phpstan level 9 (#18)
* remove support to php74 * phpstan level 9 * fix optionable
1 parent 7237745 commit 35250de

15 files changed

+105
-51
lines changed

.github/workflows/tests.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
fail-fast: false
1010
matrix:
11-
php: [8.2, 8.1, 8.0, 7.4]
11+
php: [8.2, 8.1, 8.0]
1212

1313
name: P${{ matrix.php }}
1414

@@ -25,6 +25,6 @@ jobs:
2525
- name: Install dependencies
2626
run: |
2727
composer update --prefer-dist --no-interaction
28-
28+
2929
- name: Execute tests
3030
run: composer test

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
],
1717
"license": "MIT",
1818
"require": {
19-
"php": "^7.4 || ^8.0"
19+
"php": "^8.0"
2020
},
2121
"autoload": {
2222
"psr-4": {

phpstan.neon.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ parameters:
55
- tests
66

77
# The level 9 is the highest level
8-
level: 5
8+
level: 9

src/Compress/Gz.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public function handler(string $filename): string
2727
}
2828

2929
while (!feof($fd)) {
30-
gzwrite($gz, fread($fd, 1024 * 512));
30+
$data = fread($fd, 1024 * 512);
31+
32+
$data = $data === false ? '' : $data;
33+
34+
gzwrite($gz, $data);
3135
}
3236

3337
gzclose($gz);

src/ErrorHandler.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait ErrorHandler
2121
* The first argument will be the name of the destination file and
2222
* the second the name of the rotated file.
2323
*/
24-
public function then(callable $callable): self
24+
public function then(Closure $callable): self
2525
{
2626
$this->thenCallback = $callable;
2727

@@ -31,7 +31,7 @@ public function then(callable $callable): self
3131
/**
3232
* Call function if roteted catch any Exception.
3333
*/
34-
public function catch(callable $callable): self
34+
public function catch(Closure $callable): self
3535
{
3636
$this->catchCallable = $callable;
3737

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

src/Optionable.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
trait Optionable
88
{
9-
private $validMethods = [];
9+
/**
10+
* @var string[]
11+
*/
12+
private array $validMethods = [];
1013

1114
/**
1215
* Set options
1316
*
14-
* @param array $options
17+
* @param mixed[] $options
1518
* @throws LogicException
1619
* @return self
1720
*/
@@ -24,14 +27,17 @@ public function options(array $options): self
2427
return $this;
2528
}
2629

30+
/**
31+
* @param string[] $methods
32+
*/
2733
protected function methodsOptionables(array $methods): self
2834
{
2935
$this->validMethods = $methods;
3036

3137
return $this;
3238
}
3339

34-
private function setMethod($key, $value)
40+
private function setMethod(string $key, mixed $value): void
3541
{
3642
$method = $this->convert($key);
3743

src/Processors/AbstractProcessor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function removeExtention(string $extension): void
2525
$this->extension = str_replace('.'.$extension, '', $this->extension);
2626
}
2727

28-
public function setFilenameSource($filenameSource): self
28+
public function setFilenameSource(string $filenameSource): self
2929
{
3030
$this->filenameSource = $filenameSource;
3131

src/Processors/RotativeProcessor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class RotativeProcessor extends AbstractProcessor
66
{
7-
private $maxFiles = 366;
7+
private int $maxFiles = 366;
88

99
/**
1010
* Log files are rotated count times before being removed.

src/Rotation.php

+55-15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class Rotation
1919

2020
private bool $_truncate = false;
2121

22+
/**
23+
* @param mixed[] $options
24+
*/
2225
public function __construct(array $options = [])
2326
{
2427
$this->processor = new RotativeProcessor();
@@ -199,25 +202,15 @@ private function copyAndTruncate(string $filename): ?string
199202
{
200203
clearstatcache();
201204

202-
$filenameTarget = tempnam(dirname($filename), 'LOG');
203-
204-
$fd = fopen($filename, 'r+');
205-
206-
if ($fd === false) {
207-
$this->exception(
208-
new Exception(sprintf('the file %s not can open.', $filename), 20)
209-
);
205+
$filenameTarget = $this->getTempFilename(dirname($filename));
210206

207+
if (!$filenameTarget) {
211208
return null;
212209
}
213210

214-
if (!flock($fd, LOCK_EX)) {
215-
fclose($fd);
216-
217-
$this->exception(
218-
new Exception(sprintf('the file %s not can lock.', $filename), 21)
219-
);
211+
$fd = $this->openFileWithLock($filename);
220212

213+
if (!$fd) {
221214
return null;
222215
}
223216

@@ -259,7 +252,11 @@ private function move(string $filename): ?string
259252
{
260253
clearstatcache();
261254

262-
$filenameTarget = tempnam(dirname($filename), 'LOG');
255+
$filenameTarget = $this->getTempFilename(dirname($filename));
256+
257+
if (!$filenameTarget) {
258+
return null;
259+
}
263260

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

275272
return $filenameTarget;
276273
}
274+
275+
private function getTempFilename(string $path): ?string
276+
{
277+
$filename = tempnam($path, 'LOG');
278+
279+
if ($filename === false) {
280+
$this->exception(
281+
new Exception(sprintf('the file %s not can create temp file.', $path), 19)
282+
);
283+
284+
return null;
285+
}
286+
287+
return $filename;
288+
}
289+
290+
/**
291+
* @return null|resource
292+
*/
293+
private function openFileWithLock(string $filename)
294+
{
295+
$fd = fopen($filename, 'r+');
296+
297+
if ($fd === false) {
298+
$this->exception(
299+
new Exception(sprintf('the file %s not can open.', $filename), 20)
300+
);
301+
302+
return null;
303+
}
304+
305+
if (!flock($fd, LOCK_EX)) {
306+
fclose($fd);
307+
308+
$this->exception(
309+
new Exception(sprintf('the file %s not can lock.', $filename), 21)
310+
);
311+
312+
return null;
313+
}
314+
315+
return $fd;
316+
}
277317
}

tests/Compress/GzTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class GzTest extends TestCase
99
{
10-
public function testRotationProcessorWithGzProcessor()
10+
public function testRotationProcessorWithGzProcessor(): void
1111
{
1212
$rotation = new Rotation();
1313

@@ -31,6 +31,6 @@ public function testRotationProcessorWithGzProcessor()
3131

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

34-
$this->assertEquals($content, implode('', gzfile(self::DIR_WORK.'file.log.1.gz')));
34+
$this->assertEquals($content, implode('', (array)gzfile(self::DIR_WORK.'file.log.1.gz')));
3535
}
3636
}

tests/ErrorHandlerTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class ErrorHandlerTest extends TestCase
99
{
10-
public function testCallThenIfRotateWasSucessfull()
10+
public function testCallThenIfRotateWasSuccessful(): void
1111
{
1212
file_put_contents(self::DIR_WORK.'file.log', microtime(true));
1313

@@ -22,7 +22,7 @@ public function testCallThenIfRotateWasSucessfull()
2222
$this->assertTrue($thenCalled);
2323
}
2424

25-
public function testNotCallThenIfRotateNotWasSucessfull()
25+
public function testNotCallThenIfRotateNotWasSuccessful(): void
2626
{
2727
$rotation = new Rotation();
2828

@@ -35,7 +35,7 @@ public function testNotCallThenIfRotateNotWasSucessfull()
3535
$this->assertFalse($thenCalled);
3636
}
3737

38-
public function testThrowsException()
38+
public function testThrowsException(): void
3939
{
4040
$this->expectException(RotationFailed::class);
4141

@@ -49,7 +49,7 @@ public function testThrowsException()
4949
$this->assertFalse($result);
5050
}
5151

52-
public function testCatchException()
52+
public function testCatchException(): void
5353
{
5454
$rotation = new Rotation();
5555

@@ -68,7 +68,7 @@ public function testCatchException()
6868
$this->assertFalse($result);
6969
}
7070

71-
public function testCallFinallyIfRotateWasSucessfull()
71+
public function testCallFinallyIfRotateWasSuccessful(): void
7272
{
7373
file_put_contents(self::DIR_WORK.'file.log', microtime(true));
7474

@@ -83,7 +83,7 @@ public function testCallFinallyIfRotateWasSucessfull()
8383
$this->assertTrue($finallyCalled);
8484
}
8585

86-
public function testCallFinallyIfFileDontExists()
86+
public function testCallFinallyIfFileDontExists(): void
8787
{
8888
$rotation = new Rotation();
8989

@@ -96,7 +96,7 @@ public function testCallFinallyIfFileDontExists()
9696
$this->assertTrue($finallyCalled);
9797
}
9898

99-
public function testCallFinallyIfThrowException()
99+
public function testCallFinallyIfThrowException(): void
100100
{
101101
$this->expectException(RotationFailed::class);
102102

tests/OptionTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class OptionTest extends TestCase
88
{
9-
public function testPassOptions()
9+
public function testPassOptions(): void
1010
{
1111
$rotation = new Rotation([
1212
'files' => 1,
@@ -21,7 +21,7 @@ public function testPassOptions()
2121
$this->assertNotNull($rotation);
2222
}
2323

24-
public function testCatchExceptioIfMethodIsNotPermited()
24+
public function testCatchExceptionIfMethodIsNotPermitted(): void
2525
{
2626
$this->expectException(\LogicException::class);
2727

tests/Processors/RotativeProcessorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class RotativeProcessorTest extends TestCase
99
{
10-
public function testRotationProcessor()
10+
public function testRotationProcessor(): void
1111
{
1212
$maxFiles = 5;
1313

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

32-
public function testRotationProcessorWithGzProcessor()
32+
public function testRotationProcessorWithGzProcessor(): void
3333
{
3434
$maxFiles = 5;
3535

0 commit comments

Comments
 (0)