Skip to content

Commit 0a5fcd4

Browse files
authored
minor #1494 [ysm] improve type declarations
1 parent 876c779 commit 0a5fcd4

File tree

1 file changed

+34
-41
lines changed

1 file changed

+34
-41
lines changed

src/Util/YamlSourceManipulator.php

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,16 @@ class YamlSourceManipulator
3434
public const ARRAY_TYPE_SEQUENCE = 'sequence';
3535
public const ARRAY_TYPE_HASH = 'hash';
3636

37-
/**
38-
* @var LoggerInterface|null
39-
*/
40-
private $logger;
37+
private ?LoggerInterface $logger = null;
4138
private $currentData;
4239

43-
private $currentPosition = 0;
44-
private $previousPath = [];
45-
private $currentPath = [];
46-
private $depth = 0;
47-
private $indentationForDepths = [];
48-
private $arrayFormatForDepths = [];
49-
private $arrayTypeForDepths = [];
40+
private int $currentPosition = 0;
41+
private array $previousPath = [];
42+
private array $currentPath = [];
43+
private int $depth = 0;
44+
private array $indentationForDepths = [];
45+
private array $arrayFormatForDepths = [];
46+
private array $arrayTypeForDepths = [];
5047

5148
public function __construct(
5249
private string $contents,
@@ -110,7 +107,7 @@ public function createCommentLine(string $comment): string
110107
return self::COMMENT_PLACEHOLDER_VALUE.$comment;
111108
}
112109

113-
private function updateData(array $newData)
110+
private function updateData(array $newData): void
114111
{
115112
++$this->depth;
116113
if (0 === $this->depth) {
@@ -266,7 +263,7 @@ private function updateData(array $newData)
266263
* The position should be set *right* where this new key
267264
* should be inserted.
268265
*/
269-
private function addNewKeyToYaml($key, $value)
266+
private function addNewKeyToYaml(int|string $key, $value): void
270267
{
271268
$extraOffset = 0;
272269
$firstItemInArray = false;
@@ -384,7 +381,7 @@ private function addNewKeyToYaml($key, $value)
384381
);
385382
}
386383

387-
private function removeKeyFromYaml($key, $currentVal)
384+
private function removeKeyFromYaml($key, $currentVal): void
388385
{
389386
$endKeyPosition = $this->getEndOfKeyPosition($key);
390387

@@ -456,7 +453,7 @@ private function removeKeyFromYaml($key, $currentVal)
456453
*
457454
* @param mixed $value The new value to set into YAML
458455
*/
459-
private function changeValueInYaml(mixed $value)
456+
private function changeValueInYaml(mixed $value): void
460457
{
461458
$originalVal = $this->getCurrentData();
462459

@@ -522,19 +519,19 @@ private function changeValueInYaml(mixed $value)
522519
);
523520
}
524521

525-
private function advanceBeyondKey($key)
522+
private function advanceBeyondKey(int|string $key): void
526523
{
527524
$this->log(sprintf('Advancing position beyond key "%s"', $key));
528525
$this->advanceCurrentPosition($this->getEndOfKeyPosition($key));
529526
}
530527

531-
private function advanceBeyondEndOfPreviousKey($key)
528+
private function advanceBeyondEndOfPreviousKey($key): void
532529
{
533530
$this->log('Advancing position beyond PREV key');
534531
$this->advanceCurrentPosition($this->getEndOfPreviousKeyPosition($key));
535532
}
536533

537-
private function advanceBeyondMultilineArrayLastItem()
534+
private function advanceBeyondMultilineArrayLastItem(): void
538535
{
539536
$this->log('Trying to advance beyond the last item in a multiline array');
540537
$this->advanceBeyondWhitespace();
@@ -553,7 +550,7 @@ private function advanceBeyondMultilineArrayLastItem()
553550
}
554551
}
555552

556-
private function advanceBeyondValue($value)
553+
private function advanceBeyondValue($value): void
557554
{
558555
if (\is_array($value)) {
559556
throw new \LogicException('Do not pass an array to this method');
@@ -563,7 +560,7 @@ private function advanceBeyondValue($value)
563560
$this->advanceCurrentPosition($this->findEndPositionOfValue($value));
564561
}
565562

566-
private function getEndOfKeyPosition($key)
563+
private function getEndOfKeyPosition($key): int
567564
{
568565
preg_match($this->getKeyRegex($key), $this->contents, $matches, \PREG_OFFSET_CAPTURE, $this->currentPosition);
569566

@@ -677,12 +674,12 @@ private function getEndOfPreviousKeyPosition($key): int
677674
return $startOfKey;
678675
}
679676

680-
private function getKeyRegex($key)
677+
private function getKeyRegex($key): string
681678
{
682679
return sprintf('#(?<!\w)\$?%s\'?( )*:#', preg_quote($key));
683680
}
684681

685-
private function updateContents(string $newContents, array $newData, int $newPosition)
682+
private function updateContents(string $newContents, array $newData, int $newPosition): void
686683
{
687684
$this->log('updateContents()');
688685

@@ -706,7 +703,7 @@ private function updateContents(string $newContents, array $newData, int $newPos
706703
$this->currentData = $newData;
707704
}
708705

709-
private function convertToYaml($data)
706+
private function convertToYaml($data): string
710707
{
711708
$indent = $this->depth > 0 && isset($this->indentationForDepths[$this->depth])
712709
? intdiv($this->indentationForDepths[$this->depth], $this->depth)
@@ -727,7 +724,7 @@ private function convertToYaml($data)
727724
* to determine *where* in the array to put the new item (so that it's
728725
* placed in the middle when necessary).
729726
*/
730-
private function appendToArrayAtCurrentPath($key, $value, array $data): array
727+
private function appendToArrayAtCurrentPath(string|int $key, $value, array $data): array
731728
{
732729
if ($this->isPositionAtBeginningOfArray()) {
733730
// this should be prepended
@@ -882,7 +879,7 @@ private function findEndPositionOfValue($value, $offset = null)
882879
throw new YamlManipulationFailedException(sprintf('Unsupported Yaml value of type "%s"', \gettype($value)));
883880
}
884881

885-
private function advanceCurrentPosition(int $newPosition)
882+
private function advanceCurrentPosition(int $newPosition): void
886883
{
887884
$this->log(sprintf('advanceCurrentPosition() from %d to %d', $this->currentPosition, $newPosition), true);
888885
$originalPosition = $this->currentPosition;
@@ -936,7 +933,7 @@ private function advanceCurrentPosition(int $newPosition)
936933
$this->indentationForDepths[$this->depth] = $newIndentation;
937934
}
938935

939-
private function decrementDepth()
936+
private function decrementDepth(): void
940937
{
941938
$this->log('Moving up 1 level of depth');
942939
unset($this->indentationForDepths[$this->depth]);
@@ -954,7 +951,7 @@ private function getCurrentIndentation(?int $override = null): string
954951
return str_repeat(' ', $indent);
955952
}
956953

957-
private function log(string $message, $includeContent = false)
954+
private function log(string $message, bool $includeContent = false): void
958955
{
959956
if (null === $this->logger) {
960957
return;
@@ -1039,7 +1036,7 @@ private function findPositionOfNextCharacter(array $chars)
10391036
}
10401037
}
10411038

1042-
private function advanceBeyondWhitespace()
1039+
private function advanceBeyondWhitespace(): void
10431040
{
10441041
while (' ' === substr($this->contents, $this->currentPosition, 1)) {
10451042
if ($this->isEOF()) {
@@ -1050,7 +1047,7 @@ private function advanceBeyondWhitespace()
10501047
}
10511048
}
10521049

1053-
private function advanceToEndOfLine()
1050+
private function advanceToEndOfLine(): void
10541051
{
10551052
$newPosition = $this->currentPosition;
10561053
while (!$this->isCharLineBreak(substr($this->contents, $newPosition, 1))) {
@@ -1087,10 +1084,10 @@ private function isHash($value): bool
10871084
return false;
10881085
}
10891086

1090-
private function normalizeSequences(array $data)
1087+
private function normalizeSequences(array $data): array
10911088
{
10921089
// https://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential/4254008#4254008
1093-
$hasStringKeys = fn (array $array) => \count(array_filter(array_keys($array), 'is_string')) > 0;
1090+
$hasStringKeys = fn (array $array): bool => \count(array_filter(array_keys($array), 'is_string')) > 0;
10941091

10951092
foreach ($data as $key => $val) {
10961093
if (!\is_array($val)) {
@@ -1110,7 +1107,7 @@ private function normalizeSequences(array $data)
11101107
return $data;
11111108
}
11121109

1113-
private function removeMetadataKeys(array $data)
1110+
private function removeMetadataKeys(array $data): array
11141111
{
11151112
foreach ($data as $key => $val) {
11161113
if (\is_array($val)) {
@@ -1131,7 +1128,7 @@ private function removeMetadataKeys(array $data)
11311128
return $data;
11321129
}
11331130

1134-
private function replaceSpecialMetadataCharacters()
1131+
private function replaceSpecialMetadataCharacters(): void
11351132
{
11361133
while (preg_match('#\n.*'.self::EMPTY_LINE_PLACEHOLDER_VALUE.'.*\n#', $this->contents, $matches)) {
11371134
$this->contents = str_replace($matches[0], "\n\n", $this->contents);
@@ -1167,12 +1164,12 @@ private function isPositionAtBeginningOfArray(): bool
11671164
return null === $this->previousPath[$this->depth];
11681165
}
11691166

1170-
private function manuallyIncrementIndentation()
1167+
private function manuallyIncrementIndentation(): void
11711168
{
11721169
$this->indentationForDepths[$this->depth] += $this->getPreferredIndentationSize();
11731170
}
11741171

1175-
private function isEOF(?int $position = null)
1172+
private function isEOF(?int $position = null): bool
11761173
{
11771174
$position ??= $this->currentPosition;
11781175

@@ -1194,10 +1191,6 @@ private function isCurrentLineComment(int $position): bool
11941191
{
11951192
$line = $this->getCurrentLine($position);
11961193

1197-
if (null === $line) {
1198-
return false;
1199-
}
1200-
12011194
return $this->isLineComment($line);
12021195
}
12031196

@@ -1223,7 +1216,7 @@ private function isFinalLineComment(string $content): bool
12231216
return $this->isLineComment($line);
12241217
}
12251218

1226-
private function getPreviousLine(int $position)
1219+
private function getPreviousLine(int $position): ?string
12271220
{
12281221
// find the previous \n by finding the last one in the content up to the position
12291222
$endPos = strrpos(substr($this->contents, 0, $position), "\n");
@@ -1246,7 +1239,7 @@ private function getPreviousLine(int $position)
12461239
return trim($previousLine, "\r");
12471240
}
12481241

1249-
private function getCurrentLine(int $position)
1242+
private function getCurrentLine(int $position): string
12501243
{
12511244
$startPos = strrpos(substr($this->contents, 0, $position), "\n") + 1;
12521245
$endPos = strpos($this->contents, "\n", $startPos);

0 commit comments

Comments
 (0)