Skip to content

Commit 2b5f2ae

Browse files
derrabusnicolas-grekas
authored andcommitted
Leverage str_contains/str_starts_with
Signed-off-by: Alexander M. Turek <[email protected]>
1 parent 54262b5 commit 2b5f2ae

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

Command/LintCommand.php

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

166-
if (false !== strpos($info['message'], 'PARSE_CUSTOM_TAGS')) {
166+
if (str_contains($info['message'], 'PARSE_CUSTOM_TAGS')) {
167167
$suggestTagOption = true;
168168
}
169169
}
@@ -188,7 +188,7 @@ private function displayJson(SymfonyStyle $io, array $filesInfo): int
188188
++$errors;
189189
}
190190

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

Dumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
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($input, int $inline = 0, int $indent = 0, int $flags = 0):
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 : '';

Inline.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public static function dump($value, int $flags = 0): string
163163
return 'false';
164164
case \is_int($value):
165165
return $value;
166-
case is_numeric($value) && false === strpos($value, "\f") && false === strpos($value, "\n") && false === strpos($value, "\r") && false === strpos($value, "\t") && false === strpos($value, "\v"):
166+
case is_numeric($value) && false === strpbrk($value, "\f\n\r\t\v"):
167167
$locale = setlocale(\LC_NUMERIC, 0);
168168
if (false !== $locale) {
169169
setlocale(\LC_NUMERIC, 'C');
@@ -377,7 +377,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
377377
$value = self::parseScalar($sequence, $flags, [',', ']'], $i, null === $tag, $references);
378378

379379
// the value can be an array if a reference has been resolved to an array var
380-
if (\is_string($value) && !$isQuoted && false !== strpos($value, ': ')) {
380+
if (\is_string($value) && !$isQuoted && str_contains($value, ': ')) {
381381
// embedded mapping?
382382
try {
383383
$pos = 0;
@@ -565,7 +565,7 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
565565
$scalar = trim($scalar);
566566
$scalarLower = strtolower($scalar);
567567

568-
if (0 === strpos($scalar, '*')) {
568+
if (str_starts_with($scalar, '*')) {
569569
if (false !== $pos = strpos($scalar, '#')) {
570570
$value = substr($scalar, 1, $pos - 2);
571571
} else {
@@ -595,11 +595,11 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
595595
return false;
596596
case '!' === $scalar[0]:
597597
switch (true) {
598-
case 0 === strpos($scalar, '!!str '):
598+
case str_starts_with($scalar, '!!str '):
599599
return (string) substr($scalar, 6);
600-
case 0 === strpos($scalar, '! '):
600+
case str_starts_with($scalar, '! '):
601601
return substr($scalar, 2);
602-
case 0 === strpos($scalar, '!php/object'):
602+
case str_starts_with($scalar, '!php/object'):
603603
if (self::$objectSupport) {
604604
if (!isset($scalar[12])) {
605605
return false;
@@ -613,7 +613,7 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
613613
}
614614

615615
return null;
616-
case 0 === strpos($scalar, '!php/const'):
616+
case str_starts_with($scalar, '!php/const'):
617617
if (self::$constantSupport) {
618618
if (!isset($scalar[11])) {
619619
return '';
@@ -631,9 +631,9 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
631631
}
632632

633633
return null;
634-
case 0 === strpos($scalar, '!!float '):
634+
case str_starts_with($scalar, '!!float '):
635635
return (float) substr($scalar, 8);
636-
case 0 === strpos($scalar, '!!binary '):
636+
case str_starts_with($scalar, '!!binary '):
637637
return self::evaluateBinaryScalar(substr($scalar, 9));
638638
default:
639639
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private function doParse(string $value, int $flags)
170170
}
171171

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

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

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

456-
if (false !== strpos($line, ': ')) {
456+
if (str_contains($line, ': ')) {
457457
@trigger_error('Support for mapping keys in multi-line blocks is deprecated since Symfony 4.3 and will throw a ParseException in 5.0.', \E_USER_DEPRECATED);
458458
}
459459

@@ -716,7 +716,7 @@ private function moveToPreviousLine(): bool
716716
*/
717717
private function parseValue(string $value, int $flags, string $context)
718718
{
719-
if (0 === strpos($value, '*')) {
719+
if (str_starts_with($value, '*')) {
720720
if (false !== $pos = strpos($value, '#')) {
721721
$value = substr($value, 1, $pos - 2);
722722
} else {
@@ -803,7 +803,7 @@ private function parseValue(string $value, int $flags, string $context)
803803

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

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

@@ -1073,7 +1073,7 @@ private function isNextLineUnIndentedCollection(): bool
10731073
*/
10741074
private function isStringUnIndentedCollectionItem(): bool
10751075
{
1076-
return '-' === rtrim($this->currentLine) || 0 === strpos($this->currentLine, '- ');
1076+
return '-' === rtrim($this->currentLine) || str_starts_with($this->currentLine, '- ');
10771077
}
10781078

10791079
/**

0 commit comments

Comments
 (0)