Skip to content

Commit

Permalink
Merge branch '5.2' into 5.3
Browse files Browse the repository at this point in the history
* 5.2:
  Leverage str_contains/str_starts_with
  Leverage str_ends_with
  • Loading branch information
nicolas-grekas committed Jul 21, 2021
2 parents b634e9d + 46c4a17 commit 90909bd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private function displayTxt(SymfonyStyle $io, array $filesInfo, bool $errorAsGit
$io->text('<error> ERROR </error>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
$io->text(sprintf('<error> >> %s</error>', $info['message']));

if (false !== strpos($info['message'], 'PARSE_CUSTOM_TAGS')) {
if (str_contains($info['message'], 'PARSE_CUSTOM_TAGS')) {
$suggestTagOption = true;
}

Expand Down Expand Up @@ -203,7 +203,7 @@ private function displayJson(SymfonyStyle $io, array $filesInfo): int
++$errors;
}

if (isset($v['message']) && false !== strpos($v['message'], 'PARSE_CUSTOM_TAGS')) {
if (isset($v['message']) && str_contains($v['message'], 'PARSE_CUSTOM_TAGS')) {
$v['message'] .= ' Use the --parse-tags option if you want parse custom tags.';
}
});
Expand Down
4 changes: 2 additions & 2 deletions Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
$output .= "\n";
}

if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r")) {
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && str_contains($value, "\n") && !str_contains($value, "\r")) {
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979
$blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
Expand Down Expand Up @@ -97,7 +97,7 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
if ($value instanceof TaggedValue) {
$output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());

if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r\n")) {
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
Expand Down
2 changes: 1 addition & 1 deletion Exception/ParseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private function updateRepr()
$this->message = $this->rawMessage;

$dot = false;
if ('.' === substr($this->message, -1)) {
if (str_ends_with($this->message, '.')) {
$this->message = substr($this->message, 0, -1);
$dot = true;
}
Expand Down
18 changes: 9 additions & 9 deletions Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static function dump($value, int $flags = 0): string
return 'false';
case \is_int($value):
return $value;
case is_numeric($value) && false === strpos($value, "\f") && false === strpos($value, "\n") && false === strpos($value, "\r") && false === strpos($value, "\t") && false === strpos($value, "\v"):
case is_numeric($value) && false === strpbrk($value, "\f\n\r\t\v"):
$locale = setlocale(\LC_NUMERIC, 0);
if (false !== $locale) {
setlocale(\LC_NUMERIC, 'C');
Expand Down Expand Up @@ -377,7 +377,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
$value = self::parseScalar($sequence, $flags, [',', ']'], $i, null === $tag, $references);

// the value can be an array if a reference has been resolved to an array var
if (\is_string($value) && !$isQuoted && false !== strpos($value, ': ')) {
if (\is_string($value) && !$isQuoted && str_contains($value, ': ')) {
// embedded mapping?
try {
$pos = 0;
Expand Down Expand Up @@ -564,7 +564,7 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
{
$scalar = trim($scalar);

if ('*' === ($scalar[0] ?? '')) {
if (str_starts_with($scalar, '*')) {
if (false !== $pos = strpos($scalar, '#')) {
$value = substr($scalar, 1, $pos - 2);
} else {
Expand Down Expand Up @@ -596,11 +596,11 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
return false;
case '!' === $scalar[0]:
switch (true) {
case 0 === strncmp($scalar, '!!str ', 6):
case str_starts_with($scalar, '!!str '):
return (string) substr($scalar, 6);
case 0 === strncmp($scalar, '! ', 2):
case str_starts_with($scalar, '! '):
return substr($scalar, 2);
case 0 === strncmp($scalar, '!php/object', 11):
case str_starts_with($scalar, '!php/object'):
if (self::$objectSupport) {
if (!isset($scalar[12])) {
trigger_deprecation('symfony/yaml', '5.1', 'Using the !php/object tag without a value is deprecated.');
Expand All @@ -616,7 +616,7 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
}

return null;
case 0 === strncmp($scalar, '!php/const', 10):
case str_starts_with($scalar, '!php/const'):
if (self::$constantSupport) {
if (!isset($scalar[11])) {
trigger_deprecation('symfony/yaml', '5.1', 'Using the !php/const tag without a value is deprecated.');
Expand All @@ -636,9 +636,9 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
}

return null;
case 0 === strncmp($scalar, '!!float ', 8):
case str_starts_with($scalar, '!!float '):
return (float) substr($scalar, 8);
case 0 === strncmp($scalar, '!!binary ', 9):
case str_starts_with($scalar, '!!binary '):
return self::evaluateBinaryScalar(substr($scalar, 9));
default:
throw new ParseException(sprintf('The string "%s" could not be parsed as it uses an unsupported built-in tag.', $scalar), self::$parsedLineNumber, $scalar, self::$parsedFilename);
Expand Down
20 changes: 10 additions & 10 deletions Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private function doParse(string $value, int $flags)
}

// array
if (isset($values['value']) && 0 === strpos(ltrim($values['value'], ' '), '-')) {
if (isset($values['value']) && str_starts_with(ltrim($values['value'], ' '), '-')) {
// Inline first child
$currentLineNumber = $this->getRealCurrentLineNb();

Expand All @@ -182,7 +182,7 @@ private function doParse(string $value, int $flags)
$sequenceYaml .= "\n".$this->getNextEmbedBlock($sequenceIndentation, true);

$data[] = $this->parseBlock($currentLineNumber, rtrim($sequenceYaml), $flags);
} elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
} elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || str_starts_with(ltrim($values['value'], ' '), '#')) {
$data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true) ?? '', $flags);
} elseif (null !== $subTag = $this->getLineTag(ltrim($values['value'], ' '), $flags)) {
$data[] = new TaggedValue(
Expand Down Expand Up @@ -214,7 +214,7 @@ private function doParse(string $value, int $flags)
}
} elseif (
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(( |\t)++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
&& (false === strpos($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"]))
&& (!str_contains($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"]))
) {
if ($context && 'sequence' == $context) {
throw new ParseException('You cannot define a mapping item when in a sequence.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
Expand Down Expand Up @@ -309,7 +309,7 @@ private function doParse(string $value, int $flags)
$subTag = null;
if ($mergeNode) {
// Merge keys
} elseif (!isset($values['value']) || '' === $values['value'] || '#' === ($values['value'][0] ?? '') || (null !== $subTag = $this->getLineTag($values['value'], $flags)) || '<<' === $key) {
} elseif (!isset($values['value']) || '' === $values['value'] || str_starts_with($values['value'], '#') || (null !== $subTag = $this->getLineTag($values['value'], $flags)) || '<<' === $key) {
// hash
// if next line is less indented or equal, then it means that the current value is null
if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
Expand Down Expand Up @@ -457,7 +457,7 @@ private function doParse(string $value, int $flags)
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
}

if (false !== strpos($line, ': ')) {
if (str_contains($line, ': ')) {
throw new ParseException('Mapping values are not allowed in multi-line blocks.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
}

Expand All @@ -467,7 +467,7 @@ private function doParse(string $value, int $flags)
$value .= ' ';
}

if ('' !== $trimmedLine && '\\' === $line[-1]) {
if ('' !== $trimmedLine && str_ends_with($line, '\\')) {
$value .= ltrim(substr($line, 0, -1));
} elseif ('' !== $trimmedLine) {
$value .= $trimmedLine;
Expand All @@ -476,7 +476,7 @@ private function doParse(string $value, int $flags)
if ('' === $trimmedLine) {
$previousLineWasNewline = true;
$previousLineWasTerminatedWithBackslash = false;
} elseif ('\\' === $line[-1]) {
} elseif (str_ends_with($line, '\\')) {
$previousLineWasNewline = false;
$previousLineWasTerminatedWithBackslash = true;
} else {
Expand Down Expand Up @@ -724,7 +724,7 @@ private function moveToPreviousLine(): bool
*/
private function parseValue(string $value, int $flags, string $context)
{
if ('*' === ($value[0] ?? '')) {
if (str_starts_with($value, '*')) {
if (false !== $pos = strpos($value, '#')) {
$value = substr($value, 1, $pos - 2);
} else {
Expand Down Expand Up @@ -811,7 +811,7 @@ private function parseValue(string $value, int $flags, string $context)

$parsedValue = Inline::parse($value, $flags, $this->refs);

if ('mapping' === $context && \is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
if ('mapping' === $context && \is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && str_contains($parsedValue, ': ')) {
throw new ParseException('A colon cannot be used in an unquoted mapping value.', $this->getRealCurrentLineNb() + 1, $value, $this->filename);
}

Expand Down Expand Up @@ -1081,7 +1081,7 @@ private function isNextLineUnIndentedCollection(): bool
*/
private function isStringUnIndentedCollectionItem(): bool
{
return 0 === strncmp($this->currentLine, '- ', 2) || '-' === rtrim($this->currentLine);
return '-' === rtrim($this->currentLine) || str_starts_with($this->currentLine, '- ');
}

/**
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": ">=7.2.5",
"symfony/deprecation-contracts": "^2.1",
"symfony/polyfill-ctype": "~1.8"
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-php80": "^1.16"
},
"require-dev": {
"symfony/console": "^4.4|^5.0"
Expand Down

0 comments on commit 90909bd

Please sign in to comment.