Skip to content

Commit 34f43e1

Browse files
authored
feat(format)!: simplify format reader and writer interfaces (#21)
BREAKING CHANGE: change to interface arguments for ReaderInterface and WriterInterface
1 parent 38170ba commit 34f43e1

37 files changed

Lines changed: 174 additions & 143 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1717

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

2123
### Deprecated
2224

src/Extractor/MessageExtractor.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@
3232
use FormatPHP\Extractor\Parser\Descriptor\PhpParser;
3333
use FormatPHP\Extractor\Parser\DescriptorParserInterface;
3434
use FormatPHP\Extractor\Parser\ParserErrorCollection;
35+
use FormatPHP\Format\WriterInterface;
36+
use FormatPHP\Format\WriterOptions;
3537
use FormatPHP\Util\FileSystemHelper;
3638
use FormatPHP\Util\FormatHelper;
3739
use FormatPHP\Util\Globber;
3840
use LogicException;
3941
use Psr\Log\LoggerInterface;
42+
use Ramsey\Collection\Exception\CollectionMismatchException;
4043

4144
use function class_exists;
4245
use function count;
@@ -47,6 +50,9 @@
4750

4851
/**
4952
* Extracts message descriptors from application source code
53+
*
54+
* @psalm-import-type DescriptorParserCallable from DescriptorParserInterface
55+
* @psalm-import-type WriterType from WriterInterface
5056
*/
5157
class MessageExtractor
5258
{
@@ -82,6 +88,7 @@ public function __construct(
8288
* @throws InvalidArgumentException
8389
* @throws ImproperContextException
8490
* @throws LogicException
91+
* @throws CollectionMismatchException
8592
*/
8693
public function process(array $files): void
8794
{
@@ -129,6 +136,7 @@ public function getErrors(): ParserErrorCollection
129136
* @throws UnableToProcessFileException
130137
* @throws ImproperContextException
131138
* @throws LogicException
139+
* @throws CollectionMismatchException
132140
*/
133141
private function parse(DescriptorCollection $descriptors, string $filePath): DescriptorCollection
134142
{
@@ -163,15 +171,14 @@ private function getDescriptorParsers(): array
163171
*/
164172
private function loadDescriptorParser(string $parserNameOrScript): DescriptorParserInterface
165173
{
166-
switch (strtolower($parserNameOrScript)) {
167-
case 'php':
168-
return new PhpParser($this->file);
174+
if (strtolower($parserNameOrScript) === 'php') {
175+
return new PhpParser($this->file);
169176
}
170177

171178
if (class_exists($parserNameOrScript) && is_a($parserNameOrScript, DescriptorParserInterface::class, true)) {
172179
$parser = new $parserNameOrScript();
173180
} else {
174-
/** @var Closure(string,MessageExtractorOptions,ParserErrorCollection):DescriptorCollection | null $parser */
181+
/** @var DescriptorParserCallable | null $parser */
175182
$parser = $this->file->loadClosureFromScript($parserNameOrScript);
176183
}
177184

@@ -194,7 +201,9 @@ private function loadDescriptorParser(string $parserNameOrScript): DescriptorPar
194201
}
195202

196203
/**
197-
* @param callable(DescriptorCollection,MessageExtractorOptions):array<mixed> $formatter
204+
* @see WriterInterface
205+
*
206+
* @param WriterType $formatter
198207
*
199208
* @throws UnableToWriteFileException
200209
* @throws InvalidArgumentException
@@ -203,7 +212,11 @@ private function write(callable $formatter, DescriptorCollection $descriptors):
203212
{
204213
$file = $this->options->outFile ?? 'php://output';
205214

206-
$messages = $formatter($descriptors, $this->options);
215+
$writerOptions = new WriterOptions();
216+
$writerOptions->includesSourceLocation = $this->options->extractSourceLocation;
217+
$writerOptions->includesPragma = $this->options->pragma !== null;
218+
219+
$messages = $formatter($descriptors, $writerOptions);
207220
if (count($messages) === 0) {
208221
$messages = (object) $messages;
209222
}

src/Extractor/Parser/DescriptorParserInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828

2929
/**
3030
* Parses message descriptors from application source code files
31+
*
32+
* @psalm-type DescriptorParserCallable = callable(string,MessageExtractorOptions,ParserErrorCollection):DescriptorCollection
33+
* @psalm-type DescriptorParserType = DescriptorParserInterface | DescriptorParserCallable
3134
*/
3235
interface DescriptorParserInterface
3336
{

src/Format/Reader/FormatPHPReader.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use FormatPHP\Exception\InvalidMessageShapeException;
2727
use FormatPHP\Format\ReaderInterface;
2828
use FormatPHP\Format\Writer\FormatPHPWriter;
29-
use FormatPHP\Intl\LocaleInterface;
3029
use FormatPHP\Message;
3130
use FormatPHP\MessageCollection;
3231

@@ -46,7 +45,7 @@ class FormatPHPReader implements ReaderInterface
4645
/**
4746
* @inheritdoc
4847
*/
49-
public function __invoke(ConfigInterface $config, array $data, LocaleInterface $localeResolved): MessageCollection
48+
public function __invoke(ConfigInterface $config, array $data): MessageCollection
5049
{
5150
$messages = new MessageCollection();
5251

src/Format/Reader/SimpleReader.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use FormatPHP\Exception\InvalidMessageShapeException;
2727
use FormatPHP\Format\ReaderInterface;
2828
use FormatPHP\Format\Writer\SimpleWriter;
29-
use FormatPHP\Intl\LocaleInterface;
3029
use FormatPHP\Message;
3130
use FormatPHP\MessageCollection;
3231

@@ -46,7 +45,7 @@ class SimpleReader implements ReaderInterface
4645
/**
4746
* @inheritdoc
4847
*/
49-
public function __invoke(ConfigInterface $config, array $data, LocaleInterface $localeResolved): MessageCollection
48+
public function __invoke(ConfigInterface $config, array $data): MessageCollection
5049
{
5150
$messages = new MessageCollection();
5251

src/Format/Reader/SmartlingReader.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use FormatPHP\Exception\InvalidMessageShapeException;
2727
use FormatPHP\Format\ReaderInterface;
2828
use FormatPHP\Format\Writer\SmartlingWriter;
29-
use FormatPHP\Intl\LocaleInterface;
3029
use FormatPHP\Message;
3130
use FormatPHP\MessageCollection;
3231

@@ -46,7 +45,7 @@ class SmartlingReader implements ReaderInterface
4645
/**
4746
* @inheritdoc
4847
*/
49-
public function __invoke(ConfigInterface $config, array $data, LocaleInterface $localeResolved): MessageCollection
48+
public function __invoke(ConfigInterface $config, array $data): MessageCollection
5049
{
5150
$messages = new MessageCollection();
5251

src/Format/ReaderInterface.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,21 @@
2424

2525
use FormatPHP\ConfigInterface;
2626
use FormatPHP\Exception\InvalidMessageShapeException;
27-
use FormatPHP\Intl\LocaleInterface;
2827
use FormatPHP\MessageCollection;
2928

3029
/**
3130
* Returns a collection of messages parsed from JSON-decoded message data
31+
*
32+
* @psalm-type ReaderCallableType = callable(ConfigInterface,mixed[]):MessageCollection
33+
* @psalm-type ReaderType = ReaderInterface | ReaderCallableType
3234
*/
3335
interface ReaderInterface
3436
{
3537
/**
3638
* @param array<array-key, mixed> $data An arbitrary array of JSON-decoded
3739
* data, loaded from a message file.
38-
* @param LocaleInterface $localeResolved We utilize a "fallback" algorithm
39-
* to look up a suitable replacement locale (i.e., if we receive "en-US"
40-
* and have only a locale for "en," we will use "en" instead). This
41-
* parameter is the actual locale we used, which may be different from
42-
* the one provided on Config.
4340
*
4441
* @throws InvalidMessageShapeException
4542
*/
46-
public function __invoke(ConfigInterface $config, array $data, LocaleInterface $localeResolved): MessageCollection;
43+
public function __invoke(ConfigInterface $config, array $data): MessageCollection;
4744
}

src/Format/Writer/FormatPHPWriter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
use FormatPHP\DescriptorCollection;
2626
use FormatPHP\ExtendedDescriptorInterface;
27-
use FormatPHP\Extractor\MessageExtractorOptions;
2827
use FormatPHP\Format\Reader\FormatPHPReader;
2928
use FormatPHP\Format\WriterInterface;
29+
use FormatPHP\Format\WriterOptions;
3030

3131
use function array_merge;
3232
use function ksort;
@@ -64,7 +64,7 @@ class FormatPHPWriter implements WriterInterface
6464
/**
6565
* @inheritdoc
6666
*/
67-
public function __invoke(DescriptorCollection $collection, MessageExtractorOptions $options): array
67+
public function __invoke(DescriptorCollection $collection, WriterOptions $options): array
6868
{
6969
$format = [];
7070

@@ -76,7 +76,7 @@ public function __invoke(DescriptorCollection $collection, MessageExtractorOptio
7676
$message['description'] = $item->getDescription();
7777
}
7878

79-
if ($options->extractSourceLocation === true && $item instanceof ExtendedDescriptorInterface) {
79+
if ($options->includesSourceLocation && $item instanceof ExtendedDescriptorInterface) {
8080
$message = array_merge($message, [
8181
'end' => $item->getSourceEndOffset(),
8282
'file' => $item->getSourceFile(),
@@ -85,7 +85,7 @@ public function __invoke(DescriptorCollection $collection, MessageExtractorOptio
8585
]);
8686
}
8787

88-
if ($options->pragma !== null && $item instanceof ExtendedDescriptorInterface) {
88+
if ($options->includesPragma && $item instanceof ExtendedDescriptorInterface) {
8989
$message = array_merge($message, [
9090
'meta' => $item->getMetadata(),
9191
]);

src/Format/Writer/SimpleWriter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
namespace FormatPHP\Format\Writer;
2424

2525
use FormatPHP\DescriptorCollection;
26-
use FormatPHP\Extractor\MessageExtractorOptions;
2726
use FormatPHP\Format\Reader\SimpleReader;
2827
use FormatPHP\Format\WriterInterface;
28+
use FormatPHP\Format\WriterOptions;
2929

3030
/**
3131
* A simple formatter for FormatPHP, producing message key-value pairs
@@ -45,7 +45,7 @@ class SimpleWriter implements WriterInterface
4545
/**
4646
* @inheritdoc
4747
*/
48-
public function __invoke(DescriptorCollection $collection, MessageExtractorOptions $options): array
48+
public function __invoke(DescriptorCollection $collection, WriterOptions $options): array
4949
{
5050
$simple = [];
5151
foreach ($collection as $item) {

src/Format/Writer/SmartlingWriter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
namespace FormatPHP\Format\Writer;
2424

2525
use FormatPHP\DescriptorCollection;
26-
use FormatPHP\Extractor\MessageExtractorOptions;
2726
use FormatPHP\Format\Reader\SmartlingReader;
2827
use FormatPHP\Format\WriterInterface;
28+
use FormatPHP\Format\WriterOptions;
2929

3030
/**
3131
* Smartling formatter for FormatPHP
@@ -61,7 +61,7 @@ class SmartlingWriter implements WriterInterface
6161
/**
6262
* @inheritdoc
6363
*/
64-
public function __invoke(DescriptorCollection $collection, MessageExtractorOptions $options): array
64+
public function __invoke(DescriptorCollection $collection, WriterOptions $options): array
6565
{
6666
$format = [
6767
'smartling' => [

0 commit comments

Comments
 (0)