Skip to content

Commit 50db9da

Browse files
authored
refactor: move readers and writers under common Format namespace (#14)
1 parent a9bd712 commit 50db9da

28 files changed

Lines changed: 123 additions & 111 deletions

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ version: 2
22
jobs:
33
build:
44
docker:
5-
- image: cimg/php:8.0
5+
- image: cimg/php:7.4
66

77
steps:
88
- checkout
99
- run: |
1010
sudo apt update
11-
sudo apt install php-intl php-pcov
11+
sudo apt install php7.4-intl php7.4-pcov
1212
1313
# Install Composer dependencies.
1414
- restore_cache:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"ext-intl": "*",
2121
"ext-json": "*",
2222
"nikic/php-parser": "^4.13",
23-
"psr/log": "^2",
23+
"psr/log": "^1 || ^2",
2424
"ramsey/collection": "^1.2",
2525
"symfony/console": "^5.3",
2626
"symfony/polyfill-php80": "^1.23",

phpcs.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<exclude name="SlevomatCodingStandard.Classes.SuperfluousExceptionNaming"/>
1717
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming"/>
1818
<exclude name="SlevomatCodingStandard.Classes.SuperfluousTraitNaming"/>
19+
20+
<!-- Ignore this for PHP 7.4 -->
21+
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint"/>
1922
</rule>
2023

2124
</ruleset>

src/Console/Command/ExtractCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,12 @@ private function buildOptions(InputInterface $input): MessageExtractorOptions
212212
$options->extractSourceLocation = (bool) $input->getOption('extract-source-location');
213213
$options->throws = (bool) $input->getOption('throws');
214214
$options->preserveWhitespace = (bool) $input->getOption('preserve-whitespace');
215+
216+
/** @var string $additionalFunctionNames */
217+
$additionalFunctionNames = $input->getOption('additional-function-names') ?? '';
215218
$options->additionalFunctionNames = array_merge(
216219
self::DEFAULT_FUNCTION_NAMES,
217-
array_map('trim', explode(',', (string) $input->getOption('additional-function-names'))),
220+
array_map('trim', explode(',', $additionalFunctionNames)),
218221
);
219222

220223
return $options;

src/DescriptorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function setId(string $id): void;
5757
/**
5858
* Returns an array representation of the descriptor
5959
*
60-
* @return array<string, string | int | array | null>
60+
* @return array<string, string | int | mixed[] | null>
6161
*/
6262
public function toArray(): array;
6363
}

src/Exception/InvalidMessageShapeException.php

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

2323
namespace FormatPHP\Exception;
2424

25-
use FormatPHP\Reader\FormatInterface;
25+
use FormatPHP\Format\ReaderInterface;
2626
use RuntimeException as PhpRuntimeException;
2727

2828
/**
2929
* Thrown when reading a message that doesn't conform to the expected shape
3030
*
31-
* @see FormatInterface
31+
* @see ReaderInterface
3232
*/
3333
class InvalidMessageShapeException extends PhpRuntimeException implements FormatPHPExceptionInterface
3434
{

src/Extractor/MessageExtractor.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
use FormatPHP\Exception\UnableToWriteFileException;
3232
use FormatPHP\Extractor\Parser\Descriptor\PhpParser;
3333
use FormatPHP\Extractor\Parser\DescriptorParserInterface;
34+
use FormatPHP\Format\Writer\FormatPHPWriter;
35+
use FormatPHP\Format\Writer\SimpleWriter;
36+
use FormatPHP\Format\Writer\SmartlingWriter;
37+
use FormatPHP\Format\WriterInterface;
3438
use FormatPHP\Util\FileSystemHelper;
3539
use FormatPHP\Util\Globber;
36-
use FormatPHP\Writer\Format\FormatPHP;
37-
use FormatPHP\Writer\Format\Simple;
38-
use FormatPHP\Writer\Format\Smartling;
39-
use FormatPHP\Writer\FormatInterface;
4040
use LogicException;
4141
use Psr\Log\LoggerInterface;
4242

@@ -183,20 +183,20 @@ private function getPhpParser(): PhpParser
183183
private function getFormatter(?string $format): callable
184184
{
185185
if ($format === null) {
186-
return new FormatPHP();
186+
return new FormatPHPWriter();
187187
}
188188

189189
switch (strtolower($format)) {
190190
case 'simple':
191-
return new Simple();
191+
return new SimpleWriter();
192192
case 'smartling':
193-
return new Smartling();
193+
return new SmartlingWriter();
194194
case 'formatjs':
195195
case 'formatphp':
196-
return new FormatPHP();
196+
return new FormatPHPWriter();
197197
}
198198

199-
if (class_exists($format) && is_a($format, FormatInterface::class, true)) {
199+
if (class_exists($format) && is_a($format, WriterInterface::class, true)) {
200200
$formatter = new $format();
201201
} else {
202202
/** @var Closure(DescriptorCollection,MessageExtractorOptions):array<mixed> | null $formatter */
@@ -210,7 +210,7 @@ private function getFormatter(?string $format): callable
210210
throw new InvalidArgumentException(sprintf(
211211
'The format provided is not a known format, an instance of '
212212
. '%s, or a callable of the shape `callable(%s,%s):array<mixed>`.',
213-
FormatInterface::class,
213+
WriterInterface::class,
214214
DescriptorCollection::class,
215215
MessageExtractorOptions::class,
216216
));
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020

2121
declare(strict_types=1);
2222

23-
namespace FormatPHP\Reader\Format;
23+
namespace FormatPHP\Format\Reader;
2424

2525
use FormatPHP\Config;
2626
use FormatPHP\Exception\InvalidMessageShapeException;
27+
use FormatPHP\Format\ReaderInterface;
28+
use FormatPHP\Format\Writer\FormatPHPWriter;
2729
use FormatPHP\Intl\LocaleInterface;
2830
use FormatPHP\Message;
2931
use FormatPHP\MessageCollection;
30-
use FormatPHP\Reader\FormatInterface;
3132

32-
use function assert;
3333
use function gettype;
3434
use function is_array;
3535
use function is_string;
@@ -39,9 +39,9 @@
3939
* Returns a MessageCollection parsed from JSON-decoded data that was written
4040
* using Writer\Format\FormatPHP
4141
*
42-
* @see \FormatPHP\Writer\Format\FormatPHP
42+
* @see FormatPHPWriter
4343
*/
44-
class FormatPHP implements FormatInterface
44+
class FormatPHPReader implements ReaderInterface
4545
{
4646
/**
4747
* @inheritdoc
@@ -50,11 +50,12 @@ public function __invoke(Config $config, array $data, LocaleInterface $localeRes
5050
{
5151
$messages = new MessageCollection($config);
5252

53+
/**
54+
* @var string $messageId
55+
* @var array{defaultMessage: string} $message
56+
*/
5357
foreach ($data as $messageId => $message) {
5458
$this->validateShape($messageId, $message);
55-
assert(is_string($messageId));
56-
assert(isset($message['defaultMessage']));
57-
assert(is_string($message['defaultMessage']));
5859

5960
$messages[] = new Message($localeResolved, $messageId, $message['defaultMessage']);
6061
}
@@ -67,8 +68,6 @@ public function __invoke(Config $config, array $data, LocaleInterface $localeRes
6768
* @param mixed $message
6869
*
6970
* @throws InvalidMessageShapeException
70-
*
71-
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
7271
*/
7372
private function validateShape($messageId, $message): void
7473
{
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020

2121
declare(strict_types=1);
2222

23-
namespace FormatPHP\Reader\Format;
23+
namespace FormatPHP\Format\Reader;
2424

2525
use FormatPHP\Config;
2626
use FormatPHP\Exception\InvalidMessageShapeException;
27+
use FormatPHP\Format\ReaderInterface;
28+
use FormatPHP\Format\Writer\SimpleWriter;
2729
use FormatPHP\Intl\LocaleInterface;
2830
use FormatPHP\Message;
2931
use FormatPHP\MessageCollection;
30-
use FormatPHP\Reader\FormatInterface;
3132

3233
use function assert;
3334
use function gettype;
@@ -38,9 +39,9 @@
3839
* Returns a MessageCollection parsed from JSON-decoded data that was written
3940
* using Writer\Format\Simple
4041
*
41-
* @see \FormatPHP\Writer\Format\Simple
42+
* @see SimpleWriter
4243
*/
43-
class Simple implements FormatInterface
44+
class SimpleReader implements ReaderInterface
4445
{
4546
/**
4647
* @inheritdoc
@@ -65,8 +66,6 @@ public function __invoke(Config $config, array $data, LocaleInterface $localeRes
6566
* @param mixed $message
6667
*
6768
* @throws InvalidMessageShapeException
68-
*
69-
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
7069
*/
7170
private function validateShape($messageId, $message): void
7271
{
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020

2121
declare(strict_types=1);
2222

23-
namespace FormatPHP\Reader\Format;
23+
namespace FormatPHP\Format\Reader;
2424

2525
use FormatPHP\Config;
2626
use FormatPHP\Exception\InvalidMessageShapeException;
27+
use FormatPHP\Format\ReaderInterface;
28+
use FormatPHP\Format\Writer\SmartlingWriter;
2729
use FormatPHP\Intl\LocaleInterface;
2830
use FormatPHP\Message;
2931
use FormatPHP\MessageCollection;
30-
use FormatPHP\Reader\FormatInterface;
3132

32-
use function assert;
3333
use function gettype;
3434
use function is_array;
3535
use function is_string;
@@ -39,9 +39,9 @@
3939
* Returns a MessageCollection parsed from JSON-decoded data that was written
4040
* using Writer\Format\Smartling
4141
*
42-
* @see \FormatPHP\Writer\Format\Smartling
42+
* @see SmartlingWriter
4343
*/
44-
class Smartling implements FormatInterface
44+
class SmartlingReader implements ReaderInterface
4545
{
4646
/**
4747
* @inheritdoc
@@ -52,11 +52,12 @@ public function __invoke(Config $config, array $data, LocaleInterface $localeRes
5252

5353
unset($data['smartling']);
5454

55+
/**
56+
* @var string $messageId
57+
* @var array{message: string} $message
58+
*/
5559
foreach ($data as $messageId => $message) {
5660
$this->validateShape($messageId, $message);
57-
assert(is_string($messageId));
58-
assert(isset($message['message']));
59-
assert(is_string($message['message']));
6061

6162
$messages[] = new Message($localeResolved, $messageId, $message['message']);
6263
}
@@ -69,8 +70,6 @@ public function __invoke(Config $config, array $data, LocaleInterface $localeRes
6970
* @param mixed $message
7071
*
7172
* @throws InvalidMessageShapeException
72-
*
73-
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
7473
*/
7574
private function validateShape($messageId, $message): void
7675
{

0 commit comments

Comments
 (0)