Skip to content

Commit 8d3ead8

Browse files
authored
Merge pull request #342 from ker0x/type-hinting
Add I/O type hinting, use null coalescing operator
2 parents f96c7cd + b794c18 commit 8d3ead8

File tree

75 files changed

+365
-727
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+365
-727
lines changed

Catalogue/CatalogueCounter.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
*/
2323
class CatalogueCounter
2424
{
25-
/**
26-
* @return int
27-
*/
28-
public function getNumberOfDefinedMessages(MessageCatalogueInterface $catalogue)
25+
public function getNumberOfDefinedMessages(MessageCatalogueInterface $catalogue): int
2926
{
3027
$total = 0;
3128
foreach ($catalogue->getDomains() as $domain) {
@@ -35,10 +32,7 @@ public function getNumberOfDefinedMessages(MessageCatalogueInterface $catalogue)
3532
return $total;
3633
}
3734

38-
/**
39-
* @return array
40-
*/
41-
public function getCatalogueStatistics(MessageCatalogueInterface $catalogue)
35+
public function getCatalogueStatistics(MessageCatalogueInterface $catalogue): array
4236
{
4337
$result = [];
4438
$domains = $catalogue->getDomains();

Catalogue/CatalogueFetcher.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ public function __construct($reader)
4848

4949
/**
5050
* load any existing messages from the translation files.
51-
*
52-
* @return MessageCatalogue[]
5351
*/
54-
public function getCatalogues(Configuration $config, array $locales = [])
52+
public function getCatalogues(Configuration $config, array $locales = []): array
5553
{
5654
$dirs = $config->getPathsToTranslationFiles();
5755
if (empty($locales)) {
@@ -80,21 +78,16 @@ public function getCatalogues(Configuration $config, array $locales = [])
8078
return $catalogues;
8179
}
8280

83-
/**
84-
* @param string $domain
85-
*
86-
* @return bool
87-
*/
88-
private function isValidDomain(Configuration $config, $domain)
81+
private function isValidDomain(Configuration $config, string $domain): bool
8982
{
9083
$blacklist = $config->getBlacklistDomains();
9184
$whitelist = $config->getWhitelistDomains();
9285

93-
if (!empty($blacklist) && \in_array($domain, $blacklist)) {
86+
if (!empty($blacklist) && \in_array($domain, $blacklist, true)) {
9487
return false;
9588
}
9689

97-
if (!empty($whitelist) && !\in_array($domain, $whitelist)) {
90+
if (!empty($whitelist) && !\in_array($domain, $whitelist, true)) {
9891
return false;
9992
}
10093

Catalogue/CatalogueManager.php

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,20 @@ final class CatalogueManager
2626
/**
2727
* @var MessageCatalogueInterface[]
2828
*/
29-
private $catalogues;
29+
private $catalogues = [];
3030

3131
/**
3232
* @param MessageCatalogueInterface[] $catalogues
3333
*/
34-
public function load(array $catalogues)
34+
public function load(array $catalogues): void
3535
{
3636
$this->catalogues = [];
3737
foreach ($catalogues as $c) {
3838
$this->catalogues[$c->getLocale()] = $c;
3939
}
4040
}
4141

42-
/**
43-
* @return array
44-
*/
45-
public function getDomains()
42+
public function getDomains(): array
4643
{
4744
/** @var MessageCatalogueInterface $c */
4845
$c = \reset($this->catalogues);
@@ -51,12 +48,9 @@ public function getDomains()
5148
}
5249

5350
/**
54-
* @param string $locale
55-
* @param string $domain
56-
*
5751
* @return CatalogueMessage[]
5852
*/
59-
public function getMessages($locale, $domain)
53+
public function getMessages(string $locale, string $domain): array
6054
{
6155
$messages = [];
6256
if (!isset($this->catalogues[$locale])) {
@@ -82,12 +76,12 @@ public function getMessages($locale, $domain)
8276
*
8377
* @return CatalogueMessage[]
8478
*/
85-
public function findMessages(array $config = [])
79+
public function findMessages(array $config = []): array
8680
{
87-
$inputDomain = isset($config['domain']) ? $config['domain'] : null;
88-
$isNew = isset($config['isNew']) ? $config['isNew'] : null;
89-
$isObsolete = isset($config['isObsolete']) ? $config['isObsolete'] : null;
90-
$isApproved = isset($config['isApproved']) ? $config['isApproved'] : null;
81+
$inputDomain = $config['domain'] ?? null;
82+
$isNew = $config['isNew'] ?? null;
83+
$isObsolete = $config['isObsolete'] ?? null;
84+
$isApproved = $config['isApproved'] ?? null;
9185

9286
$messages = [];
9387
$catalogues = [];
@@ -112,7 +106,7 @@ public function findMessages(array $config = [])
112106
}
113107
}
114108

115-
$messages = \array_filter($messages, function (CatalogueMessage $m) use ($isNew, $isObsolete, $isApproved) {
109+
$messages = \array_filter($messages, static function (CatalogueMessage $m) use ($isNew, $isObsolete, $isApproved) {
116110
if (null !== $isNew && $m->isNew() !== $isNew) {
117111
return false;
118112
}
@@ -132,10 +126,8 @@ public function findMessages(array $config = [])
132126
/**
133127
* @param string $domain
134128
* @param string $key
135-
*
136-
* @return array
137129
*/
138-
public function getTranslations($domain, $key)
130+
public function getTranslations($domain, $key): array
139131
{
140132
$translations = [];
141133
foreach ($this->catalogues as $locale => $catalogue) {
@@ -147,15 +139,7 @@ public function getTranslations($domain, $key)
147139
return $translations;
148140
}
149141

150-
/**
151-
* @param $locale
152-
* @param $domain
153-
* @param $key
154-
* @param $text
155-
*
156-
* @return CatalogueMessage
157-
*/
158-
private function createMessage(MessageCatalogueInterface $catalogue, $locale, $domain, $key, $text)
142+
private function createMessage(MessageCatalogueInterface $catalogue, string $locale, string $domain, string $key, string $text): CatalogueMessage
159143
{
160144
$catalogueMessage = new CatalogueMessage($this, $locale, $domain, $key, $text);
161145

Catalogue/CatalogueWriter.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ final class CatalogueWriter
3636
*/
3737
private $defaultLocale;
3838

39-
/**
40-
* @param string $defaultLocale
41-
*/
42-
public function __construct(TranslationWriter $writer, $defaultLocale)
39+
public function __construct(TranslationWriter $writer, string $defaultLocale)
4340
{
4441
if (!$writer instanceof TranslationWriterInterface) {
4542
$writer = new LegacyTranslationWriter($writer);
@@ -52,7 +49,7 @@ public function __construct(TranslationWriter $writer, $defaultLocale)
5249
/**
5350
* @param MessageCatalogue[] $catalogues
5451
*/
55-
public function writeCatalogues(Configuration $config, array $catalogues)
52+
public function writeCatalogues(Configuration $config, array $catalogues): void
5653
{
5754
foreach ($catalogues as $catalogue) {
5855
$this->writer->write(

Catalogue/Operation/ReplaceOperation.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
final class ReplaceOperation extends AbstractOperation
3232
{
33-
protected function processDomain($domain)
33+
protected function processDomain($domain): void
3434
{
3535
$this->messages[$domain] = [
3636
'all' => [],
@@ -96,25 +96,20 @@ protected function processDomain($domain)
9696
}
9797

9898
/**
99+
* @param MessageCatalogueInterface|MetadataAwareInterface $catalogue
100+
*
99101
* @return array|string|mixed|null Can return anything..
100102
*/
101-
private function getMetadata(MessageCatalogueInterface $catalogue, string $domain, string $key = '')
103+
private function getMetadata($catalogue, string $domain, string $key = '')
102104
{
103105
if (!$this->target instanceof MetadataAwareInterface) {
104106
return [];
105107
}
106108

107-
/* @var MetadataAwareInterface $catalogue */
108109
return $catalogue->getMetadata($key, $domain);
109110
}
110111

111-
/**
112-
* @param array|null $source
113-
* @param array|null $target
114-
*
115-
* @return array
116-
*/
117-
private function mergeMetadata($source, $target)
112+
private function mergeMetadata(?array $source, ?array $target): array
118113
{
119114
if (empty($source) && empty($target)) {
120115
return [];
@@ -132,12 +127,10 @@ private function mergeMetadata($source, $target)
132127
return $source;
133128
}
134129

135-
$result = $this->doMergeMetadata($source, $target);
136-
137-
return $result;
130+
return $this->doMergeMetadata($source, $target);
138131
}
139132

140-
private function doMergeMetadata(array $source, array $target)
133+
private function doMergeMetadata(array $source, array $target): array
141134
{
142135
$isTargetArrayAssociative = $this->isArrayAssociative($target);
143136
foreach ($target as $key => $value) {
@@ -153,15 +146,15 @@ private function doMergeMetadata(array $source, array $target)
153146
$source[$key] = $value;
154147
}
155148
// if sequential
156-
} elseif (!\in_array($value, $source)) {
149+
} elseif (!\in_array($value, $source, true)) {
157150
$source[] = $value;
158151
}
159152
}
160153

161154
return $source;
162155
}
163156

164-
public function isArrayAssociative(array $arr)
157+
public function isArrayAssociative(array $arr): bool
165158
{
166159
if ([] === $arr) {
167160
return false;

Command/BundleTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
trait BundleTrait
1919
{
20-
private function configureBundleDirs(InputInterface $input, Configuration $config)
20+
private function configureBundleDirs(InputInterface $input, Configuration $config): void
2121
{
2222
if ($bundleName = $input->getOption('bundle')) {
23-
if ('@' === $bundleName[0]) {
23+
if (0 === \strpos($bundleName, '@')) {
2424
if (false === $pos = \strpos($bundleName, '/')) {
2525
$bundleName = \substr($bundleName, 1);
2626
} else {

Command/DeleteObsoleteCommand.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct(
6161
parent::__construct();
6262
}
6363

64-
protected function configure()
64+
protected function configure(): void
6565
{
6666
$this
6767
->setName(self::$defaultName)
@@ -72,15 +72,18 @@ protected function configure()
7272
;
7373
}
7474

75-
protected function execute(InputInterface $input, OutputInterface $output)
75+
protected function execute(InputInterface $input, OutputInterface $output): void
7676
{
7777
$configName = $input->getArgument('configuration');
7878
$locales = [];
7979
if (null !== $inputLocale = $input->getArgument('locale')) {
8080
$locales = [$inputLocale];
8181
}
8282

83-
$config = $this->configurationManager->getConfiguration($configName);
83+
if (null === $config = $this->configurationManager->getConfiguration($configName)) {
84+
throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $configName));
85+
}
86+
8487
$this->configureBundleDirs($input, $config);
8588
$this->catalogueManager->load($this->catalogueFetcher->getCatalogues($config, $locales));
8689

Command/DownloadCommand.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct(
5252
parent::__construct();
5353
}
5454

55-
protected function configure()
55+
protected function configure(): void
5656
{
5757
$this
5858
->setName(self::$defaultName)
@@ -63,11 +63,14 @@ protected function configure()
6363
;
6464
}
6565

66-
protected function execute(InputInterface $input, OutputInterface $output)
66+
protected function execute(InputInterface $input, OutputInterface $output): void
6767
{
6868
$configName = $input->getArgument('configuration');
6969
$storage = $this->getStorage($configName);
70-
$configuration = $this->configurationManager->getConfiguration($configName);
70+
71+
if (null === $configuration = $this->configurationManager->getConfiguration($configName)) {
72+
throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $configName));
73+
}
7174

7275
$this->configureBundleDirs($input, $configuration);
7376

@@ -87,7 +90,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
8790
return 0;
8891
}
8992

90-
private function hashDirectory($directory)
93+
/**
94+
* @return bool|string
95+
*/
96+
private function hashDirectory(string $directory)
9197
{
9298
if (!\is_dir($directory)) {
9399
return false;

Command/ExtractCommand.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function __construct(
7676
parent::__construct();
7777
}
7878

79-
protected function configure()
79+
protected function configure(): void
8080
{
8181
$this
8282
->setName(self::$defaultName)
@@ -88,9 +88,12 @@ protected function configure()
8888
;
8989
}
9090

91-
protected function execute(InputInterface $input, OutputInterface $output)
91+
protected function execute(InputInterface $input, OutputInterface $output): void
9292
{
93-
$config = $this->configurationManager->getConfiguration($input->getArgument('configuration'));
93+
$configName = $input->getArgument('configuration');
94+
if (null === $config = $this->configurationManager->getConfiguration($configName)) {
95+
throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $configName));
96+
}
9497

9598
$locales = [];
9699
if ($inputLocale = $input->getArgument('locale')) {
@@ -135,10 +138,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
135138
return 0;
136139
}
137140

138-
/**
139-
* @return Finder
140-
*/
141-
private function getConfiguredFinder(Configuration $config)
141+
private function getConfiguredFinder(Configuration $config): Finder
142142
{
143143
$finder = new Finder();
144144
$finder->in($config->getDirs());

Command/StatusCommand.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(
5757
parent::__construct();
5858
}
5959

60-
protected function configure()
60+
protected function configure(): void
6161
{
6262
$this
6363
->setName(self::$defaultName)
@@ -69,9 +69,13 @@ protected function configure()
6969
;
7070
}
7171

72-
protected function execute(InputInterface $input, OutputInterface $output)
72+
protected function execute(InputInterface $input, OutputInterface $output): void
7373
{
74-
$config = $this->configurationManager->getConfiguration($input->getArgument('configuration'));
74+
$configName = $input->getArgument('configuration');
75+
if (null === $config = $this->configurationManager->getConfiguration($configName)) {
76+
throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $configName));
77+
}
78+
7579
$this->configureBundleDirs($input, $config);
7680

7781
$locales = [];

0 commit comments

Comments
 (0)