8000 [Yaml] use scalar type hints where possible · symfony/http-kernel@7b1715b · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b1715b

Browse files
committed
[Yaml] use scalar type hints where possible
1 parent 6ce70e4 commit 7b1715b

File tree

7 files changed

+68
-71
lines changed

7 files changed

+68
-71
lines changed

src/Symfony/Component/Yaml/Dumper.php

Expand all lines: src/Symfony/Component/Yaml/Dumper.php
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ class Dumper
2727
*/
2828
protected $indentation;
2929

30-
/**
31-
* @param int $indentation
32-
*/
33-
public function __construct($indentation = 4)
30+
public function __construct(int $indentation = 4)
3431
{
3532
if ($indentation < 1) {
3633
throw new \InvalidArgumentException('The indentation must be greater than zero.');
@@ -49,7 +46,7 @@ public function __construct($indentation = 4)
4946
*
5047
* @return string The YAML representation of the PHP value
5148
*/
52-
public function dump($input, $inline = 0, $indent = 0, $flags = 0)
49+
public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): string
5350
{
5451
$output = '';
5552
$prefix = $indent ? str_repeat(' ', $ind 8000 ent) : '';

src/Symfony/Component/Yaml/Escaper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Escaper
5050
*
5151
* @return bool True if the value would require double quotes
5252
*/
53-
public static function requiresDoubleQuoting($value)
53+
public static function requiresDoubleQuoting(string $value): bool
5454
{
5555
return 0 < preg_match('/'.self::REGEX_CHARACTER_TO_ESCAPE.'/u', $value);
5656
}
@@ -62,7 +62,7 @@ public static function requiresDoubleQuoting($value)
6262
*
6363
* @return string The quoted, escaped string
6464
*/
65-
public static function escapeWithDoubleQuotes($value)
65+
public static function escapeWithDoubleQuotes(string $value): string
6666
{
6767
return sprintf('"%s"', str_replace(self::$escapees, self::$escaped, $value));
6868
}
@@ -74,7 +74,7 @@ public static function escapeWithDoubleQuotes($value)
7474
*
7575
* @return bool True if the value would require single quotes
7676
*/
77-
public static function requiresSingleQuoting($value)
77+
public static function requiresSingleQuoting(string $value): bool
7878
{
7979
// Determines if a PHP value is entirely composed of a value that would
8080
// require single quoting in YAML.
@@ -94,7 +94,7 @@ public static function requiresSingleQuoting($value)
9494
*
9595
* @return string The quoted, escaped string
9696
*/
97-
public static function escapeWithSingleQuotes($value)
97+
public static function escapeWithSingleQuotes(string $value): string
9898
{
9999
return sprintf("'%s'", str_replace('\'', '\'\'', $value));
100100
}

src/Symfony/Component/Yaml/Exception/ParseException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ParseException extends RuntimeException
3232
* @param string|null $parsedFile The file name where the error occurred
3333
* @param \Exception|null $previous The previous exception
3434
*/
35-
public function __construct($message, $parsedLine = -1, $snippet = null, $parsedFile = null, \Exception $previous = null)
35+
public function __construct(string $message, int $parsedLine = -1, string $snippet = null, string $parsedFile = null, \Exception $previous = null)
3636
{
3737
$this->parsedFile = $parsedFile;
3838
$this->parsedLine = $parsedLine;

src/Symfony/Component/Yaml/Inline.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Inline
4444
*
4545
* @throws ParseException
4646
*/
47-
public static function parse($value, $flags = 0, $references = array())
47+
public static function parse(string $value = null, int $flags = 0, array $references = array())
4848
{
4949
self::$exceptionOnInvalidType = (bool) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $flags);
5050
self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
@@ -103,7 +103,7 @@ public static function parse($value, $flags = 0, $references = array())
103103
*
104104
* @throws DumpException When trying to dump PHP resource
105105
*/
106-
public static function dump($value, $flags = 0)
106+
public static function dump($value, int $flags = 0): string
107107
{
108108
switch (true) {
109109
case is_resource($value):
@@ -124,7 +124,13 @@ public static function dump($value, $flags = 0)
124124
}
125125

126126
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
127-
return self::dumpArray($value, $flags & ~Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
127+
$output = array();
128+
129+
foreach ($value as $key => $val) {
130+
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
131+
}
132+
133+
return sprintf('{ %s }', implode(', ', $output));
128134
}
129135

130136
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
@@ -182,13 +188,11 @@ public static function dump($value, $flags = 0)
182188
/**
183189
* Check if given array is hash or just normal indexed array.
184190
*
185-
* @internal
186-
*
187191
* @param array|\ArrayObject|\stdClass $value The PHP array or array-like object to check
188192
*
189193
* @return bool true if value is hash array, false otherwise
190194
*/
191-
public static function isHash($value)
195+
public static function isHash($value): bool
192196
{
193197
if ($value instanceof \stdClass || $value instanceof \ArrayObject) {
194198
return true;
@@ -213,7 +217,7 @@ public static function isHash($value)
213217
*
214218
* @return string The YAML string representing the PHP array
215219
*/
216-
private static function dumpArray($value, $flags)
220+
private static function dumpArray(array $value, int $flags): string
217221
{
218222
// array
219223
if (($value || Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE & $flags) && !self::isHash($value)) {
@@ -244,13 +248,11 @@ private static function dumpArray($value, $flags)
244248
* @param bool $evaluate
245249
* @param array $references
246250
*
247-
* @return string
251+
* @return mixed
248252
*
249253
* @throws ParseException When malformed inline YAML string is parsed
250-
*
251-
* @internal
252254
*/
253-
public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i = 0, $evaluate = true, $references = array())
255+
public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array $references = array())
254256
{
255257
if (in_array($scalar[$i], array('"', "'"))) {
256258
// quoted scalar
@@ -302,7 +304,7 @@ public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i
302304
*
303305
* @throws ParseException When malformed inline YAML string is parsed
304306
*/
305-
private static function parseQuotedScalar($scalar, &$i)
307+
private static function parseQuotedScalar(string $scalar, int &$i): string
306308
{
307309
if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
308310
throw new ParseException(sprintf('Malformed inline YAML string: %s.', substr($scalar, $i)));
@@ -334,7 +336,7 @@ private static function parseQuotedScalar($scalar, &$i)
334336
*
335337
* @throws ParseException When malformed inline YAML string is parsed
336338
*/
337-
private static function parseSequence($sequence, $flags, &$i = 0, $references = array())
339+
private static function parseSequence(string $sequence, int $flags, int &$i = 0, array $references = array()): array
338340
{
339341
$output = array();
340342
$len = strlen($sequence);
@@ -403,7 +405,7 @@ private static function parseSequence($sequence, $flags, &$i = 0, $references =
403405
*
404406
* @throws ParseException When malformed inline YAML string is parsed
405407
*/
406-
private static function parseMapping($mapping, $flags, &$i = 0, $references = array())
408+
private static function parseMapping(string $mapping, int $flags, int &$i = 0, array $references = array())
407409
{
408410
$output = array();
409411
$len = strlen($mapping);
@@ -515,7 +517,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
515517
*
516518
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
517519
*/
518-
private static function evaluateScalar($scalar, $flags, $references = array())
520+
private static function evaluateScalar(string $scalar, int $flags, array $references = array())
519521
{
520522
$scalar = trim($scalar);
521523
$scalarLower = strtolower($scalar);
@@ -638,10 +640,10 @@ private static function evaluateScalar($scalar, $flags, $references = array())
638640
*
639641
* @return null|string
640642
*/
641-
private static function parseTag($value, &$i, $flags)
643+
private static function parseTag(string $value, int &$i, int $flags): ?string
642644
{
643645
if ('!' !== $value[$i]) {
644-
return;
646+
return null;
645647
}
646648

647649
$tagLength = strcspn($value, " \t\n[]{},", $i + 1);
@@ -653,7 +655,7 @@ private static function parseTag($value, &$i, $flags)
653655
// Is followed by a scalar and is a built-in tag
654656
if ($tag && (!isset($value[$nextOffset]) || !in_array($value[$nextOffset], array('[', '{'), true)) && ('!' === $tag[0] || 'str' === $tag || 'php/const' === $tag || 'php/object' === $tag)) {
655657
// Manage in {@link self::evaluateScalar()}
656-
return;
658+
return null;
657659
}
658660

659661
$i = $nextOffset;
@@ -674,10 +676,8 @@ private static function parseTag($value, &$i, $flags)
674676
* @param string $scalar
675677
*
676678
* @return string
677-
*
678-
* @internal
679679
*/
680-
public static function evaluateBinaryScalar($scalar)
680+
public static function evaluateBinaryScalar(string $scalar): string
681681
{
682682
$parsedBinaryData = self::parseScalar(preg_replace('/\s/', '', $scalar));
683683

@@ -692,7 +692,7 @@ public static function evaluateBinaryScalar($scalar)
692692
return base64_decode($parsedBinaryData, true);
693693
}
694694

695-
private static function isBinaryString($value)
695+
private static function isBinaryString(string $value)
696696
{
697697
return !preg_match('//u', $value) || preg_match('/[^\x00\x07-\x0d\x1B\x20-\xff]/', $value);
698698
}
@@ -704,7 +704,7 @@ private static function isBinaryString($value)
704704
*
705705
* @see http://www.yaml.org/spec/1.2/spec.html#id2761573
706706
*/
707-
private static function getTimestampRegex()
707+
private static function getTimestampRegex(): string
708708
{
709709
return <<<EOF
710710
~^
@@ -727,7 +727,7 @@ private static function getTimestampRegex()
727727
*
728728
* @return string
729729
*/
730-
private static function getHexRegex()
730+
private static function getHexRegex(): string
731731
{
732732
return '~^0x[0-9a-f_]++$~i';
733733
}

0 commit comments

Comments
 (0)
0