@@ -34,19 +34,16 @@ class YamlSourceManipulator
34
34
public const ARRAY_TYPE_SEQUENCE = 'sequence ' ;
35
35
public const ARRAY_TYPE_HASH = 'hash ' ;
36
36
37
- /**
38
- * @var LoggerInterface|null
39
- */
40
- private $ logger ;
37
+ private ?LoggerInterface $ logger = null ;
41
38
private $ currentData ;
42
39
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 = [];
50
47
51
48
public function __construct (
52
49
private string $ contents ,
@@ -110,7 +107,7 @@ public function createCommentLine(string $comment): string
110
107
return self ::COMMENT_PLACEHOLDER_VALUE .$ comment ;
111
108
}
112
109
113
- private function updateData (array $ newData )
110
+ private function updateData (array $ newData ): void
114
111
{
115
112
++$ this ->depth ;
116
113
if (0 === $ this ->depth ) {
@@ -266,7 +263,7 @@ private function updateData(array $newData)
266
263
* The position should be set *right* where this new key
267
264
* should be inserted.
268
265
*/
269
- private function addNewKeyToYaml ($ key , $ value )
266
+ private function addNewKeyToYaml (int | string $ key , $ value ): void
270
267
{
271
268
$ extraOffset = 0 ;
272
269
$ firstItemInArray = false ;
@@ -384,7 +381,7 @@ private function addNewKeyToYaml($key, $value)
384
381
);
385
382
}
386
383
387
- private function removeKeyFromYaml ($ key , $ currentVal )
384
+ private function removeKeyFromYaml ($ key , $ currentVal ): void
388
385
{
389
386
$ endKeyPosition = $ this ->getEndOfKeyPosition ($ key );
390
387
@@ -456,7 +453,7 @@ private function removeKeyFromYaml($key, $currentVal)
456
453
*
457
454
* @param mixed $value The new value to set into YAML
458
455
*/
459
- private function changeValueInYaml (mixed $ value )
456
+ private function changeValueInYaml (mixed $ value ): void
460
457
{
461
458
$ originalVal = $ this ->getCurrentData ();
462
459
@@ -522,19 +519,19 @@ private function changeValueInYaml(mixed $value)
522
519
);
523
520
}
524
521
525
- private function advanceBeyondKey ($ key )
522
+ private function advanceBeyondKey (int | string $ key ): void
526
523
{
527
524
$ this ->log (sprintf ('Advancing position beyond key "%s" ' , $ key ));
528
525
$ this ->advanceCurrentPosition ($ this ->getEndOfKeyPosition ($ key ));
529
526
}
530
527
531
- private function advanceBeyondEndOfPreviousKey ($ key )
528
+ private function advanceBeyondEndOfPreviousKey ($ key ): void
532
529
{
533
530
$ this ->log ('Advancing position beyond PREV key ' );
534
531
$ this ->advanceCurrentPosition ($ this ->getEndOfPreviousKeyPosition ($ key ));
535
532
}
536
533
537
- private function advanceBeyondMultilineArrayLastItem ()
534
+ private function advanceBeyondMultilineArrayLastItem (): void
538
535
{
539
536
$ this ->log ('Trying to advance beyond the last item in a multiline array ' );
540
537
$ this ->advanceBeyondWhitespace ();
@@ -553,7 +550,7 @@ private function advanceBeyondMultilineArrayLastItem()
553
550
}
554
551
}
555
552
556
- private function advanceBeyondValue ($ value )
553
+ private function advanceBeyondValue ($ value ): void
557
554
{
558
555
if (\is_array ($ value )) {
559
556
throw new \LogicException ('Do not pass an array to this method ' );
@@ -563,7 +560,7 @@ private function advanceBeyondValue($value)
563
560
$ this ->advanceCurrentPosition ($ this ->findEndPositionOfValue ($ value ));
564
561
}
565
562
566
- private function getEndOfKeyPosition ($ key )
563
+ private function getEndOfKeyPosition ($ key ): int
567
564
{
568
565
preg_match ($ this ->getKeyRegex ($ key ), $ this ->contents , $ matches , \PREG_OFFSET_CAPTURE , $ this ->currentPosition );
569
566
@@ -677,12 +674,12 @@ private function getEndOfPreviousKeyPosition($key): int
677
674
return $ startOfKey ;
678
675
}
679
676
680
- private function getKeyRegex ($ key )
677
+ private function getKeyRegex ($ key ): string
681
678
{
682
679
return sprintf ('#(?<!\w)\$?%s \'?( )*:# ' , preg_quote ($ key ));
683
680
}
684
681
685
- private function updateContents (string $ newContents , array $ newData , int $ newPosition )
682
+ private function updateContents (string $ newContents , array $ newData , int $ newPosition ): void
686
683
{
687
684
$ this ->log ('updateContents() ' );
688
685
@@ -706,7 +703,7 @@ private function updateContents(string $newContents, array $newData, int $newPos
706
703
$ this ->currentData = $ newData ;
707
704
}
708
705
709
- private function convertToYaml ($ data )
706
+ private function convertToYaml ($ data ): string
710
707
{
711
708
$ indent = $ this ->depth > 0 && isset ($ this ->indentationForDepths [$ this ->depth ])
712
709
? intdiv ($ this ->indentationForDepths [$ this ->depth ], $ this ->depth )
@@ -727,7 +724,7 @@ private function convertToYaml($data)
727
724
* to determine *where* in the array to put the new item (so that it's
728
725
* placed in the middle when necessary).
729
726
*/
730
- private function appendToArrayAtCurrentPath ($ key , $ value , array $ data ): array
727
+ private function appendToArrayAtCurrentPath (string | int $ key , $ value , array $ data ): array
731
728
{
732
729
if ($ this ->isPositionAtBeginningOfArray ()) {
733
730
// this should be prepended
@@ -882,7 +879,7 @@ private function findEndPositionOfValue($value, $offset = null)
882
879
throw new YamlManipulationFailedException (sprintf ('Unsupported Yaml value of type "%s" ' , \gettype ($ value )));
883
880
}
884
881
885
- private function advanceCurrentPosition (int $ newPosition )
882
+ private function advanceCurrentPosition (int $ newPosition ): void
886
883
{
887
884
$ this ->log (sprintf ('advanceCurrentPosition() from %d to %d ' , $ this ->currentPosition , $ newPosition ), true );
888
885
$ originalPosition = $ this ->currentPosition ;
@@ -936,7 +933,7 @@ private function advanceCurrentPosition(int $newPosition)
936
933
$ this ->indentationForDepths [$ this ->depth ] = $ newIndentation ;
937
934
}
938
935
939
- private function decrementDepth ()
936
+ private function decrementDepth (): void
940
937
{
941
938
$ this ->log ('Moving up 1 level of depth ' );
942
939
unset($ this ->indentationForDepths [$ this ->depth ]);
@@ -954,7 +951,7 @@ private function getCurrentIndentation(?int $override = null): string
954
951
return str_repeat (' ' , $ indent );
955
952
}
956
953
957
- private function log (string $ message , $ includeContent = false )
954
+ private function log (string $ message , bool $ includeContent = false ): void
958
955
{
959
956
if (null === $ this ->logger ) {
960
957
return ;
@@ -1039,7 +1036,7 @@ private function findPositionOfNextCharacter(array $chars)
1039
1036
}
1040
1037
}
1041
1038
1042
- private function advanceBeyondWhitespace ()
1039
+ private function advanceBeyondWhitespace (): void
1043
1040
{
1044
1041
while (' ' === substr ($ this ->contents , $ this ->currentPosition , 1 )) {
1045
1042
if ($ this ->isEOF ()) {
@@ -1050,7 +1047,7 @@ private function advanceBeyondWhitespace()
1050
1047
}
1051
1048
}
1052
1049
1053
- private function advanceToEndOfLine ()
1050
+ private function advanceToEndOfLine (): void
1054
1051
{
1055
1052
$ newPosition = $ this ->currentPosition ;
1056
1053
while (!$ this ->isCharLineBreak (substr ($ this ->contents , $ newPosition , 1 ))) {
@@ -1087,10 +1084,10 @@ private function isHash($value): bool
1087
1084
return false ;
1088
1085
}
1089
1086
1090
- private function normalizeSequences (array $ data )
1087
+ private function normalizeSequences (array $ data ): array
1091
1088
{
1092
1089
// 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 ;
1094
1091
1095
1092
foreach ($ data as $ key => $ val ) {
1096
1093
if (!\is_array ($ val )) {
@@ -1110,7 +1107,7 @@ private function normalizeSequences(array $data)
1110
1107
return $ data ;
1111
1108
}
1112
1109
1113
- private function removeMetadataKeys (array $ data )
1110
+ private function removeMetadataKeys (array $ data ): array
1114
1111
{
1115
1112
foreach ($ data as $ key => $ val ) {
1116
1113
if (\is_array ($ val )) {
@@ -1131,7 +1128,7 @@ private function removeMetadataKeys(array $data)
1131
1128
return $ data ;
1132
1129
}
1133
1130
1134
- private function replaceSpecialMetadataCharacters ()
1131
+ private function replaceSpecialMetadataCharacters (): void
1135
1132
{
1136
1133
while (preg_match ('#\n.* ' .self ::EMPTY_LINE_PLACEHOLDER_VALUE .'.*\n# ' , $ this ->contents , $ matches )) {
1137
1134
$ this ->contents = str_replace ($ matches [0 ], "\n\n" , $ this ->contents );
@@ -1167,12 +1164,12 @@ private function isPositionAtBeginningOfArray(): bool
1167
1164
return null === $ this ->previousPath [$ this ->depth ];
1168
1165
}
1169
1166
1170
- private function manuallyIncrementIndentation ()
1167
+ private function manuallyIncrementIndentation (): void
1171
1168
{
1172
1169
$ this ->indentationForDepths [$ this ->depth ] += $ this ->getPreferredIndentationSize ();
1173
1170
}
1174
1171
1175
- private function isEOF (?int $ position = null )
1172
+ private function isEOF (?int $ position = null ): bool
1176
1173
{
1177
1174
$ position ??= $ this ->currentPosition ;
1178
1175
@@ -1194,10 +1191,6 @@ private function isCurrentLineComment(int $position): bool
1194
1191
{
1195
1192
$ line = $ this ->getCurrentLine ($ position );
1196
1193
1197
- if (null === $ line ) {
1198
- return false ;
1199
- }
1200
-
1201
1194
return $ this ->isLineComment ($ line );
1202
1195
}
1203
1196
@@ -1223,7 +1216,7 @@ private function isFinalLineComment(string $content): bool
1223
1216
return $ this ->isLineComment ($ line );
1224
1217
}
1225
1218
1226
- private function getPreviousLine (int $ position )
1219
+ private function getPreviousLine (int $ position ): ? string
1227
1220
{
1228
1221
// find the previous \n by finding the last one in the content up to the position
1229
1222
$ endPos = strrpos (substr ($ this ->contents , 0 , $ position ), "\n" );
@@ -1246,7 +1239,7 @@ private function getPreviousLine(int $position)
1246
1239
return trim ($ previousLine , "\r" );
1247
1240
}
1248
1241
1249
- private function getCurrentLine (int $ position )
1242
+ private function getCurrentLine (int $ position ): string
1250
1243
{
1251
1244
$ startPos = strrpos (substr ($ this ->contents , 0 , $ position ), "\n" ) + 1 ;
1252
1245
$ endPos = strpos ($ this ->contents , "\n" , $ startPos );
0 commit comments