Skip to content

Commit e850002

Browse files
Merge branch '5.4' into 6.0
* 5.4: Leverage str_contains/str_starts_with Leverage str_ends_with
2 parents 0293364 + 1eb028d commit e850002

File tree

6 files changed

+25
-24
lines changed

6 files changed

+25
-24
lines changed

Command/LintCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private function displayTxt(SymfonyStyle $io, array $filesInfo, bool $errorAsGit
174174
$io->text('<error> ERROR </error>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
175175
$io->text(sprintf('<error> >> %s</error>', $info['message']));
176176

177-
if (false !== strpos($info['message'], 'PARSE_CUSTOM_TAGS')) {
177+
if (str_contains($info['message'], 'PARSE_CUSTOM_TAGS')) {
178178
$suggestTagOption = true;
179179
}
180180

@@ -203,7 +203,7 @@ private function displayJson(SymfonyStyle $io, array $filesInfo): int
203203
++$errors;
204204
}
205205

206-
if (isset($v['message']) && false !== strpos($v['message'], 'PARSE_CUSTOM_TAGS')) {
206+
if (isset($v['message']) && str_contains($v['message'], 'PARSE_CUSTOM_TAGS')) {
207207
$v['message'] .= ' Use the --parse-tags option if you want parse custom tags.';
208208
}
209209
});

Dumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
6868
$output .= "\n";
6969
}
7070

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

100-
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r\n")) {
100+
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
101101
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
102102
// http://www.yaml.org/spec/1.2/spec.html#id2793979
103103
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';

Exception/ParseException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private function updateRepr()
108108
$this->message = $this->rawMessage;
109109

110110
$dot = false;
111-
if ('.' === substr($this->message, -1)) {
111+
if (str_ends_with($this->message, '.')) {
112112
$this->message = substr($this->message, 0, -1);
113113
$dot = true;
114114
}

Inline.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public static function dump(mixed $value, int $flags = 0): string
148148
return 'false';
149149
case \is_int($value):
150150
return $value;
151-
case is_numeric($value) && false === strpos($value, "\f") && false === strpos($value, "\n") && false === strpos($value, "\r") && false === strpos($value, "\t") && false === strpos($value, "\v"):
151+
case is_numeric($value) && false === strpbrk($value, "\f\n\r\t\v"):
152152
$locale = setlocale(\LC_NUMERIC, 0);
153153
if (false !== $locale) {
154154
setlocale(\LC_NUMERIC, 'C');
@@ -342,7 +342,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
342342
$value = self::parseScalar($sequence, $flags, [',', ']'], $i, null === $tag, $references);
343343

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

528-
if ('*' === ($scalar[0] ?? '')) {
528+
if (str_starts_with($scalar, '*')) {
529529
if (false !== $pos = strpos($scalar, '#')) {
530530
$value = substr($scalar, 1, $pos - 2);
531531
} else {
@@ -557,11 +557,11 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
557557
return false;
558558
case '!' === $scalar[0]:
559559
switch (true) {
560-
case 0 === strncmp($scalar, '!!str ', 6):
560+
case str_starts_with($scalar, '!!str '):
561561
return (string) substr($scalar, 6);
562-
case 0 === strncmp($scalar, '! ', 2):
562+
case str_starts_with($scalar, '! '):
563563
return substr($scalar, 2);
564-
case 0 === strncmp($scalar, '!php/object', 11):
564+
case str_starts_with($scalar, '!php/object'):
565565
if (self::$objectSupport) {
566566
if (!isset($scalar[12])) {
567567
throw new ParseException('Missing value for tag "!php/object".', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
@@ -575,7 +575,7 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
575575
}
576576

577577
return null;
578-
case 0 === strncmp($scalar, '!php/const', 10):
578+
case str_starts_with($scalar, '!php/const'):
579579
if (self::$constantSupport) {
580580
if (!isset($scalar[11])) {
581581
throw new ParseException('Missing value for tag "!php/const".', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
@@ -593,9 +593,9 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
593593
}
594594

595595
return null;
596-
case 0 === strncmp($scalar, '!!float ', 8):
596+
case str_starts_with($scalar, '!!float '):
597597
return (float) substr($scalar, 8);
598-
case 0 === strncmp($scalar, '!!binary ', 9):
598+
case str_starts_with($scalar, '!!binary '):
599599
return self::evaluateBinaryScalar(substr($scalar, 9));
600600
default:
601601
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);

Parser.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private function doParse(string $value, int $flags)
159159
}
160160

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

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

170170
$data[] = $this->parseBlock($currentLineNumber, rtrim($sequenceYaml), $flags);
171-
} elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
171+
} elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || str_starts_with(ltrim($values['value'], ' '), '#')) {
172172
$data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true) ?? '', $flags);
173173
} elseif (null !== $subTag = $this->getLineTag(ltrim($values['value'], ' '), $flags)) {
174174
$data[] = new TaggedValue(
@@ -200,7 +200,7 @@ private function doParse(string $value, int $flags)
200200
}
201201
} elseif (
202202
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(( |\t)++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
203-
&& (false === strpos($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"]))
203+
&& (!str_contains($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"]))
204204
) {
205205
if ($context && 'sequence' == $context) {
206206
throw new ParseException('You cannot define a mapping item when in a sequence.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
@@ -295,7 +295,7 @@ private function doParse(string $value, int $flags)
295295
$subTag = null;
296296
if ($mergeNode) {
297297
// Merge keys
298-
} elseif (!isset($values['value']) || '' === $values['value'] || '#' === ($values['value'][0] ?? '') || (null !== $subTag = $this->getLineTag($values['value'], $flags)) || '<<' === $key) {
298+
} elseif (!isset($values['value']) || '' === $values['value'] || str_starts_with($values['value'], '#') || (null !== $subTag = $this->getLineTag($values['value'], $flags)) || '<<' === $key) {
299299
// hash
300300
// if next line is less indented or equal, then it means that the current value is null
301301
if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
@@ -443,7 +443,7 @@ private function doParse(string $value, int $flags)
443443
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename);
444444
}
445445

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

@@ -453,7 +453,7 @@ private function doParse(string $value, int $flags)
453453
$value .= ' ';
454454
}
455455

456-
if ('' !== $trimmedLine && '\\' === $line[-1]) {
456+
if ('' !== $trimmedLine && str_ends_with($line, '\\')) {
457457
$value .= ltrim(substr($line, 0, -1));
458458
} elseif ('' !== $trimmedLine) {
459459
$value .= $trimmedLine;
@@ -462,7 +462,7 @@ private function doParse(string $value, int $flags)
462462
if ('' === $trimmedLine) {
463463
$previousLineWasNewline = true;
464464
$previousLineWasTerminatedWithBackslash = false;
465-
} elseif ('\\' === $line[-1]) {
465+
} elseif (str_ends_with($line, '\\')) {
466466
$previousLineWasNewline = false;
467467
$previousLineWasTerminatedWithBackslash = true;
468468
} else {
@@ -699,7 +699,7 @@ private function moveToPreviousLine(): bool
699699
*/
700700
private function parseValue(string $value, int $flags, string $context): mixed
701701
{
702-
if ('*' === ($value[0] ?? '')) {
702+
if (str_starts_with($value, '*')) {
703703
if (false !== $pos = strpos($value, '#')) {
704704
$value = substr($value, 1, $pos - 2);
705705
} else {
@@ -786,7 +786,7 @@ private function parseValue(string $value, int $flags, string $context): mixed
786786

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

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

@@ -1022,7 +1022,7 @@ private function isNextLineUnIndentedCollection(): bool
10221022

10231023
private function isStringUnIndentedCollectionItem(): bool
10241024
{
1025-
return 0 === strncmp($this->currentLine, '- ', 2) || '-' === rtrim($this->currentLine);
1025+
return '-' === rtrim($this->currentLine) || str_starts_with($this->currentLine, '- ');
10261026
}
10271027

10281028
/**

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"php": ">=8.0.2",
2020
"symfony/deprecation-contracts": "^2.1|^3.0",
2121
"symfony/polyfill-ctype": "^1.8",
22+
"symfony/polyfill-php80": "^1.16",
2223
"symfony/polyfill-php81": "^1.22"
2324
},
2425
"require-dev": {

0 commit comments

Comments
 (0)