Skip to content

Commit af6484f

Browse files
committed
Up phpstan baseline
1 parent 2158d0a commit af6484f

14 files changed

+110
-218
lines changed

src/Exception/NotCreatableException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class NotCreatableException extends NotAccessibleException
1818

1919
protected const CODE_LAST = self::CODE_INVALID_TYPE;
2020

21-
public static function fromInvalidType($source): self
21+
public static function fromInvalidType(mixed $source): self
2222
{
2323
$message = \vsprintf('Cannot create %s instance from %s', [
2424
ReadableInterface::class,

src/Exception/NotReadableException.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static function fromOpeningFile(string $filename, ?\Throwable $prev = nul
8181
/**
8282
* @return static
8383
*/
84-
public static function fromInvalidResource($stream): self
84+
public static function fromInvalidResource(mixed $stream): self
8585
{
8686
$message = 'The "%s" is not valid resource stream';
8787
$message = \sprintf($message, \str_replace("\0", '\0', \get_debug_type($stream)));
@@ -94,7 +94,7 @@ public static function fromInvalidResource($stream): self
9494
*
9595
* @return static
9696
*/
97-
public static function fromInvalidStream($stream): self
97+
public static function fromInvalidStream(mixed $stream): self
9898
{
9999
assert(\is_resource($stream));
100100

src/File.php

+19-37
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,37 @@
1010

1111
class File extends Readable implements FileInterface
1212
{
13-
/**
14-
* @var non-empty-string
15-
*
16-
* @psalm-readonly-allow-private-mutation
17-
*/
18-
private string $filename;
19-
20-
/**
21-
* @var non-empty-string
22-
*
23-
* @psalm-readonly-allow-private-mutation
24-
*/
25-
private string $algo = SourceFactory::DEFAULT_HASH_ALGO;
26-
27-
/**
28-
* @var int<1, max>
29-
*
30-
* @psalm-readonly-allow-private-mutation
31-
*/
32-
private int $chunkSize = SourceFactory::DEFAULT_CHUNK_SIZE;
33-
3413
/**
3514
* @psalm-taint-sink file $filename
36-
* @param non-empty-string $filename
37-
* @param non-empty-string $algo hashing algorithm for the source
38-
* @param int<1, max> $chunkSize the chunk size used while non-blocking
39-
* reading the file inside the {@see \Fiber}
4015
*/
4116
public function __construct(
42-
string $filename,
43-
string $algo = SourceFactory::DEFAULT_HASH_ALGO,
44-
int $chunkSize = SourceFactory::DEFAULT_CHUNK_SIZE
17+
/**
18+
* @var non-empty-string
19+
*/
20+
private readonly string $filename,
21+
/**
22+
* Hashing algorithm for the source.
23+
*
24+
* @var non-empty-string
25+
*/
26+
private readonly string $algo = SourceFactory::DEFAULT_HASH_ALGO,
27+
/**
28+
* The chunk size used while non-blocking reading the file inside
29+
* the {@see \Fiber}.
30+
*
31+
* @var int<1, max>
32+
*/
33+
private readonly int $chunkSize = SourceFactory::DEFAULT_CHUNK_SIZE,
4534
) {
4635
assert($filename !== '', 'Filename must not be empty');
4736
assert($algo !== '', 'Hashing algorithm name must not be empty');
4837
assert($chunkSize >= 1, 'Chunk size must be greater than 0');
49-
50-
$this->chunkSize = $chunkSize;
51-
$this->algo = $algo;
52-
$this->filename = $filename;
5338
}
5439

5540
public function getContents(): string
5641
{
5742
try {
58-
if (\PHP_MAJOR_VERSION >= 8
59-
&& \PHP_MINOR_VERSION >= 1
60-
&& \Fiber::getCurrent() !== null
61-
) {
43+
if (\Fiber::getCurrent() !== null) {
6244
return $this->asyncGetContents();
6345
}
6446

@@ -111,7 +93,7 @@ private function syncGetContents(): string
11193
/**
11294
* @throws NotReadableException
11395
*/
114-
public function getStream()
96+
public function getStream(): mixed
11597
{
11698
$stream = \fopen($this->filename, 'rb');
11799

src/Provider/PsrStreamSourceProvider.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,11 @@
1010

1111
final class PsrStreamSourceProvider implements SourceProviderInterface
1212
{
13-
/**
14-
* @readonly
15-
*/
16-
private SourceFactory $parent;
13+
public function __construct(
14+
private readonly SourceFactory $parent,
15+
) {}
1716

18-
public function __construct(SourceFactory $parent)
19-
{
20-
$this->parent = $parent;
21-
}
22-
23-
public function create($source): ?ReadableInterface
17+
public function create(mixed $source): ?ReadableInterface
2418
{
2519
if (!$source instanceof StreamInterface) {
2620
return null;

src/Provider/SourceProviderInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ interface SourceProviderInterface
1818
* @throws SourceExceptionInterface in case of an error in creating the
1919
* source object
2020
*/
21-
public function create($source): ?ReadableInterface;
21+
public function create(mixed $source): ?ReadableInterface;
2222
}

src/Provider/SplFileInfoSourceProvider.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,11 @@
99

1010
final class SplFileInfoSourceProvider implements SourceProviderInterface
1111
{
12-
/**
13-
* @readonly
14-
*/
15-
private SourceFactory $parent;
12+
public function __construct(
13+
private readonly SourceFactory $parent,
14+
) {}
1615

17-
public function __construct(SourceFactory $parent)
18-
{
19-
$this->parent = $parent;
20-
}
21-
22-
public function create($source): ?ReadableInterface
16+
public function create(mixed $source): ?ReadableInterface
2317
{
2418
if (!$source instanceof \SplFileInfo) {
2519
return null;

src/Provider/StreamSourceProvider.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,11 @@
99

1010
final class StreamSourceProvider implements SourceProviderInterface
1111
{
12-
/**
13-
* @readonly
14-
*/
15-
private SourceFactory $parent;
12+
public function __construct(
13+
private readonly SourceFactory $parent,
14+
) {}
1615

17-
public function __construct(SourceFactory $parent)
18-
{
19-
$this->parent = $parent;
20-
}
21-
22-
public function create($source): ?ReadableInterface
16+
public function create(mixed $source): ?ReadableInterface
2317
{
2418
if (!\is_resource($source)) {
2519
return null;

src/Provider/TextSourceProvider.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,11 @@
99

1010
final class TextSourceProvider implements SourceProviderInterface
1111
{
12-
/**
13-
* @readonly
14-
*/
15-
private SourceFactory $parent;
12+
public function __construct(
13+
private readonly SourceFactory $parent,
14+
) {}
1615

17-
public function __construct(SourceFactory $parent)
18-
{
19-
$this->parent = $parent;
20-
}
21-
22-
public function create($source): ?ReadableInterface
16+
public function create(mixed $source): ?ReadableInterface
2317
{
2418
if (!\is_string($source)) {
2519
return null;

src/Source.php

+16-31
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,29 @@ class Source extends Readable implements PreferContentReadingInterface
1919
/**
2020
* @var resource|null
2121
*/
22-
private $stream;
23-
24-
/**
25-
* @psalm-readonly-allow-private-mutation
26-
*/
27-
private string $content;
28-
29-
/**
30-
* @var non-empty-string
31-
*
32-
* @psalm-readonly-allow-private-mutation
33-
*/
34-
private string $algo = SourceFactory::DEFAULT_HASH_ALGO;
35-
36-
/**
37-
* @var non-empty-string
38-
*
39-
* @psalm-readonly-allow-private-mutation
40-
*/
41-
private string $temp = SourceFactory::DEFAULT_TEMP_STREAM;
22+
private mixed $stream = null;
4223

4324
/**
4425
* @psalm-taint-sink file $temp
45-
* @param non-empty-string $algo hashing algorithm for the source
46-
* @param non-empty-string $temp the name of the temporary stream, which is
47-
* used as a resource during the reading of the source
4826
*/
4927
public function __construct(
50-
string $content,
51-
string $algo = SourceFactory::DEFAULT_HASH_ALGO,
52-
string $temp = SourceFactory::DEFAULT_TEMP_STREAM
28+
private readonly string $content,
29+
/**
30+
* Hashing algorithm for the source.
31+
*
32+
* @var non-empty-string
33+
*/
34+
private readonly string $algo = SourceFactory::DEFAULT_HASH_ALGO,
35+
/**
36+
* The name of the temporary stream, which is used as a resource during
37+
* the reading of the source.
38+
*
39+
* @var non-empty-string
40+
*/
41+
private readonly string $temp = SourceFactory::DEFAULT_TEMP_STREAM
5342
) {
5443
assert($algo !== '', 'Hashing algorithm name must not be empty');
5544
assert($temp !== '', 'Temporary stream name must not be empty');
56-
57-
$this->temp = $temp;
58-
$this->algo = $algo;
59-
$this->content = $content;
6045
}
6146

6247
public function getContents(): string
@@ -67,7 +52,7 @@ public function getContents(): string
6752
/**
6853
* @throws NotAccessibleException
6954
*/
70-
public function getStream()
55+
public function getStream(): mixed
7156
{
7257
if (!\is_resource($this->stream)) {
7358
$this->stream = \fopen($this->temp, 'rb+');

src/SourceFactory.php

+22-35
Original file line numberDiff line numberDiff line change
@@ -39,54 +39,41 @@ final class SourceFactory implements SourceFactoryInterface
3939
*/
4040
public const DEFAULT_TEMP_STREAM = 'php://memory';
4141

42-
/**
43-
* @var non-empty-string
44-
*
45-
* @psalm-readonly-allow-private-mutation
46-
*/
47-
public string $algo = self::DEFAULT_HASH_ALGO;
48-
49-
/**
50-
* @var non-empty-string
51-
*
52-
* @psalm-readonly-allow-private-mutation
53-
*/
54-
public string $temp = self::DEFAULT_TEMP_STREAM;
55-
56-
/**
57-
* @var int<1, max>
58-
*
59-
* @psalm-readonly-allow-private-mutation
60-
*/
61-
public int $chunkSize = self::DEFAULT_CHUNK_SIZE;
62-
6342
/**
6443
* @var list<SourceProviderInterface>
6544
*/
6645
private array $providers = [];
6746

6847
/**
69-
* @param non-empty-string $algo hashing algorithm for the sources
70-
* @param non-empty-string $temp the name of the temporary stream, which is
71-
* used as a resource during the reading of the source
72-
* @param int<1, max> $chunkSize the chunk size used while non-blocking
73-
* reading the file inside the {@see \Fiber} context
7448
* @param list<SourceProviderInterface> $providers list of source providers
7549
*/
7650
public function __construct(
77-
string $algo = self::DEFAULT_HASH_ALGO,
78-
string $temp = self::DEFAULT_TEMP_STREAM,
79-
int $chunkSize = self::DEFAULT_CHUNK_SIZE,
51+
/**
52+
* Hashing algorithm for the sources.
53+
*
54+
* @var non-empty-string
55+
*/
56+
public readonly string $algo = self::DEFAULT_HASH_ALGO,
57+
/**
58+
* The name of the temporary stream, which is used as a resource
59+
* during the reading of the source.
60+
*
61+
* @var non-empty-string
62+
*/
63+
public readonly string $temp = self::DEFAULT_TEMP_STREAM,
64+
/**
65+
* The chunk size used while non-blocking reading the file
66+
* inside the {@see \Fiber} context.
67+
*
68+
* @var int<1, max>
69+
*/
70+
public readonly int $chunkSize = self::DEFAULT_CHUNK_SIZE,
8071
iterable $providers = []
8172
) {
8273
assert($algo !== '', 'Hashing algorithm name must not be empty');
8374
assert($temp !== '', 'Temporary stream name must not be empty');
8475
assert($chunkSize >= 1, 'Chunk size must be greater than 0');
8576

86-
$this->chunkSize = $chunkSize;
87-
$this->temp = $temp;
88-
$this->algo = $algo;
89-
9077
$this->providers = [
9178
...$providers,
9279
new PsrStreamSourceProvider($this),
@@ -122,7 +109,7 @@ public function withPrependedProvider(SourceProviderInterface $provider): self
122109
return $self;
123110
}
124111

125-
public function create($source): ReadableInterface
112+
public function create(mixed $source): ReadableInterface
126113
{
127114
foreach ($this->providers as $provider) {
128115
$readable = $provider->create($source);
@@ -166,7 +153,7 @@ public function createFromFile(string $filename): FileInterface
166153
/**
167154
* @throws NotReadableException
168155
*/
169-
public function createFromStream($stream, ?string $name = null): ReadableInterface
156+
public function createFromStream(mixed $stream, ?string $name = null): ReadableInterface
170157
{
171158
assert($name !== '', 'Name must not be empty');
172159

src/SourceFactoryTrait.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ public static function getSourceFactory(): SourceFactoryInterface
3232
* : ReadableInterface)
3333
* )
3434
* @throws SourceExceptionInterface
35-
*
36-
* @psalm-suppress NoValue : Allow any value
3735
*/
38-
public static function new($source): ReadableInterface
36+
public static function new(mixed $source): ReadableInterface
3937
{
4038
if ($source instanceof StreamInterface) {
4139
return static::fromPsrStream($source);
@@ -117,7 +115,7 @@ public static function fromPsrStream(StreamInterface $stream, ?string $pathname
117115
trigger_deprecation('phplrt/source', '3.4', <<<'MSG'
118116
Using "%s::fromPsrStream($stream)" with %s argument is deprecated,
119117
use "%1$s::fromResource($stream->detach())" instead.
120-
MSG, static::class, \get_class($stream));
118+
MSG, static::class, $stream::class);
121119

122120
return static::fromResource($stream->detach(), $pathname);
123121
}
@@ -129,7 +127,7 @@ public static function fromPsrStream(StreamInterface $stream, ?string $pathname
129127
* @return ($pathname is null ? ReadableInterface : FileInterface)
130128
* @throws SourceExceptionInterface
131129
*/
132-
public static function fromResource($resource, ?string $pathname = null): ReadableInterface
130+
public static function fromResource(mixed $resource, ?string $pathname = null): ReadableInterface
133131
{
134132
$factory = static::getSourceFactory();
135133

0 commit comments

Comments
 (0)