Skip to content

Commit 0d73ae4

Browse files
committed
Use list<T> in ArrayBuffer instead of custom keys (possibly keys duplication fix).
1 parent bf461e1 commit 0d73ae4

12 files changed

Lines changed: 149 additions & 171 deletions

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
fail-fast: false
1818
matrix:
19-
php: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
19+
php: [ '8.1', '8.2', '8.3', '8.4' ]
2020
os: [ ubuntu-latest, macos-latest, windows-latest ]
2121
stability: [ prefer-lowest, prefer-stable ]
2222
steps:

composer.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^8.1",
20-
"phplrt/source-contracts": "^4.0",
20+
"phplrt/source-contracts": "^3.7",
2121
"symfony/deprecation-contracts": "^2.5|^3.0"
2222
},
2323
"autoload": {
@@ -28,6 +28,7 @@
2828
"require-dev": {
2929
"phpunit/phpunit": "^10.5|^11.0",
3030
"psr/http-message": "^1.0|^2.0",
31+
"httpsoft/http-message": "^1.1",
3132
"phpstan/extension-installer": "^1.4",
3233
"phpstan/phpstan": "^1.11",
3334
"phpstan/phpstan-strict-rules": "^1.6"
@@ -38,16 +39,19 @@
3839
}
3940
},
4041
"provide": {
41-
"phplrt/source-contracts-implementation": "^4.0"
42+
"phplrt/source-contracts-implementation": "^3.7"
4243
},
4344
"extra": {
4445
"branch-alias": {
45-
"dev-master": "4.x-dev",
46-
"dev-main": "4.x-dev"
46+
"dev-master": "3.x-dev",
47+
"dev-main": "3.x-dev"
4748
}
4849
},
4950
"config": {
50-
"sort-packages": true
51+
"sort-packages": true,
52+
"allow-plugins": {
53+
"phpstan/extension-installer": true
54+
}
5155
},
5256
"minimum-stability": "dev",
5357
"prefer-stable": true

src/Exception/NotCreatableException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function fromInvalidType(mixed $source): self
2222
{
2323
$message = \vsprintf('Cannot create %s instance from %s', [
2424
ReadableInterface::class,
25-
\get_debug_type($source),
25+
\get_debug_type($source)
2626
]);
2727

2828
return new static($message, self::CODE_INVALID_TYPE);

src/File.php

Lines changed: 19 additions & 37 deletions
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/MemoizableInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phplrt\Source;
6+
7+
/**
8+
* @deprecated since phplrt 3.4 and will be removed in 4.0.
9+
*/
10+
interface MemoizableInterface
11+
{
12+
public function refresh(): void;
13+
}

src/Source.php

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

3824
/**
3925
* @psalm-taint-sink file $temp
40-
* @param non-empty-string $algo hashing algorithm for the source
41-
* @param non-empty-string $temp the name of the temporary stream, which is
42-
* used as a resource during the reading of the source
4326
*/
4427
public function __construct(
28+
private readonly string $content,
4529
/**
46-
* @psalm-readonly-allow-private-mutation
30+
* Hashing algorithm for the source.
31+
*
32+
* @var non-empty-string
4733
*/
48-
private string $content,
49-
string $algo = SourceFactory::DEFAULT_HASH_ALGO,
50-
string $temp = SourceFactory::DEFAULT_TEMP_STREAM
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
5142
) {
5243
assert($algo !== '', 'Hashing algorithm name must not be empty');
5344
assert($temp !== '', 'Temporary stream name must not be empty');
54-
55-
$this->temp = $temp;
56-
$this->algo = $algo;
5745
}
5846

5947
public function getContents(): string
@@ -64,7 +52,7 @@ public function getContents(): string
6452
/**
6553
* @throws NotAccessibleException
6654
*/
67-
public function getStream()
55+
public function getStream(): mixed
6856
{
6957
if (!\is_resource($this->stream)) {
7058
$this->stream = \fopen($this->temp, 'rb+');

src/SourceFactory.php

Lines changed: 20 additions & 33 deletions
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),

src/SourceFactoryTrait.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Phplrt\Contracts\Source\ReadableInterface;
99
use Phplrt\Contracts\Source\SourceExceptionInterface;
1010
use Phplrt\Contracts\Source\SourceFactoryInterface;
11+
use Psr\Http\Message\StreamInterface;
1112

1213
trait SourceFactoryTrait
1314
{
@@ -31,11 +32,13 @@ public static function getSourceFactory(): SourceFactoryInterface
3132
* : ReadableInterface)
3233
* )
3334
* @throws SourceExceptionInterface
34-
*
35-
* @psalm-suppress NoValue : Allow any value
3635
*/
3736
public static function new(mixed $source): ReadableInterface
3837
{
38+
if ($source instanceof StreamInterface) {
39+
return static::fromPsrStream($source);
40+
}
41+
3942
$factory = self::getSourceFactory();
4043

4144
return $factory->create($source);
@@ -46,6 +49,7 @@ public static function new(mixed $source): ReadableInterface
4649
* @param non-empty-string|null $pathname
4750
*
4851
* @return ($pathname is null ? ReadableInterface : FileInterface)
52+
* @throws SourceExceptionInterface
4953
*/
5054
public static function empty(?string $pathname = null): ReadableInterface
5155
{
@@ -57,6 +61,7 @@ public static function empty(?string $pathname = null): ReadableInterface
5761
* @param non-empty-string|null $pathname
5862
*
5963
* @return ($pathname is null ? ReadableInterface : FileInterface)
64+
* @throws SourceExceptionInterface
6065
*/
6166
public static function fromSources(string $sources, ?string $pathname = null): ReadableInterface
6267
{
@@ -97,6 +102,24 @@ public static function fromPathname(string $pathname): FileInterface
97102
return new File($pathname);
98103
}
99104

105+
/**
106+
* @param non-empty-string|null $pathname
107+
*
108+
* @return ($pathname is null ? ReadableInterface : FileInterface)
109+
* @throws SourceExceptionInterface
110+
*
111+
* @deprecated since phplrt 3.4 and will be removed in 4.0, use {@see fromResource()} instead.
112+
*/
113+
public static function fromPsrStream(StreamInterface $stream, ?string $pathname = null): ReadableInterface
114+
{
115+
trigger_deprecation('phplrt/source', '3.4', <<<'MSG'
116+
Using "%s::fromPsrStream($stream)" with %s argument is deprecated,
117+
use "%1$s::fromResource($stream->detach())" instead.
118+
MSG, static::class, $stream::class);
119+
120+
return static::fromResource($stream->detach(), $pathname);
121+
}
122+
100123
/**
101124
* @param resource $resource
102125
* @param non-empty-string|null $pathname

0 commit comments

Comments
 (0)