Skip to content

Commit ad1790c

Browse files
[ErrorHandler] Do not use the php80 polyfill
1 parent 855232f commit ad1790c

8 files changed

+30
-29
lines changed

src/Symfony/Component/ErrorHandler/BufferingLogger.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __wakeup()
4848
public function __destruct()
4949
{
5050
foreach ($this->logs as [$level, $message, $context]) {
51-
if (str_contains($message, '{')) {
51+
if (false !== strpos($message, '{')) {
5252
foreach ($context as $key => $val) {
5353
if (null === $val || is_scalar($val) || (\is_object($val) && \is_callable([$val, '__toString']))) {
5454
$message = str_replace("{{$key}}", $val, $message);

src/Symfony/Component/ErrorHandler/DebugClassLoader.php

+17-17
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ private function checkClass(string $class, string $file = null): void
396396
}
397397

398398
if (!$exists) {
399-
if (str_contains($class, '/')) {
399+
if (false !== strpos($class, '/')) {
400400
throw new \RuntimeException(sprintf('Trying to autoload a class with an invalid name "%s". Be careful that the namespace separator is "\" in PHP, not "/".', $class));
401401
}
402402

@@ -419,7 +419,7 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
419419
}
420420
$deprecations = [];
421421

422-
$className = str_contains($class, "@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous' : $class;
422+
$className = false !== strpos($class, "@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous' : $class;
423423

424424
// Don't trigger deprecations for classes in the same vendor
425425
if ($class !== $className) {
@@ -435,17 +435,17 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
435435
// Detect annotations on the class
436436
if (false !== $doc = $refl->getDocComment()) {
437437
foreach (['final', 'deprecated', 'internal'] as $annotation) {
438-
if (str_contains($doc, $annotation) && preg_match('#\n\s+\* @'.$annotation.'(?:( .+?)\.?)?\r?\n\s+\*(?: @|/$|\r?\n)#s', $doc, $notice)) {
438+
if (false !== strpos($doc, $annotation) && preg_match('#\n\s+\* @'.$annotation.'(?:( .+?)\.?)?\r?\n\s+\*(?: @|/$|\r?\n)#s', $doc, $notice)) {
439439
self::${$annotation}[$class] = isset($notice[1]) ? preg_replace('#\.?\r?\n( \*)? *(?= |\r?\n|$)#', '', $notice[1]) : '';
440440
}
441441
}
442442

443-
if ($refl->isInterface() && str_contains($doc, 'method') && preg_match_all('#\n \* @method\s+(static\s+)?+([\w\|&\[\]\\\]+\s+)?(\w+(?:\s*\([^\)]*\))?)+(.+?([[:punct:]]\s*)?)?(?=\r?\n \*(?: @|/$|\r?\n))#', $doc, $notice, \PREG_SET_ORDER)) {
443+
if ($refl->isInterface() && false !== strpos($doc, 'method') && preg_match_all('#\n \* @method\s+(static\s+)?+([\w\|&\[\]\\\]+\s+)?(\w+(?:\s*\([^\)]*\))?)+(.+?([[:punct:]]\s*)?)?(?=\r?\n \*(?: @|/$|\r?\n))#', $doc, $notice, \PREG_SET_ORDER)) {
444444
foreach ($notice as $method) {
445445
$static = '' !== $method[1] && !empty($method[2]);
446446
$name = $method[3];
447447
$description = $method[4] ?? null;
448-
if (!str_contains($name, '(')) {
448+
if (false === strpos($name, '(')) {
449449
$name .= '()';
450450
}
451451
if (null !== $description) {
@@ -496,7 +496,7 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
496496
}
497497
} elseif (!$refl->isInterface()) {
498498
if (!strncmp($vendor, str_replace('_', '\\', $use), $vendorLen)
499-
&& str_starts_with($className, 'Symfony\\')
499+
&& 0 === strpos($className, 'Symfony\\')
500500
&& (!class_exists(InstalledVersions::class)
501501
|| 'symfony/symfony' !== InstalledVersions::getRootPackage()['name'])
502502
) {
@@ -597,12 +597,12 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
597597

598598
$forcePatchTypes = $this->patchTypes['force'];
599599

600-
if ($canAddReturnType = null !== $forcePatchTypes && !str_contains($method->getFileName(), \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR)) {
600+
if ($canAddReturnType = null !== $forcePatchTypes && false === strpos($method->getFileName(), \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR)) {
601601
if ('void' !== (self::MAGIC_METHODS[$method->name] ?? 'void')) {
602602
$this->patchTypes['force'] = $forcePatchTypes ?: 'docblock';
603603
}
604604

605-
$canAddReturnType = str_contains($refl->getFileName(), \DIRECTORY_SEPARATOR.'Tests'.\DIRECTORY_SEPARATOR)
605+
$canAddReturnType = false !== strpos($refl->getFileName(), \DIRECTORY_SEPARATOR.'Tests'.\DIRECTORY_SEPARATOR)
606606
|| $refl->isFinal()
607607
|| $method->isFinal()
608608
|| $method->isPrivate()
@@ -623,8 +623,8 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
623623
$this->patchMethod($method, $returnType, $declaringFile, $normalizedType);
624624
}
625625

626-
if (!str_contains($doc, '* @deprecated') && strncmp($ns, $declaringClass, $len)) {
627-
if ($canAddReturnType && 'docblock' === $this->patchTypes['force'] && !str_contains($method->getFileName(), \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR)) {
626+
if (false === strpos($doc, '* @deprecated') && strncmp($ns, $declaringClass, $len)) {
627+
if ($canAddReturnType && 'docblock' === $this->patchTypes['force'] && false === strpos($method->getFileName(), \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR)) {
628628
$this->patchMethod($method, $returnType, $declaringFile, $normalizedType);
629629
} elseif ('' !== $declaringClass && $this->patchTypes['deprecations']) {
630630
$deprecations[] = sprintf('Method "%s::%s()" will return "%s" as of its next major version. Doing the same in %s "%s" will be required when upgrading.', $declaringClass, $method->name, $normalizedType, interface_exists($declaringClass) ? 'implementation' : 'child class', $className);
@@ -640,7 +640,7 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
640640

641641
$matches = [];
642642

643-
if (!$method->hasReturnType() && ((str_contains($doc, '@return') && preg_match('/\n\s+\* @return +([^\s<(]+)/', $doc, $matches)) || 'void' !== (self::MAGIC_METHODS[$method->name] ?? 'void'))) {
643+
if (!$method->hasReturnType() && ((false !== strpos($doc, '@return') && preg_match('/\n\s+\* @return +([^\s<(]+)/', $doc, $matches)) || 'void' !== (self::MAGIC_METHODS[$method->name] ?? 'void'))) {
644644
$matches = $matches ?: [1 => self::MAGIC_METHODS[$method->name]];
645645
$this->setReturnType($matches[1], $method, $parent);
646646

@@ -662,14 +662,14 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
662662
$finalOrInternal = false;
663663

664664
foreach (['final', 'internal'] as $annotation) {
665-
if (str_contains($doc, $annotation) && preg_match('#\n\s+\* @'.$annotation.'(?:( .+?)\.?)?\r?\n\s+\*(?: @|/$|\r?\n)#s', $doc, $notice)) {
665+
if (false !== strpos($doc, $annotation) && preg_match('#\n\s+\* @'.$annotation.'(?:( .+?)\.?)?\r?\n\s+\*(?: @|/$|\r?\n)#s', $doc, $notice)) {
666666
$message = isset($notice[1]) ? preg_replace('#\.?\r?\n( \*)? *(?= |\r?\n|$)#', '', $notice[1]) : '';
667667
self::${$annotation.'Methods'}[$class][$method->name] = [$class, $message];
668668
$finalOrInternal = true;
669669
}
670670
}
671671

672-
if ($finalOrInternal || $method->isConstructor() || !str_contains($doc, '@param') || StatelessInvocation::class === $class) {
672+
if ($finalOrInternal || $method->isConstructor() || false === strpos($doc, '@param') || StatelessInvocation::class === $class) {
673673
continue;
674674
}
675675
if (!preg_match_all('#\n\s+\* @param +((?(?!callable *\().*?|callable *\(.*\).*?))(?<= )\$([a-zA-Z0-9_\x7f-\xff]++)#', $doc, $matches, \PREG_SET_ORDER)) {
@@ -850,7 +850,7 @@ private function setReturnType(string $types, \ReflectionMethod $method, ?string
850850
$iterable = $object = true;
851851
foreach ($typesMap as $n => $t) {
852852
if ('null' !== $n) {
853-
$iterable = $iterable && (\in_array($n, ['array', 'iterable']) || str_contains($n, 'Iterator'));
853+
$iterable = $iterable && (\in_array($n, ['array', 'iterable']) || false !== strpos($n, 'Iterator'));
854854
$object = $object && (\in_array($n, ['callable', 'object', '$this', 'static']) || !isset(self::SPECIAL_RETURN_TYPES[$n]));
855855
}
856856
}
@@ -1034,15 +1034,15 @@ private static function getUseStatements(string $file): array
10341034
break;
10351035
}
10361036

1037-
if (str_starts_with($file[$i], 'namespace ')) {
1037+
if (0 === strpos($file[$i], 'namespace ')) {
10381038
$namespace = substr($file[$i], \strlen('namespace '), -2).'\\';
10391039
$useOffset = $i + 2;
10401040
}
10411041

1042-
if (str_starts_with($file[$i], 'use ')) {
1042+
if (0 === strpos($file[$i], 'use ')) {
10431043
$useOffset = $i;
10441044

1045-
for (; str_starts_with($file[$i], 'use '); ++$i) {
1045+
for (; 0 === strpos($file[$i], 'use '); ++$i) {
10461046
$u = explode(' as ', substr($file[$i], 4, -2), 2);
10471047

10481048
if (1 === \count($u)) {

src/Symfony/Component/ErrorHandler/ErrorEnhancer/ClassNotFoundErrorEnhancer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private function convertFileToClass(string $path, string $file, string $prefix):
145145
];
146146

147147
if ($prefix) {
148-
$candidates = array_filter($candidates, function ($candidate) use ($prefix) { return str_starts_with($candidate, $prefix); });
148+
$candidates = array_filter($candidates, function ($candidate) use ($prefix) { return 0 === strpos($candidate, $prefix); });
149149
}
150150

151151
// We cannot use the autoloader here as most of them use require; but if the class

src/Symfony/Component/ErrorHandler/ErrorEnhancer/UndefinedFunctionErrorEnhancer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function enhance(\Throwable $error): ?\Throwable
4242

4343
$prefix = 'Call to undefined function ';
4444
$prefixLen = \strlen($prefix);
45-
if (!str_starts_with($message, $prefix)) {
45+
if (0 !== strpos($message, $prefix)) {
4646
return null;
4747
}
4848

src/Symfony/Component/ErrorHandler/ErrorEnhancer/UndefinedMethodErrorEnhancer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function enhance(\Throwable $error): ?\Throwable
4747
$candidates = [];
4848
foreach ($methods as $definedMethodName) {
4949
$lev = levenshtein($methodName, $definedMethodName);
50-
if ($lev <= \strlen($methodName) / 3 || str_contains($definedMethodName, $methodName)) {
50+
if ($lev <= \strlen($methodName) / 3 || false !== strpos($definedMethodName, $methodName)) {
5151
$candidates[] = $definedMethodName;
5252
}
5353
}

src/Symfony/Component/ErrorHandler/ErrorHandler.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ private function reRegister(int $prev): void
400400
*/
401401
public function handleError(int $type, string $message, string $file, int $line): bool
402402
{
403-
if (\PHP_VERSION_ID >= 70300 && \E_WARNING === $type && '"' === $message[0] && str_contains($message, '" targeting switch is equivalent to "break')) {
403+
// Because this file might be used while loading classes, it cannot use any polyfill
404+
if (\PHP_VERSION_ID >= 70300 && \E_WARNING === $type && '"' === $message[0] && false !== strpos($message, '" targeting switch is equivalent to "break')) {
404405
$type = \E_DEPRECATED;
405406
}
406407

@@ -451,7 +452,7 @@ public function handleError(int $type, string $message, string $file, int $line)
451452
return true;
452453
}
453454
} else {
454-
if (str_contains($message, '@anonymous')) {
455+
if (false !== strpos($message, '@anonymous')) {
455456
$backtrace = debug_backtrace(false, 5);
456457

457458
for ($i = 1; isset($backtrace[$i]); ++$i) {
@@ -560,7 +561,7 @@ public function handleException(\Throwable $exception)
560561
}
561562

562563
if ($this->loggedErrors & $type) {
563-
if (str_contains($message = $exception->getMessage(), "@anonymous\0")) {
564+
if (false !== strpos($message = $exception->getMessage(), "@anonymous\0")) {
564565
$message = $this->parseAnonymousClass($message);
565566
}
566567

@@ -674,7 +675,7 @@ public static function handleFatalError(array $error = null): void
674675
$handler->throwAt(0, true);
675676
$trace = $error['backtrace'] ?? null;
676677

677-
if (str_starts_with($error['message'], 'Allowed memory') || str_starts_with($error['message'], 'Out of memory')) {
678+
if (0 === strpos($error['message'], 'Allowed memory') || 0 === strpos($error['message'], 'Out of memory')) {
678679
$fatalError = new OutOfMemoryError($handler->levels[$error['type']].': '.$error['message'], 0, $error, 2, false, $trace);
679680
} else {
680681
$fatalError = new FatalError($handler->levels[$error['type']].': '.$error['message'], 0, $error, 2, true, $trace);

src/Symfony/Component/ErrorHandler/ErrorRenderer/HtmlErrorRenderer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private function getFileRelative(string $file): ?string
199199
{
200200
$file = str_replace('\\', '/', $file);
201201

202-
if (null !== $this->projectDir && str_starts_with($file, $this->projectDir)) {
202+
if (null !== $this->projectDir && 0 === strpos($file, $this->projectDir)) {
203203
return ltrim(substr($file, \strlen($this->projectDir)), '/');
204204
}
205205

@@ -316,7 +316,7 @@ private function formatFileFromText(string $text)
316316

317317
private function formatLogMessage(string $message, array $context)
318318
{
319-
if ($context && str_contains($message, '{')) {
319+
if ($context && false !== strpos($message, '{')) {
320320
$replacements = [];
321321
foreach ($context as $key => $val) {
322322
if (is_scalar($val)) {

src/Symfony/Component/ErrorHandler/Exception/FlattenException.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public function getClass(): string
171171
*/
172172
public function setClass($class): self
173173
{
174-
$this->class = str_contains($class, "@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous' : $class;
174+
$this->class = false !== strpos($class, "@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous' : $class;
175175

176176
return $this;
177177
}
@@ -234,7 +234,7 @@ public function getMessage(): string
234234
*/
235235
public function setMessage($message): self
236236
{
237-
if (str_contains($message, "@anonymous\0")) {
237+
if (false !== strpos($message, "@anonymous\0")) {
238238
$message = preg_replace_callback('/[a-zA-Z_\x7f-\xff][\\\\a-zA-Z0-9_\x7f-\xff]*+@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
239239
return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0])) ?: 'class').'@anonymous' : $m[0];
240240
}, $message);

0 commit comments

Comments
 (0)