Skip to content

Commit 70c973f

Browse files
committed
Leverage str_starts_with(), str_ends_with() and str_contains()
1 parent f3064a2 commit 70c973f

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

Command/LintCommand.php

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

187-
if (false !== strpos($info['message'], 'PARSE_CUSTOM_TAGS')) {
187+
if (str_contains($info['message'], 'PARSE_CUSTOM_TAGS')) {
188188
$suggestTagOption = true;
189189
}
190190

@@ -213,7 +213,7 @@ private function displayJson(SymfonyStyle $io, array $filesInfo): int
213213
++$errors;
214214
}
215215

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

Dumper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
6464
$output .= "\n";
6565
}
6666

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

7272
if (isset($value[-2]) && "\n" === $value[-2] && "\n" === $value[-1]) {
7373
$blockChompingIndicator = '+';
@@ -93,10 +93,10 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
9393
if ($value instanceof TaggedValue) {
9494
$output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
9595

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

102102
foreach (explode("\n", $value->getValue()) as $row) {

Exception/ParseException.php

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

104104
$dot = false;
105-
if ('.' === substr($this->message, -1)) {
105+
if (str_ends_with($this->message, '.')) {
106106
$this->message = substr($this->message, 0, -1);
107107
$dot = true;
108108
}

Inline.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public static function dump(mixed $value, int $flags = 0): string
158158
$repr = str_ireplace('INF', '.Inf', $repr);
159159
} elseif (floor($value) == $value && $repr == $value) {
160160
// Preserve float data type since storing a whole number will result in integer value.
161-
if (false === strpos($repr, 'E')) {
161+
if (!str_contains($repr, 'E')) {
162162
$repr = $repr.'.0';
163163
}
164164
}
@@ -357,7 +357,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
357357
$value = self::parseScalar($sequence, $flags, [',', ']'], $i, null === $tag, $references, $isQuoted);
358358

359359
// the value can be an array if a reference has been resolved to an array var
360-
if (\is_string($value) && !$isQuoted && false !== strpos($value, ': ')) {
360+
if (\is_string($value) && !$isQuoted && str_contains($value, ': ')) {
361361
// embedded mapping?
362362
try {
363363
$pos = 0;
@@ -541,7 +541,7 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
541541
$isQuotedString = false;
542542
$scalar = trim($scalar);
543543

544-
if (0 === strpos($scalar, '*')) {
544+
if (str_starts_with($scalar, '*')) {
545545
if (false !== $pos = strpos($scalar, '#')) {
546546
$value = substr($scalar, 1, $pos - 2);
547547
} else {
@@ -573,7 +573,7 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
573573
return false;
574574
case '!' === $scalar[0]:
575575
switch (true) {
576-
case 0 === strpos($scalar, '!!str '):
576+
case str_starts_with($scalar, '!!str '):
577577
$s = (string) substr($scalar, 6);
578578

579579
if (\in_array($s[0] ?? '', ['"', "'"], true)) {
@@ -582,9 +582,9 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
582582
}
583583

584584
return $s;
585-
case 0 === strpos($scalar, '! '):
585+
case str_starts_with($scalar, '! '):
586586
return substr($scalar, 2);
587-
case 0 === strpos($scalar, '!php/object'):
587+
case str_starts_with($scalar, '!php/object'):
588588
if (self::$objectSupport) {
589589
if (!isset($scalar[12])) {
590590
throw new ParseException('Missing value for tag "!php/object".', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
@@ -598,7 +598,7 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
598598
}
599599

600600
return null;
601-
case 0 === strpos($scalar, '!php/const'):
601+
case str_starts_with($scalar, '!php/const'):
602602
if (self::$constantSupport) {
603603
if (!isset($scalar[11])) {
604604
throw new ParseException('Missing value for tag "!php/const".', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
@@ -616,9 +616,9 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
616616
}
617617

618618
return null;
619-
case 0 === strpos($scalar, '!!float '):
619+
case str_starts_with($scalar, '!!float '):
620620
return (float) substr($scalar, 8);
621-
case 0 === strpos($scalar, '!!binary '):
621+
case str_starts_with($scalar, '!!binary '):
622622
return self::evaluateBinaryScalar(substr($scalar, 9));
623623
default:
624624
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'] || 0 === strpos($values['value'], '#') || (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 && '\\' === substr($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 ('\\' === substr($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 (0 === strpos($value, '*')) {
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 '-' === rtrim($this->currentLine) || 0 === strpos($this->currentLine, '- ');
1025+
return '-' === rtrim($this->currentLine) || str_starts_with($this->currentLine, '- ');
10261026
}
10271027

10281028
/**

0 commit comments

Comments
 (0)