Skip to content

Commit 595c0f1

Browse files
authored
fix: ensure zero doesn't break parsing & errors don't interrupt hoisting (#31)
1 parent faf5a70 commit 595c0f1

6 files changed

Lines changed: 46 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## 0.3.1 - 2021-12-17
9+
10+
### Added
11+
12+
- Nothing.
13+
14+
### Changed
15+
16+
- Nothing.
17+
18+
### Deprecated
19+
20+
- Nothing.
21+
22+
### Removed
23+
24+
- Nothing.
25+
26+
### Fixed
27+
28+
- Fixed case where errors occurring during flattening would cause `null` to be passed to a method that does not accept `null`
29+
- Fixed case where "0" character caused a truthy check to fail, since `0 == false`
30+
831
## 0.3.0 - 2021-12-16
932

1033
### Added

src/Extractor/MessageExtractor.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use Psr\Log\LoggerInterface;
5050
use Ramsey\Collection\Exception\CollectionMismatchException;
5151

52+
use function array_filter;
5253
use function class_exists;
5354
use function count;
5455
use function is_a;
@@ -226,18 +227,18 @@ private function loadDescriptorParser(string $parserNameOrScript): DescriptorPar
226227
*/
227228
private function write(callable $formatter, DescriptorCollection $descriptors): void
228229
{
229-
if ($this->options->flatten === true) {
230-
/** @var DescriptorInterface[] $flattened */
231-
$flattened = $descriptors->map($this->flattenMessage())->toArray();
232-
$descriptors = new DescriptorCollection($flattened);
233-
}
234-
235230
if ($this->options->validateMessages === true && count($this->errors) > 0) {
236231
$this->logger->error('Validation errors encountered; extraction failed');
237232

238233
return;
239234
}
240235

236+
if ($this->options->flatten === true) {
237+
/** @var DescriptorInterface[] $flattened */
238+
$flattened = $descriptors->map($this->flattenMessage())->toArray();
239+
$descriptors = new DescriptorCollection(array_filter($flattened));
240+
}
241+
241242
$file = $this->options->outFile ?? 'php://output';
242243

243244
$writerOptions = new WriterOptions();
@@ -282,11 +283,15 @@ public function __invoke(
282283

283284
private function flattenMessage(): Closure
284285
{
285-
return function (Descriptor $descriptor): Descriptor {
286+
return function (Descriptor $descriptor): ?Descriptor {
286287
$message = $descriptor->getDefaultMessage();
287288
$messageFormatParser = new MessageFormatParser((string) $message);
288289
$result = $messageFormatParser->parse();
289290

291+
if ($result->err !== null) {
292+
return null;
293+
}
294+
290295
/** @var MessageFormatParser\Type\ElementCollection $messageAst */
291296
$messageAst = $result->val;
292297

src/Icu/MessageFormat/Parser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,21 +373,21 @@ private function parseLiteral(int $nestingLevel, string $parentArgType): Result
373373

374374
while (true) {
375375
$quoted = $this->tryParseQuote($parentArgType);
376-
if ($quoted) {
376+
if ($quoted !== null) {
377377
$value .= $quoted;
378378

379379
continue;
380380
}
381381

382382
$unquoted = $this->tryParseUnquoted($nestingLevel, $parentArgType);
383-
if ($unquoted) {
383+
if ($unquoted !== null) {
384384
$value .= $unquoted;
385385

386386
continue;
387387
}
388388

389389
$leftAngle = $this->tryParseLeftAngleBracket();
390-
if ($leftAngle) {
390+
if ($leftAngle !== null) {
391391
$value .= $leftAngle;
392392

393393
continue;

tests/Extractor/MessageExtractorTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,10 @@ public function testProcessFlatten(): void
795795
);
796796

797797
ob_start();
798-
$extractor->process([__DIR__ . '/Parser/Descriptor/fixtures/*.ph*']);
798+
$extractor->process([
799+
__DIR__ . '/Parser/Descriptor/fixtures/*.ph*',
800+
__DIR__ . '/../fixtures/invalid-message.php',
801+
]);
799802
$output = ob_get_contents();
800803
ob_end_clean();
801804

tests/Icu/MessageFormat/ManipulatorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public function messageProvider(): array
6464
other{many cats}}
6565
EOD,
6666
],
67+
'should not mangle numbers in messages' => [
68+
'message' => 'All classes are pre-recorded and average 30-40 minutes in total length.',
69+
],
6770
];
6871
}
6972
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
All classes are pre-recorded and average 30-40 minutes in total length.

0 commit comments

Comments
 (0)