Skip to content

Commit 146a0d4

Browse files
authored
feat!: remove $config parameter from ReaderInterface (#23)
BREAKING CHANGE: Change to ReaderInterface interface
1 parent ccee19b commit 146a0d4

18 files changed

Lines changed: 26 additions & 70 deletions

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1818
### Changed
1919

2020
- The `Extractor\MessageExtractor` constructor now requires `Util\FormatHelper` as a fifth parameter
21-
- Use `ConfigInterface` instead of `Config` as the type-hint on `Format\ReaderInterface`
21+
- Remove `$config` argument from `Format\ReaderInterface`
2222
- Remove `$localeResolved` argument from `Format\ReaderInterface`
2323
- Change type on `$options` argument in `Format\WriterInterface` from `MessageExtractorOptions` to a dedicated `WriterOptions` type
2424

src/Format/Reader/FormatPHPReader.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
namespace FormatPHP\Format\Reader;
2424

25-
use FormatPHP\ConfigInterface;
2625
use FormatPHP\Exception\InvalidMessageShapeException;
2726
use FormatPHP\Format\ReaderInterface;
2827
use FormatPHP\Format\Writer\FormatPHPWriter;
@@ -45,7 +44,7 @@ class FormatPHPReader implements ReaderInterface
4544
/**
4645
* @inheritdoc
4746
*/
48-
public function __invoke(ConfigInterface $config, array $data): MessageCollection
47+
public function __invoke(array $data): MessageCollection
4948
{
5049
$messages = new MessageCollection();
5150

src/Format/Reader/SimpleReader.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
namespace FormatPHP\Format\Reader;
2424

25-
use FormatPHP\ConfigInterface;
2625
use FormatPHP\Exception\InvalidMessageShapeException;
2726
use FormatPHP\Format\ReaderInterface;
2827
use FormatPHP\Format\Writer\SimpleWriter;
@@ -45,7 +44,7 @@ class SimpleReader implements ReaderInterface
4544
/**
4645
* @inheritdoc
4746
*/
48-
public function __invoke(ConfigInterface $config, array $data): MessageCollection
47+
public function __invoke(array $data): MessageCollection
4948
{
5049
$messages = new MessageCollection();
5150

src/Format/Reader/SmartlingReader.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
namespace FormatPHP\Format\Reader;
2424

25-
use FormatPHP\ConfigInterface;
2625
use FormatPHP\Exception\InvalidMessageShapeException;
2726
use FormatPHP\Format\ReaderInterface;
2827
use FormatPHP\Format\Writer\SmartlingWriter;
@@ -45,7 +44,7 @@ class SmartlingReader implements ReaderInterface
4544
/**
4645
* @inheritdoc
4746
*/
48-
public function __invoke(ConfigInterface $config, array $data): MessageCollection
47+
public function __invoke(array $data): MessageCollection
4948
{
5049
$messages = new MessageCollection();
5150

src/Format/ReaderInterface.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222

2323
namespace FormatPHP\Format;
2424

25-
use FormatPHP\ConfigInterface;
2625
use FormatPHP\Exception\InvalidMessageShapeException;
2726
use FormatPHP\MessageCollection;
2827

2928
/**
3029
* Returns a collection of messages parsed from JSON-decoded message data
3130
*
32-
* @psalm-type ReaderCallableType = callable(ConfigInterface,mixed[]):MessageCollection
31+
* @psalm-type ReaderCallableType = callable(mixed[]):MessageCollection
3332
* @psalm-type ReaderType = ReaderInterface | ReaderCallableType
3433
*/
3534
interface ReaderInterface
@@ -40,5 +39,5 @@ interface ReaderInterface
4039
*
4140
* @throws InvalidMessageShapeException
4241
*/
43-
public function __invoke(ConfigInterface $config, array $data): MessageCollection;
42+
public function __invoke(array $data): MessageCollection;
4443
}

src/MessageLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function __construct(
7979
*/
8080
public function loadMessages(): MessageCollection
8181
{
82-
return ($this->formatReader)($this->config, $this->getLocaleMessages());
82+
return ($this->formatReader)($this->getLocaleMessages());
8383
}
8484

8585
/**

src/Util/FormatHelper.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
namespace FormatPHP\Util;
2424

2525
use Closure;
26-
use FormatPHP\ConfigInterface;
2726
use FormatPHP\DescriptorCollection;
2827
use FormatPHP\Exception\ImproperContextException;
2928
use FormatPHP\Exception\InvalidArgumentException;
@@ -160,20 +159,17 @@ private function validateReaderClosure(?Closure $formatter): Closure
160159

161160
$reflected = new ReflectionFunction($formatter);
162161

163-
assert($reflected->getNumberOfParameters() === 2);
162+
assert($reflected->getNumberOfParameters() === 1);
164163

165164
$param1 = $reflected->getParameters()[0];
166-
$param2 = $reflected->getParameters()[1];
167165

168-
assert($param1->hasType() && $param1->getType()->getName() === ConfigInterface::class);
169-
assert($param2->hasType() && $param2->getType()->getName() === 'array');
166+
assert($param1->hasType() && $param1->getType()->getName() === 'array');
170167
assert($reflected->hasReturnType() && $reflected->getReturnType()->getName() === MessageCollection::class);
171168
} catch (Throwable $exception) {
172169
throw new InvalidArgumentException(sprintf(
173170
'The format provided is not a known format, an instance of '
174-
. '%s, or a callable of the shape `callable(%s,array<mixed>):%s`.',
171+
. '%s, or a callable of the shape `callable(array<mixed>):%s`.',
175172
ReaderInterface::class,
176-
ConfigInterface::class,
177173
MessageCollection::class,
178174
));
179175
}

tests/Format/Reader/FormatPHPReaderTest.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
namespace FormatPHP\Test\Format\Reader;
66

7-
use FormatPHP\Config;
87
use FormatPHP\Exception\InvalidMessageShapeException;
98
use FormatPHP\Format\Reader\FormatPHPReader;
10-
use FormatPHP\Intl\Locale;
119
use FormatPHP\MessageCollection;
1210
use FormatPHP\MessageInterface;
1311
use FormatPHP\Test\TestCase;
@@ -18,7 +16,6 @@ class FormatPHPReaderTest extends TestCase
1816
{
1917
public function testThrowsExceptionWhenMessageIdIsNotAString(): void
2018
{
21-
$config = new Config(new Locale('en'));
2219
$formatReader = new FormatPHPReader();
2320
$data = ['foo'];
2421

@@ -28,12 +25,11 @@ public function testThrowsExceptionWhenMessageIdIsNotAString(): void
2825
FormatPHPReader::class,
2926
));
3027

31-
$formatReader($config, $data);
28+
$formatReader($data);
3229
}
3330

3431
public function testThrowsExceptionWhenMessageIsNotAString(): void
3532
{
36-
$config = new Config(new Locale('en'));
3733
$formatReader = new FormatPHPReader();
3834
$data = ['foo' => ['bar']];
3935

@@ -43,16 +39,15 @@ public function testThrowsExceptionWhenMessageIsNotAString(): void
4339
FormatPHPReader::class,
4440
));
4541

46-
$formatReader($config, $data);
42+
$formatReader($data);
4743
}
4844

4945
public function testInvoke(): void
5046
{
51-
$config = new Config(new Locale('en-US'));
5247
$formatReader = new FormatPHPReader();
5348
$data = ['foo' => ['defaultMessage' => 'I am foo'], 'bar' => ['defaultMessage' => 'I am bar']];
5449

55-
$collection = $formatReader($config, $data);
50+
$collection = $formatReader($data);
5651

5752
$this->assertInstanceOf(MessageCollection::class, $collection);
5853
$this->assertCount(2, $collection);

tests/Format/Reader/SimpleReaderTest.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
namespace FormatPHP\Test\Format\Reader;
66

7-
use FormatPHP\Config;
87
use FormatPHP\Exception\InvalidMessageShapeException;
98
use FormatPHP\Format\Reader\SimpleReader;
10-
use FormatPHP\Intl\Locale;
119
use FormatPHP\MessageCollection;
1210
use FormatPHP\MessageInterface;
1311
use FormatPHP\Test\TestCase;
@@ -18,7 +16,6 @@ class SimpleReaderTest extends TestCase
1816
{
1917
public function testThrowsExceptionWhenMessageIdIsNotAString(): void
2018
{
21-
$config = new Config(new Locale('en'));
2219
$formatReader = new SimpleReader();
2320
$data = ['foo'];
2421

@@ -28,12 +25,11 @@ public function testThrowsExceptionWhenMessageIdIsNotAString(): void
2825
SimpleReader::class,
2926
));
3027

31-
$formatReader($config, $data);
28+
$formatReader($data);
3229
}
3330

3431
public function testThrowsExceptionWhenMessageIsNotAString(): void
3532
{
36-
$config = new Config(new Locale('en'));
3733
$formatReader = new SimpleReader();
3834
$data = ['foo' => ['bar']];
3935

@@ -43,16 +39,15 @@ public function testThrowsExceptionWhenMessageIsNotAString(): void
4339
SimpleReader::class,
4440
));
4541

46-
$formatReader($config, $data);
42+
$formatReader($data);
4743
}
4844

4945
public function testInvoke(): void
5046
{
51-
$config = new Config(new Locale('en-US'));
5247
$formatReader = new SimpleReader();
5348
$data = ['foo' => 'I am foo', 'bar' => 'I am bar'];
5449

55-
$collection = $formatReader($config, $data);
50+
$collection = $formatReader($data);
5651

5752
$this->assertInstanceOf(MessageCollection::class, $collection);
5853
$this->assertCount(2, $collection);

tests/Format/Reader/SmartlingReaderTest.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
namespace FormatPHP\Test\Format\Reader;
66

7-
use FormatPHP\Config;
87
use FormatPHP\Exception\InvalidMessageShapeException;
98
use FormatPHP\Format\Reader\SmartlingReader;
10-
use FormatPHP\Intl\Locale;
119
use FormatPHP\MessageCollection;
1210
use FormatPHP\MessageInterface;
1311
use FormatPHP\Test\TestCase;
@@ -18,7 +16,6 @@ class SmartlingReaderTest extends TestCase
1816
{
1917
public function testThrowsExceptionWhenMessageIdIsNotAString(): void
2018
{
21-
$config = new Config(new Locale('en'));
2219
$formatReader = new SmartlingReader();
2320
$data = ['smartling' => [], 'foo'];
2421

@@ -28,12 +25,11 @@ public function testThrowsExceptionWhenMessageIdIsNotAString(): void
2825
SmartlingReader::class,
2926
));
3027

31-
$formatReader($config, $data);
28+
$formatReader($data);
3229
}
3330

3431
public function testThrowsExceptionWhenMessageIsNotAString(): void
3532
{
36-
$config = new Config(new Locale('en'));
3733
$formatReader = new SmartlingReader();
3834
$data = ['smartling' => [], 'foo' => ['bar']];
3935

@@ -43,16 +39,15 @@ public function testThrowsExceptionWhenMessageIsNotAString(): void
4339
SmartlingReader::class,
4440
));
4541

46-
$formatReader($config, $data);
42+
$formatReader($data);
4743
}
4844

4945
public function testInvoke(): void
5046
{
51-
$config = new Config(new Locale('en-US'));
5247
$formatReader = new SmartlingReader();
5348
$data = ['smartling' => [], 'foo' => ['message' => 'I am foo'], 'bar' => ['message' => 'I am bar']];
5449

55-
$collection = $formatReader($config, $data);
50+
$collection = $formatReader($data);
5651

5752
$this->assertInstanceOf(MessageCollection::class, $collection);
5853
$this->assertCount(2, $collection);

0 commit comments

Comments
 (0)