Skip to content

Commit 2158d0a

Browse files
committed
Fix rector deprecations
1 parent 8b2b609 commit 2158d0a

14 files changed

+117
-34
lines changed

.github/workflows/main.yml

+1-1
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

+5-4
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,12 +39,12 @@
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": {

src/Exception/NotCreatableException.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ class NotCreatableException extends NotAccessibleException
1818

1919
protected const CODE_LAST = self::CODE_INVALID_TYPE;
2020

21-
public static function fromInvalidType(mixed $source): self
21+
public static function fromInvalidType($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/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(mixed $stream): self
84+
public static function fromInvalidResource($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(mixed $stream): self
9494
*
9595
* @return static
9696
*/
97-
public static function fromInvalidStream(mixed $stream): self
97+
public static function fromInvalidStream($stream): self
9898
{
9999
assert(\is_resource($stream));
100100

src/MemoizableInterface.php

+13
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/Provider/PsrStreamSourceProvider.php

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

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

17-
public function create(mixed $source): ?ReadableInterface
18+
public function __construct(SourceFactory $parent)
19+
{
20+
$this->parent = $parent;
21+
}
22+
23+
public function create($source): ?ReadableInterface
1824
{
1925
if (!$source instanceof StreamInterface) {
2026
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(mixed $source): ?ReadableInterface;
21+
public function create($source): ?ReadableInterface;
2222
}

src/Provider/SplFileInfoSourceProvider.php

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

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

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

src/Provider/StreamSourceProvider.php

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

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

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

src/Provider/TextSourceProvider.php

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

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

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

src/Source.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class Source extends Readable implements PreferContentReadingInterface
2121
*/
2222
private $stream;
2323

24+
/**
25+
* @psalm-readonly-allow-private-mutation
26+
*/
27+
private string $content;
28+
2429
/**
2530
* @var non-empty-string
2631
*
@@ -42,10 +47,7 @@ class Source extends Readable implements PreferContentReadingInterface
4247
* used as a resource during the reading of the source
4348
*/
4449
public function __construct(
45-
/**
46-
* @psalm-readonly-allow-private-mutation
47-
*/
48-
private string $content,
50+
string $content,
4951
string $algo = SourceFactory::DEFAULT_HASH_ALGO,
5052
string $temp = SourceFactory::DEFAULT_TEMP_STREAM
5153
) {
@@ -54,6 +56,7 @@ public function __construct(
5456

5557
$this->temp = $temp;
5658
$this->algo = $algo;
59+
$this->content = $content;
5760
}
5861

5962
public function getContents(): string

src/SourceFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function withPrependedProvider(SourceProviderInterface $provider): self
122122
return $self;
123123
}
124124

125-
public function create(mixed $source): ReadableInterface
125+
public function create($source): ReadableInterface
126126
{
127127
foreach ($this->providers as $provider) {
128128
$readable = $provider->create($source);
@@ -166,7 +166,7 @@ public function createFromFile(string $filename): FileInterface
166166
/**
167167
* @throws NotReadableException
168168
*/
169-
public function createFromStream(mixed $stream, ?string $name = null): ReadableInterface
169+
public function createFromStream($stream, ?string $name = null): ReadableInterface
170170
{
171171
assert($name !== '', 'Name must not be empty');
172172

src/SourceFactoryTrait.php

+27-2
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
{
@@ -34,8 +35,12 @@ public static function getSourceFactory(): SourceFactoryInterface
3435
*
3536
* @psalm-suppress NoValue : Allow any value
3637
*/
37-
public static function new(mixed $source): ReadableInterface
38+
public static function new($source): ReadableInterface
3839
{
40+
if ($source instanceof StreamInterface) {
41+
return static::fromPsrStream($source);
42+
}
43+
3944
$factory = self::getSourceFactory();
4045

4146
return $factory->create($source);
@@ -46,6 +51,7 @@ public static function new(mixed $source): ReadableInterface
4651
* @param non-empty-string|null $pathname
4752
*
4853
* @return ($pathname is null ? ReadableInterface : FileInterface)
54+
* @throws SourceExceptionInterface
4955
*/
5056
public static function empty(?string $pathname = null): ReadableInterface
5157
{
@@ -57,6 +63,7 @@ public static function empty(?string $pathname = null): ReadableInterface
5763
* @param non-empty-string|null $pathname
5864
*
5965
* @return ($pathname is null ? ReadableInterface : FileInterface)
66+
* @throws SourceExceptionInterface
6067
*/
6168
public static function fromSources(string $sources, ?string $pathname = null): ReadableInterface
6269
{
@@ -97,14 +104,32 @@ public static function fromPathname(string $pathname): FileInterface
97104
return new File($pathname);
98105
}
99106

107+
/**
108+
* @param non-empty-string|null $pathname
109+
*
110+
* @return ($pathname is null ? ReadableInterface : FileInterface)
111+
* @throws SourceExceptionInterface
112+
*
113+
* @deprecated since phplrt 3.4 and will be removed in 4.0, use {@see fromResource()} instead.
114+
*/
115+
public static function fromPsrStream(StreamInterface $stream, ?string $pathname = null): ReadableInterface
116+
{
117+
trigger_deprecation('phplrt/source', '3.4', <<<'MSG'
118+
Using "%s::fromPsrStream($stream)" with %s argument is deprecated,
119+
use "%1$s::fromResource($stream->detach())" instead.
120+
MSG, static::class, \get_class($stream));
121+
122+
return static::fromResource($stream->detach(), $pathname);
123+
}
124+
100125
/**
101126
* @param resource $resource
102127
* @param non-empty-string|null $pathname
103128
*
104129
* @return ($pathname is null ? ReadableInterface : FileInterface)
105130
* @throws SourceExceptionInterface
106131
*/
107-
public static function fromResource(mixed $resource, ?string $pathname = null): ReadableInterface
132+
public static function fromResource($resource, ?string $pathname = null): ReadableInterface
108133
{
109134
$factory = static::getSourceFactory();
110135

tests/Unit/TestCase.php

+17
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
namespace Phplrt\Source\Tests\Unit;
66

7+
use HttpSoft\Message\StreamFactory;
78
use Phplrt\Source\File;
89
use Phplrt\Source\Tests\TestCase as UnitTestCase;
910

1011
abstract class TestCase extends UnitTestCase
1112
{
1213
public static function provider(): array
1314
{
15+
$factory = new StreamFactory();
16+
1417
return [
1518
'File::fromSources + filename' => [
1619
function () {
@@ -32,6 +35,20 @@ function () {
3235
return File::fromSplFileInfo(new \SplFileInfo(static::getPathname()));
3336
},
3437
],
38+
'File::fromPsrStream + filename' => [
39+
function () use ($factory) {
40+
$stream = $factory->createStreamFromFile(static::getPathname());
41+
42+
return File::fromPsrStream($stream, static::getPathname());
43+
},
44+
],
45+
'File::fromPsrStream' => [
46+
function () use ($factory) {
47+
$stream = $factory->createStreamFromFile(static::getPathname());
48+
49+
return File::fromPsrStream($stream);
50+
},
51+
],
3552
'File::fromResource + filename' => [
3653
function () {
3754
$resource = \fopen(static::getPathname(), 'rb');

0 commit comments

Comments
 (0)