Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e02137b

Browse filesBrowse files
xabbuhRobin Chalas
authored and
Robin Chalas
committed
[Yaml] use scalar type hints where possible
1 parent 6ce70e4 commit e02137b
Copy full SHA for e02137b

File tree

7 files changed

+82
-77
lines changed
Filter options

7 files changed

+82
-77
lines changed

‎src/Symfony/Component/Yaml/Dumper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Dumper.php
+2-5Lines 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(' ', $indent) : '';

‎src/Symfony/Component/Yaml/Escaper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Escaper.php
+4-4Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Exception/ParseException.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
+37-28Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ class Inline
3333
private static $objectForMap = false;
3434
private static $constantSupport = false;
3535

36+
public static function initialize(int $flags, int $parsedLineNumber = null)
37+
{
38+
self::$exceptionOnInvalidType = (bool) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $flags);
39+
self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
40+
self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags);
41+
self::$constantSupport = (bool) (Yaml::PARSE_CONSTANT & $flags);
42+
43+
if (null !== $parsedLineNumber) {
44+
self::$parsedLineNumber = $parsedLineNumber;
45+
}
46+
}
47+
3648
/**
3749
* Converts a YAML string to a PHP value.
3850
*
@@ -44,12 +56,9 @@ class Inline
4456
*
4557
* @throws ParseException
4658
*/
47-
public static function parse($value, $flags = 0, $references = array())
59+
public static function parse(string $value = null, int $flags = 0, array $references = array())
4860
{
49-
self::$exceptionOnInvalidType = (bool) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $flags);
50-
self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
51-
self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags);
52-
self::$constantSupport = (bool) (Yaml::PARSE_CONSTANT & $flags);
61+
self::initialize($flags);
5362

5463
$value = trim($value);
5564

@@ -103,7 +112,7 @@ public static function parse($value, $flags = 0, $references = array())
103112
*
104113
* @throws DumpException When trying to dump PHP resource
105114
*/
106-
public static function dump($value, $flags = 0)
115+
public static function dump($value, int $flags = 0): string
107116
{
108117
switch (true) {
109118
case is_resource($value):
@@ -124,7 +133,13 @@ public static function dump($value, $flags = 0)
124133
}
125134

126135
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);
136+
$output = array();
137+
138+
foreach ($value as $key => $val) {
139+
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
140+
}
141+
142+
return sprintf('{ %s }', implode(', ', $output));
128143
}
129144

130145
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
@@ -182,13 +197,11 @@ public static function dump($value, $flags = 0)
182197
/**
183198
* Check if given array is hash or just normal indexed array.
184199
*
185-
* @internal
186-
*
187200
* @param array|\ArrayObject|\stdClass $value The PHP array or array-like object to check
188201
*
189202
* @return bool true if value is hash array, false otherwise
190203
*/
191-
public static function isHash($value)
204+
public static function isHash($value): bool
192205
{
193206
if ($value instanceof \stdClass || $value instanceof \ArrayObject) {
194207
return true;
@@ -213,7 +226,7 @@ public static function isHash($value)
213226
*
214227
* @return string The YAML string representing the PHP array
215228
*/
216-
private static function dumpArray($value, $flags)
229+
private static function dumpArray(array $value, int $flags): string
217230
{
218231
// array
219232
if (($value || Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE & $flags) && !self::isHash($value)) {
@@ -244,13 +257,11 @@ private static function dumpArray($value, $flags)
244257
* @param bool $evaluate
245258
* @param array $references
246259
*
247-
* @return string
260+
* @return mixed
248261
*
249262
* @throws ParseException When malformed inline YAML string is parsed
250-
*
251-
* @internal
252263
*/
253-
public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i = 0, $evaluate = true, $references = array())
264+
public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array $references = array())
254265
{
255266
if (in_array($scalar[$i], array('"', "'"))) {
256267
// quoted scalar
@@ -302,7 +313,7 @@ public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i
302313
*
303314
* @throws ParseException When malformed inline YAML string is parsed
304315
*/
305-
private static function parseQuotedScalar($scalar, &$i)
316+
private static function parseQuotedScalar(string $scalar, int &$i): string
306317
{
307318
if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
308319
throw new ParseException(sprintf('Malformed inline YAML string: %s.', substr($scalar, $i)));
@@ -334,7 +345,7 @@ private static function parseQuotedScalar($scalar, &$i)
334345
*
335346
* @throws ParseException When malformed inline YAML string is parsed
336347
*/
337-
private static function parseSequence($sequence, $flags, &$i = 0, $references = array())
348+
private static function parseSequence(string $sequence, int $flags, int &$i = 0, array $references = array()): array
338349
{
339350
$output = array();
340351
$len = strlen($sequence);
@@ -403,7 +414,7 @@ private static function parseSequence($sequence, $flags, &$i = 0, $references =
403414
*
404415
* @throws ParseException When malformed inline YAML string is parsed
405416
*/
406-
private static function parseMapping($mapping, $flags, &$i = 0, $references = array())
417+
private static function parseMapping(string $mapping, int $flags, int &$i = 0, array $references = array())
407418
{
408419
$output = array();
409420
$len = strlen($mapping);
@@ -515,7 +526,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
515526
*
516527
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
517528
*/
518-
private static function evaluateScalar($scalar, $flags, $references = array())
529+
private static function evaluateScalar(string $scalar, int $flags, array $references = array())
519530
{
520531
$scalar = trim($scalar);
521532
$scalarLower = strtolower($scalar);
@@ -638,10 +649,10 @@ private static function evaluateScalar($scalar, $flags, $references = array())
638649
*
639650
* @return null|string
640651
*/
641-
private static function parseTag($value, &$i, $flags)
652+
private static function parseTag(string $value, int &$i, int $flags): ?string
642653
{
643654
if ('!' !== $value[$i]) {
644-
return;
655+
return null;
645656
}
646657

647658
$tagLength = strcspn($value, " \t\n[]{},", $i + 1);
@@ -653,7 +664,7 @@ private static function parseTag($value, &$i, $flags)
653664
// Is followed by a scalar and is a built-in tag
654665
if ($tag && (!isset($value[$nextOffset]) || !in_array($value[$nextOffset], array('[', '{'), true)) && ('!' === $tag[0] || 'str' === $tag || 'php/const' === $tag || 'php/object' === $tag)) {
655666
// Manage in {@link self::evaluateScalar()}
656-
return;
667+
return null;
657668
}
658669

659670
$i = $nextOffset;
@@ -674,10 +685,8 @@ private static function parseTag($value, &$i, $flags)
674685
* @param string $scalar
675686
*
676687
* @return string
677-
*
678-
* @internal
679688
*/
680-
public static function evaluateBinaryScalar($scalar)
689+
public static function evaluateBinaryScalar(string $scalar): string
681690
{
682691
$parsedBinaryData = self::parseScalar(preg_replace('/\s/', '', $scalar));
683692

@@ -692,7 +701,7 @@ public static function evaluateBinaryScalar($scalar)
692701
return base64_decode($parsedBinaryData, true);
693702
}
694703

695-
private static function isBinaryString($value)
704+
private static function isBinaryString(string $value)
696705
{
697706
return !preg_match('//u', $value) || preg_match('/[^\x00\x07-\x0d\x1B\x20-\xff]/', $value);
698707
}
@@ -704,7 +713,7 @@ private static function isBinaryString($value)
704713
*
705714
* @see http://www.yaml.org/spec/1.2/spec.html#id2761573
706715
*/
707-
private static function getTimestampRegex()
716+
private static function getTimestampRegex(): string
708717
{
709718
return <<<EOF
710719
~^
@@ -727,7 +736,7 @@ private static function getTimestampRegex()
727736
*
728737
* @return string
729738
*/
730-
private static function getHexRegex()
739+
private static function getHexRegex(): string
731740
{
732741
return '~^0x[0-9a-f_]++$~i';
733742
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.