Skip to content

Commit b2381c0

Browse files
committed
Code: add types, refactoring
1 parent 631029e commit b2381c0

29 files changed

Lines changed: 133 additions & 177 deletions

src/Config/FileConfig.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,23 @@
88
class FileConfig
99
{
1010

11-
/** @var string */
12-
private $file;
11+
private string $file;
1312

14-
/** @var string */
15-
private $distFile;
13+
private string $distFile;
1614

1715
/** @var string|NULL */
18-
private $sourceType;
16+
private ?string $sourceType = null;
1917

2018
/** @var string|NULL */
21-
private $outputType;
19+
private ?string $outputType = null;
2220

2321
/**
24-
* @param mixed[] $config
22+
* @param array{dist-file?: string, file?: string} $config
2523
*/
2624
public function __construct(array $config)
2725
{
2826
// Dist file
29-
if (empty($config['dist-file'])) {
27+
if (!isset($config['dist-file'])) {
3028
throw new InvalidArgumentException(
3129
'The dist-file is required'
3230
);
@@ -43,20 +41,20 @@ public function __construct(array $config)
4341
}
4442

4543
// File
46-
if (!empty($config['file'])) {
44+
if (isset($config['file'])) {
4745
$this->file = $config['file'];
4846
}
4947

50-
if (!$this->file) {
48+
if (!isset($this->file)) {
5149
$this->file = Utils::removeDistExtensions($this->distFile);
5250
}
5351

5452
// Source & output type
55-
if (!$this->sourceType) {
53+
if ($this->sourceType === null) {
5654
$this->sourceType = Utils::detectFileType($this->distFile);
5755
}
5856

59-
if (!$this->outputType) {
57+
if ($this->outputType === null) {
6058
$this->outputType = Utils::detectFileType($this->file);
6159
}
6260
}

src/Decoder/DecoderFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ class DecoderFactory implements IDecoderFactory
88
{
99

1010
/** @var array<string, class-string<IDecoder>> **/
11-
private $decodersMap = [
11+
private array $decodersMap = [
1212
'json' => JsonDecoder::class,
1313
'neon' => NeonDecoder::class,
1414
];
1515

1616
/** @var IDecoder[] */
17-
private $decoders = [];
17+
private array $decoders = [];
1818

1919
public function create(string $type): IDecoder
2020
{
@@ -24,6 +24,7 @@ public function create(string $type): IDecoder
2424

2525
if (isset($this->decodersMap[$type])) {
2626
$this->decoders[$type] = new $this->decodersMap[$type]();
27+
2728
return $this->create($type);
2829
}
2930

src/Decoder/JsonDecoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class JsonDecoder implements IDecoder
1010
*/
1111
public function decode(string $value): array
1212
{
13-
return json_decode($value, true);
13+
return (array) json_decode($value, true);
1414
}
1515

1616
}

src/Decoder/NeonDecoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class NeonDecoder implements IDecoder
1212
*/
1313
public function decode(string $value): array
1414
{
15-
return Neon::decode($value) ?? [];
15+
return (array) Neon::decode($value);
1616
}
1717

1818
}

src/Encoder/EncoderFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ class EncoderFactory implements IEncoderFactory
88
{
99

1010
/** @var array<string, class-string<IEncoder>> **/
11-
private $encodersMap = [
11+
private array $encodersMap = [
1212
'json' => JsonEncoder::class,
1313
'neon' => NeonEncoder::class,
1414
];
1515

1616
/** @var IEncoder[] */
17-
private $encoders = [];
17+
private array $encoders = [];
1818

1919
public function create(string $type): IEncoder
2020
{
@@ -24,6 +24,7 @@ public function create(string $type): IEncoder
2424

2525
if (isset($this->encodersMap[$type])) {
2626
$this->encoders[$type] = new $this->encodersMap[$type]();
27+
2728
return $this->create($type);
2829
}
2930

src/Encoder/JsonEncoder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class JsonEncoder implements IEncoder
1111
public function encode(array $value): ?string
1212
{
1313
$output = json_encode($value, JSON_PRETTY_PRINT);
14+
1415
return ($output !== false) ? $output : null;
1516
}
1617

src/Encoder/NeonEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class NeonEncoder implements IEncoder
1212
*/
1313
public function encode(array $value): ?string
1414
{
15-
return '# ' . IEncoder::GENERATED_MESSAGE . "\n" . ($value !== [] ? Neon::encode($value, Neon::BLOCK) : '');
15+
return '# ' . IEncoder::GENERATED_MESSAGE . "\n" . ($value !== [] ? Neon::encode($value, true) : '');
1616
}
1717

1818
}

src/Exception/Runtime/ValidateException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ class ValidateException extends RuntimeException
88
{
99

1010
/** @var string[] */
11-
public $missingKeys;
11+
public array $missingKeys;
1212

1313
/**
1414
* @param string[] $missingKeys
1515
*/
1616
public function __construct(array $missingKeys)
1717
{
1818
parent::__construct('', 100);
19+
1920
$this->missingKeys = $missingKeys;
2021
}
2122

src/File/FileLoader.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
final class FileLoader
1313
{
1414

15-
/** @var IEncoderFactory */
16-
private $encoderFactory;
15+
private IEncoderFactory $encoderFactory;
1716

18-
/** @var IDecoderFactory */
19-
private $decoderFactory;
17+
private IDecoderFactory $decoderFactory;
2018

2119
public function __construct(
2220
IEncoderFactory $encoderFactory,
@@ -33,7 +31,7 @@ public function __construct(
3331
public function loadFile(string $filename): array
3432
{
3533
$type = Utils::detectFileType($filename);
36-
if (!$type) {
34+
if ($type === null) {
3735
throw new InvalidStateException('Unsupported file type');
3836
}
3937

@@ -54,7 +52,7 @@ public function decodeFile(string $filename, IDecoder $decoder): array
5452
public function saveFile(array $data, string $filename): void
5553
{
5654
$type = Utils::detectFileType($filename);
57-
if (!$type) {
55+
if ($type === null) {
5856
throw new InvalidStateException('Unsupported file type');
5957
}
6058

src/File/FileManager.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
final class FileManager
1212
{
1313

14-
/** @var IEncoderFactory */
15-
private $encoderFactory;
14+
private IEncoderFactory $encoderFactory;
1615

17-
/** @var IDecoderFactory */
18-
private $decoderFactory;
16+
private IDecoderFactory $decoderFactory;
1917

2018
public function __construct(
2119
IEncoderFactory $encoderFactory,
@@ -31,7 +29,7 @@ public function __construct(
3129
*/
3230
public function loadFile(FileConfig $config): array
3331
{
34-
if (empty($config->getOutputType())) {
32+
if ($config->getOutputType() === null) {
3533
throw new InvalidStateException('Invalid file output type');
3634
}
3735

@@ -45,7 +43,7 @@ public function loadFile(FileConfig $config): array
4543
*/
4644
public function loadDistFile(FileConfig $config): array
4745
{
48-
if (empty($config->getSourceType())) {
46+
if ($config->getSourceType() === null) {
4947
throw new InvalidStateException('Invalid file source type');
5048
}
5149

@@ -59,7 +57,7 @@ public function loadDistFile(FileConfig $config): array
5957
*/
6058
public function processFile(array $content, FileConfig $config): void
6159
{
62-
if (empty($config->getOutputType())) {
60+
if ($config->getOutputType() === null) {
6361
throw new InvalidStateException('Invalid file output type');
6462
}
6563

0 commit comments

Comments
 (0)