Skip to content

Commit d084403

Browse files
committed
feature: review translations loaders
1 parent 041d482 commit d084403

13 files changed

+87
-235
lines changed

Translation/Comparison/CatalogueComparator.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,27 @@
2929
*/
3030
class CatalogueComparator
3131
{
32-
private $domains = [];
33-
private $ignoredDomains = [];
32+
public function __construct(
33+
private array $domains = [],
34+
private array $ignoredDomains = [],
35+
) {
36+
}
3437

35-
public function setDomains(array $domains)
38+
public function setDomains(array $domains): static
3639
{
3740
$this->domains = $domains;
41+
42+
return $this;
3843
}
3944

40-
/**
41-
* @param array $domains
42-
*/
43-
public function setIgnoredDomains(array $domains)
45+
public function setIgnoredDomains(array $domains): static
4446
{
4547
$this->ignoredDomains = $domains;
48+
49+
return $this;
4650
}
4751

48-
/**
49-
* Compares two message catalogues.
50-
*
51-
* @param MessageCatalogue $current
52-
* @param MessageCatalogue $new
53-
*
54-
* @return ChangeSet
55-
*/
56-
public function compare(MessageCatalogue $current, MessageCatalogue $new)
52+
public function compare(MessageCatalogue $current, MessageCatalogue $new): ChangeSet
5753
{
5854
$newMessages = [];
5955

@@ -78,6 +74,7 @@ public function compare(MessageCatalogue $current, MessageCatalogue $new)
7874
}
7975

8076
$deletedMessages = [];
77+
8178
foreach ($current->getDomains() as $name => $domain) {
8279
if ($this->domains && !isset($this->domains[$name])) {
8380
continue;

Translation/Comparison/ChangeSet.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,30 @@
2020

2121
namespace JMS\TranslationBundle\Translation\Comparison;
2222

23+
use JMS\TranslationBundle\Model\Message;
24+
2325
class ChangeSet
2426
{
25-
/**
26-
* @var array
27-
*/
28-
private $addedMessages;
29-
30-
/**
31-
* @var array
32-
*/
33-
private $deletedMessages;
34-
35-
/**
36-
* @param array $addedMessages
37-
* @param array $deletedMessages
38-
*/
39-
public function __construct(array $addedMessages, array $deletedMessages)
40-
{
41-
$this->addedMessages = $addedMessages;
42-
$this->deletedMessages = $deletedMessages;
27+
public function __construct(
28+
/** @var Message[] */
29+
private readonly array $addedMessages,
30+
/** @var Message[] */
31+
private readonly array $deletedMessages,
32+
) {
4333
}
4434

4535
/**
46-
* @return array
36+
* @return Message[]
4737
*/
48-
public function getAddedMessages()
38+
public function getAddedMessages(): array
4939
{
5040
return $this->addedMessages;
5141
}
5242

5343
/**
54-
* @return array
44+
* @return Message[]
5545
*/
56-
public function getDeletedMessages()
46+
public function getDeletedMessages(): array
5747
{
5848
return $this->deletedMessages;
5949
}

Translation/Dumper/ArrayStructureDumper.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,16 @@
2525

2626
abstract class ArrayStructureDumper implements DumperInterface
2727
{
28-
/**
29-
* @var bool
30-
*/
31-
private $prettyPrint = true;
28+
private bool $prettyPrint = true;
3229

33-
public function setPrettyPrint($bool)
30+
public function setPrettyPrint(bool $bool): static
3431
{
35-
$this->prettyPrint = (bool) $bool;
32+
$this->prettyPrint = $bool;
33+
34+
return $this;
3635
}
3736

38-
/**
39-
* @param MessageCatalogue $catalogue
40-
* @param string $domain
41-
*
42-
* @return string
43-
*/
44-
public function dump(MessageCatalogue $catalogue, $domain = 'messages')
37+
public function dump(MessageCatalogue $catalogue, string $domain = 'messages'): string
4538
{
4639
$structure = $catalogue->getDomain($domain)->all();
4740

@@ -84,10 +77,5 @@ public function dump(MessageCatalogue $catalogue, $domain = 'messages')
8477
return $this->dumpStructure($structure);
8578
}
8679

87-
/**
88-
* @param array $structure
89-
*
90-
* @return string
91-
*/
92-
abstract protected function dumpStructure(array $structure);
80+
abstract protected function dumpStructure(array $structure): string;
9381
}

Translation/Dumper/DumperInterface.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ interface DumperInterface
3434
{
3535
/**
3636
* Dumps the messages of the given domain.
37-
*
38-
* @param MessageCatalogue $catalogue
39-
* @param string $domain
40-
*
41-
* @return string
4237
*/
43-
public function dump(MessageCatalogue $catalogue, $domain = 'messages');
38+
public function dump(MessageCatalogue $catalogue, string $domain = 'messages'): string;
4439
}

Translation/Dumper/PhpDumper.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,12 @@
2525

2626
class PhpDumper extends ArrayStructureDumper
2727
{
28-
/**
29-
* @var Writer
30-
*/
31-
private $writer;
32-
33-
public function __construct()
34-
{
35-
$this->writer = new Writer();
28+
public function __construct(
29+
private Writer $writer = new Writer(),
30+
) {
3631
}
3732

38-
/**
39-
* @param array $structure
40-
*
41-
* @return string
42-
*/
43-
protected function dumpStructure(array $structure)
33+
protected function dumpStructure(array $structure): string
4434
{
4535
$this->writer
4636
->reset()
@@ -53,10 +43,7 @@ protected function dumpStructure(array $structure)
5343
return $this->writer->outdent()->writeln(');')->getContent();
5444
}
5545

56-
/**
57-
* @param array $structure
58-
*/
59-
private function dumpStructureRecursively(array $structure)
46+
private function dumpStructureRecursively(array $structure): void
6047
{
6148
$isFirst = true;
6249
$precededByMessage = false;

Translation/Dumper/SymfonyDumperAdapter.php

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,13 @@
3838
*/
3939
class SymfonyDumperAdapter implements DumperInterface
4040
{
41-
/**
42-
* @var SymfonyDumper
43-
*/
44-
private $dumper;
45-
46-
/**
47-
* @var string
48-
*/
49-
private $format;
50-
51-
public function __construct(SymfonyDumper $dumper, $format)
52-
{
53-
$this->dumper = $dumper;
54-
$this->format = $format;
41+
public function __construct(
42+
private SymfonyDumper $dumper,
43+
private string $format,
44+
) {
5545
}
5646

57-
/**
58-
* @param MessageCatalogue $catalogue
59-
* @param string $domain
60-
*
61-
* @return string
62-
*
63-
* @throws RuntimeException
64-
*/
65-
public function dump(MessageCatalogue $catalogue, $domain = 'messages')
47+
public function dump(MessageCatalogue $catalogue, string $domain = 'messages'): string
6648
{
6749
$symfonyCatalogue = new SymfonyCatalogue($catalogue->getLocale());
6850

Translation/Dumper/XliffDumper.php

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use JMS\TranslationBundle\Model\FileSource;
2525
use JMS\TranslationBundle\Model\Message\XliffMessage;
2626
use JMS\TranslationBundle\Model\MessageCatalogue;
27+
use JMS\TranslationBundle\Model\SourceInterface;
2728

2829
/**
2930
* XLIFF dumper.
@@ -36,53 +37,43 @@
3637
*/
3738
class XliffDumper implements DumperInterface
3839
{
39-
/**
40-
* @var string
41-
*/
42-
private $sourceLanguage = 'en';
40+
private string $sourceLanguage = 'en';
4341

44-
/**
45-
* @var bool
46-
*/
47-
private $addDate = true;
42+
private bool $addDate = true;
4843

49-
/**
50-
* @var bool
51-
*/
52-
private $addReference = true;
44+
private bool $addReference = true;
5345

54-
/**
55-
* @var bool
56-
*/
57-
private $addReferencePosition = true;
46+
private bool $addReferencePosition = true;
5847

59-
public function setAddDate($bool)
48+
public function setAddDate(bool $bool): static
6049
{
61-
$this->addDate = (bool) $bool;
50+
$this->addDate = $bool;
51+
52+
return $this;
6253
}
6354

64-
public function setSourceLanguage($lang)
55+
public function setSourceLanguage(string $lang): static
6556
{
6657
$this->sourceLanguage = $lang;
58+
59+
return $this;
6760
}
6861

69-
public function setAddReference($bool)
62+
public function setAddReference(bool $bool): static
7063
{
7164
$this->addReference = $bool;
65+
66+
return $this;
7267
}
7368

74-
public function setAddReferencePosition($bool)
69+
public function setAddReferencePosition(bool $bool): static
7570
{
7671
$this->addReferencePosition = $bool;
72+
73+
return $this;
7774
}
7875

79-
/**
80-
* @param MessageCatalogue $catalogue
81-
* @param MessageCatalogue|string $domain
82-
*
83-
* @return string
84-
*/
85-
public function dump(MessageCatalogue $catalogue, $domain = 'messages')
76+
public function dump(MessageCatalogue $catalogue, string $domain = 'messages'): string
8677
{
8778
$doc = new \DOMDocument('1.0', 'utf-8');
8879
$doc->formatOutput = true;
@@ -199,13 +190,13 @@ public function dump(MessageCatalogue $catalogue, $domain = 'messages')
199190

200191
/**
201192
* Sort the sources by path-line-column
202-
* If the reference position are not used, the reference file will be write once
193+
* If the reference position are not used, the reference file will be written once
203194
*
204-
* @param array $sources
195+
* @param SourceInterface[] $sources
205196
*
206-
* @return array
197+
* @return array<string, SourceInterface[]>
207198
*/
208-
protected function getSortedSources(array $sources)
199+
protected function getSortedSources(array $sources): array
209200
{
210201
$indexedSources = [];
211202
foreach ($sources as $source) {

Translation/Dumper/YamlDumper.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,20 @@
2626

2727
class YamlDumper extends ArrayStructureDumper
2828
{
29-
/**
30-
* @var Writer
31-
*/
32-
private $writer;
33-
34-
public function __construct()
35-
{
36-
$this->writer = new Writer();
29+
public function __construct(
30+
private Writer $writer = new Writer(),
31+
) {
3732
}
3833

39-
/**
40-
* @param array $structure
41-
*
42-
* @return string
43-
*/
44-
protected function dumpStructure(array $structure)
34+
protected function dumpStructure(array $structure): string
4535
{
4636
$this->writer->reset();
4737
$this->dumpStructureRecursively($structure);
4838

4939
return $this->writer->getContent();
5040
}
5141

52-
/**
53-
* @param array $structure
54-
*/
55-
private function dumpStructureRecursively(array $structure)
42+
private function dumpStructureRecursively(array $structure): void
5643
{
5744
$isFirst = true;
5845
$precededByMessage = false;

Translation/Loader/LoaderInterface.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ interface LoaderInterface
3535
* The difference to Symfony's interface is that any loader is
3636
* expected to return the MessageCatalogue from the bundle which
3737
* contains more translation information.
38-
*
39-
* @param mixed $resource
40-
* @param string $locale
41-
* @param string $domain
42-
*
43-
* @return MessageCatalogue
4438
*/
45-
public function load($resource, $locale, $domain = 'messages');
39+
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue;
4640
}

Translation/Loader/Symfony/XliffLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// phpcs:ignore
3838
class XliffLoader implements LoaderInterface
3939
{
40-
public function load($resource, string $locale, string $domain = 'messages'): MessageCatalogue
40+
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
4141
{
4242
$previous = libxml_use_internal_errors(true);
4343
if (false === $xml = simplexml_load_file((string) $resource)) {

0 commit comments

Comments
 (0)