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

Add scalar typehints/return types #23262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 3, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[Yaml] use scalar type hints where possible
  • Loading branch information
xabbuh committed Sep 1, 2017
commit 7b1715b0782af05d5114cb5ad4433ac7e7e45e72
7 changes: 2 additions & 5 deletions 7 src/Symfony/Component/Yaml/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ class Dumper
*/
protected $indentation;

/**
* @param int $indentation
*/
public function __construct($indentation = 4)
public function __construct(int $indentation = 4)
{
if ($indentation < 1) {
throw new \InvalidArgumentException('The indentation must be greater than zero.');
Expand All @@ -49,7 +46,7 @@ public function __construct($indentation = 4)
*
* @return string The YAML representation of the PHP value
*/
public function dump($input, $inline = 0, $indent = 0, $flags = 0)
public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): string
{
$output = '';
$prefix = $indent ? str_repeat(' ', $indent) : '';
Expand Down
8 changes: 4 additions & 4 deletions 8 src/Symfony/Component/Yaml/Escaper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Escaper
*
* @return bool True if the value would require double quotes
*/
public static function requiresDoubleQuoting($value)
public static function requiresDoubleQuoting(string $value): bool
{
return 0 < preg_match('/'.self::REGEX_CHARACTER_TO_ESCAPE.'/u', $value);
}
Expand All @@ -62,7 +62,7 @@ public static function requiresDoubleQuoting($value)
*
* @return string The quoted, escaped string
*/
public static function escapeWithDoubleQuotes($value)
public static function escapeWithDoubleQuotes(string $value): string
{
return sprintf('"%s"', str_replace(self::$escapees, self::$escaped, $value));
}
Expand All @@ -74,7 +74,7 @@ public static function escapeWithDoubleQuotes($value)
*
* @return bool True if the value would require single quotes
*/
public static function requiresSingleQuoting($value)
public static function requiresSingleQuoting(string $value): bool
{
// Determines if a PHP value is entirely composed of a value that would
// require single quoting in YAML.
Expand All @@ -94,7 +94,7 @@ public static function requiresSingleQuoting($value)
*
* @return string The quoted, escaped string
*/
public static function escapeWithSingleQuotes($value)
public static function escapeWithSingleQuotes(string $value): string
{
return sprintf("'%s'", str_replace('\'', '\'\'', $value));
}
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/Yaml/Exception/ParseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ParseException extends RuntimeException
* @param string|null $parsedFile The file name where the error occurred
* @param \Exception|null $previous The previous exception
*/
public function __construct($message, $parsedLine = -1, $snippet = null, $parsedFile = null, \Exception $previous = null)
public function __construct(string $message, int $parsedLine = -1, string $snippet = null, string $parsedFile = null, \Exception $previous = null)
{
$this->parsedFile = $parsedFile;
$this->parsedLine = $parsedLine;
Expand Down
48 changes: 24 additions & 24 deletions 48 src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Inline
*
* @throws ParseException
*/
public static function parse($value, $flags = 0, $references = array())
public static function parse(string $value = null, int $flags = 0, array $references = array())
{
self::$exceptionOnInvalidType = (bool) (Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE & $flags);
self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
Expand Down Expand Up @@ -103,7 +103,7 @@ public static function parse($value, $flags = 0, $references = array())
*
* @throws DumpException When trying to dump PHP resource
*/
public static function dump($value, $flags = 0)
public static function dump($value, int $flags = 0): string
{
switch (true) {
case is_resource($value):
Expand All @@ -124,7 +124,13 @@ public static function dump($value, $flags = 0)
}

if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
return self::dumpArray($value, $flags & ~Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
$output = array();

foreach ($value as $key => $val) {
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
}

return sprintf('{ %s }', implode(', ', $output));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code change intended in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @xabbuh

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this part is handling the dumping of objects as YAML mappings and thus allows us to type hint the $value argument in dumpArray() with array.

}

if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
Expand Down Expand Up @@ -182,13 +188,11 @@ public static function dump($value, $flags = 0)
/**
* Check if given array is hash or just normal indexed array.
*
* @internal
*
* @param array|\ArrayObject|\stdClass $value The PHP array or array-like object to check
*
* @return bool true if value is hash array, false otherwise
*/
public static function isHash($value)
public static function isHash($value): bool
{
if ($value instanceof \stdClass || $value instanceof \ArrayObject) {
return true;
Expand All @@ -213,7 +217,7 @@ public static function isHash($value)
*
* @return string The YAML string representing the PHP array
*/
private static function dumpArray($value, $flags)
private static function dumpArray(array $value, int $flags): string
{
// array
if (($value || Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE & $flags) && !self::isHash($value)) {
Expand Down Expand Up @@ -244,13 +248,11 @@ private static function dumpArray($value, $flags)
* @param bool $evaluate
* @param array $references
*
* @return string
* @return mixed
*
* @throws ParseException When malformed inline YAML string is parsed
*
* @internal
*/
public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i = 0, $evaluate = true, $references = array())
public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array $references = array())
{
if (in_array($scalar[$i], array('"', "'"))) {
// quoted scalar
Expand Down Expand Up @@ -302,7 +304,7 @@ public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i
*
* @throws ParseException When malformed inline YAML string is parsed
*/
private static function parseQuotedScalar($scalar, &$i)
private static function parseQuotedScalar(string $scalar, int &$i): string
{
if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
throw new ParseException(sprintf('Malformed inline YAML string: %s.', substr($scalar, $i)));
Expand Down Expand Up @@ -334,7 +336,7 @@ private static function parseQuotedScalar($scalar, &$i)
*
* @throws ParseException When malformed inline YAML string is parsed
*/
private static function parseSequence($sequence, $flags, &$i = 0, $references = array())
private static function parseSequence(string $sequence, int $flags, int &$i = 0, array $references = array()): array
{
$output = array();
$len = strlen($sequence);
Expand Down Expand Up @@ -403,7 +405,7 @@ private static function parseSequence($sequence, $flags, &$i = 0, $references =
*
* @throws ParseException When malformed inline YAML string is parsed
*/
private static function parseMapping($mapping, $flags, &$i = 0, $references = array())
private static function parseMapping(string $mapping, int $flags, int &$i = 0, array $references = array())
{
$output = array();
$len = strlen($mapping);
Expand Down Expand Up @@ -515,7 +517,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
*
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
*/
private static function evaluateScalar($scalar, $flags, $references = array())
private static function evaluateScalar(string $scalar, int $flags, array $references = array())
{
$scalar = trim($scalar);
$scalarLower = strtolower($scalar);
Expand Down Expand Up @@ -638,10 +640,10 @@ private static function evaluateScalar($scalar, $flags, $references = array())
*
* @return null|string
*/
private static function parseTag($value, &$i, $flags)
private static function parseTag(string $value, int &$i, int $flags): ?string
{
if ('!' !== $value[$i]) {
return;
return null;
}

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

$i = $nextOffset;
Expand All @@ -674,10 +676,8 @@ private static function parseTag($value, &$i, $flags)
* @param string $scalar
*
* @return string
*
* @internal
*/
public static function evaluateBinaryScalar($scalar)
public static function evaluateBinaryScalar(string $scalar): string
{
$parsedBinaryData = self::parseScalar(preg_replace('/\s/', '', $scalar));

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

private static function isBinaryString($value)
private static function isBinaryString(string $value)
{
return !preg_match('//u', $value) || preg_match('/[^\x00\x07-\x0d\x1B\x20-\xff]/', $value);
}
Expand All @@ -704,7 +704,7 @@ private static function isBinaryString($value)
*
* @see http://www.yaml.org/spec/1.2/spec.html#id2761573
*/
private static function getTimestampRegex()
private static function getTimestampRegex(): string
{
return <<<EOF
~^
Expand All @@ -727,7 +727,7 @@ private static function getTimestampRegex()
*
* @return string
*/
private static function getHexRegex()
private static function getHexRegex(): string
{
return '~^0x[0-9a-f_]++$~i';
}
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.