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 9e46910

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

File tree

4 files changed

+45
-37
lines changed
Filter options

4 files changed

+45
-37
lines changed

‎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/Inline.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
+35-26Lines changed: 35 additions & 26 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)) {
@@ -247,10 +260,8 @@ private static function dumpArray($value, $flags)
247260
* @return string
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)
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())
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

@@ -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
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Parser.php
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,8 @@ private function doParse($value, $flags)
178178
$context = 'mapping';
179179

180180
// force correct settings
181-
Inline::parse(null, $flags, $this->refs);
181+
Inline::initialize($flags, $this->getRealCurrentLineNb());
182182
try {
183-
Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
184183
$i = 0;
185184
$evaluateKey = !(Yaml::PARSE_KEYS_AS_STRINGS & $flags);
186185

@@ -942,7 +941,7 @@ private function isBlockScalarHeader()
942941
*
943942
* @internal
944943
*/
945-
public static function preg_match($pattern, $subject, &$matches = null, $flags = 0, $offset = 0)
944+
public static function preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int
946945
{
947946
if (false === $ret = preg_match($pattern, $subject, $matches, $flags, $offset)) {
948947
switch (preg_last_error()) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Unescaper.php
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Unescaper
3535
*
3636
* @return string The unescaped string
3737
*/
38-
public function unescapeSingleQuotedString($value)
38+
public function unescapeSingleQuotedString(string $value): string
3939
{
4040
return str_replace('\'\'', '\'', $value);
4141
}
@@ -47,7 +47,7 @@ public function unescapeSingleQuotedString($value)
4747
*
4848
* @return string The unescaped string
4949
*/
50-
public function unescapeDoubleQuotedString($value)
50+
public function unescapeDoubleQuotedString(string $value): string
5151
{
5252
$callback = function ($match) {
5353
return $this->unescapeCharacter($match[0]);
@@ -64,7 +64,7 @@ public function unescapeDoubleQuotedString($value)
6464
*
6565
* @return string The unescaped character
6666
*/
67-
private function unescapeCharacter($value)
67+
private function unescapeCharacter(string $value): string
6868
{
6969
switch ($value[1]) {
7070
case '0':
@@ -125,7 +125,7 @@ private function unescapeCharacter($value)
125125
*
126126
* @return string The corresponding UTF-8 character
127127
*/
128-
private static function utf8chr($c)
128+
private static function utf8chr(int $c): string
129129
{
130130
if (0x80 > $c %= 0x200000) {
131131
return chr($c);

0 commit comments

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