From 1dbf36b86bdd039e875d0eae60f7d99e11f5eecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sat, 27 May 2023 23:58:28 +0200 Subject: [PATCH] Use str_contains and similar instead of strpos --- config/packages/test/framework.yaml | 2 -- phpunit.xml.dist | 1 + src/Controller/RedirectController.php | 2 +- src/Controller/SelectAPIController.php | 2 +- src/Entity/Attachments/Attachment.php | 2 +- src/Entity/LogSystem/AbstractLogEntry.php | 2 +- src/Entity/UserSystem/PermissionData.php | 2 +- .../UserSystem/PasswordChangeNeededSubscriber.php | 2 +- src/Form/Type/SIUnitType.php | 2 +- src/Helpers/LabelResponse.php | 2 +- src/Services/Attachments/AttachmentPathResolver.php | 4 ++-- src/Services/Attachments/AttachmentURLGenerator.php | 2 +- src/Services/Attachments/FileTypeFilterTools.php | 6 +++--- src/Services/LabelSystem/BarcodeGenerator.php | 2 +- src/Services/Trees/TreeViewGenerator.php | 2 +- src/Services/UserSystem/PermissionManager.php | 2 +- 16 files changed, 18 insertions(+), 19 deletions(-) diff --git a/config/packages/test/framework.yaml b/config/packages/test/framework.yaml index d051c840..f76cc2ef 100644 --- a/config/packages/test/framework.yaml +++ b/config/packages/test/framework.yaml @@ -1,4 +1,2 @@ framework: test: true - session: - storage_id: session.storage.mock_file diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 59622803..05724d3b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,6 +9,7 @@ > + diff --git a/src/Controller/RedirectController.php b/src/Controller/RedirectController.php index 811ab135..a6099ace 100644 --- a/src/Controller/RedirectController.php +++ b/src/Controller/RedirectController.php @@ -62,7 +62,7 @@ class RedirectController extends AbstractController //If either mod_rewrite is not enabled or the index.php version is enforced, add index.php to the string if (($this->enforce_index_php || !$this->checkIfModRewriteAvailable()) - && false === strpos($new_url, 'index.php')) { + && !str_contains($new_url, 'index.php')) { //Like Request::getUriForPath only with index.php $new_url = $request->getSchemeAndHttpHost().$request->getBaseUrl().'/index.php/'.$locale.$request->getPathInfo(); } diff --git a/src/Controller/SelectAPIController.php b/src/Controller/SelectAPIController.php index f01f03ca..18cca638 100644 --- a/src/Controller/SelectAPIController.php +++ b/src/Controller/SelectAPIController.php @@ -198,7 +198,7 @@ class SelectAPIController extends AbstractController //Remove the data-* prefix for each key $data = array_combine( array_map(static function ($key) { - if (strpos($key, 'data-') === 0) { + if (str_starts_with($key, 'data-')) { return substr($key, 5); } return $key; diff --git a/src/Entity/Attachments/Attachment.php b/src/Entity/Attachments/Attachment.php index 35c07571..39b755ce 100644 --- a/src/Entity/Attachments/Attachment.php +++ b/src/Entity/Attachments/Attachment.php @@ -417,7 +417,7 @@ abstract class Attachment extends AbstractNamedDBElement { //Only set if the URL is not empty if (!empty($url)) { - if (false !== strpos($url, '%BASE%') || false !== strpos($url, '%MEDIA%')) { + if (str_contains($url, '%BASE%') || str_contains($url, '%MEDIA%')) { throw new InvalidArgumentException('You can not reference internal files via the url field! But nice try!'); } diff --git a/src/Entity/LogSystem/AbstractLogEntry.php b/src/Entity/LogSystem/AbstractLogEntry.php index 703049e8..76b0d746 100644 --- a/src/Entity/LogSystem/AbstractLogEntry.php +++ b/src/Entity/LogSystem/AbstractLogEntry.php @@ -222,7 +222,7 @@ abstract class AbstractLogEntry extends AbstractDBElement */ public function isCLIEntry(): bool { - return strpos($this->username, '!!!CLI ') === 0; + return str_starts_with($this->username, '!!!CLI '); } /** diff --git a/src/Entity/UserSystem/PermissionData.php b/src/Entity/UserSystem/PermissionData.php index bac73282..d67ee866 100644 --- a/src/Entity/UserSystem/PermissionData.php +++ b/src/Entity/UserSystem/PermissionData.php @@ -128,7 +128,7 @@ final class PermissionData implements \JsonSerializable public function isPermissionSet(string $permission, string $operation): bool { //We cannot access metadata via normal permission data - if (strpos($permission, '$') !== false) { + if (str_contains($permission, '$')) { return false; } diff --git a/src/EventSubscriber/UserSystem/PasswordChangeNeededSubscriber.php b/src/EventSubscriber/UserSystem/PasswordChangeNeededSubscriber.php index ef7da6a9..fb12942b 100644 --- a/src/EventSubscriber/UserSystem/PasswordChangeNeededSubscriber.php +++ b/src/EventSubscriber/UserSystem/PasswordChangeNeededSubscriber.php @@ -96,7 +96,7 @@ final class PasswordChangeNeededSubscriber implements EventSubscriberInterface /* Dont redirect tree endpoints, as this would cause trouble and creates multiple flash warnigs for one page reload */ - if (false !== strpos($request->getUri(), '/tree/')) { + if (str_contains($request->getUri(), '/tree/')) { return; } diff --git a/src/Form/Type/SIUnitType.php b/src/Form/Type/SIUnitType.php index bfec23e2..0cddb2a1 100644 --- a/src/Form/Type/SIUnitType.php +++ b/src/Form/Type/SIUnitType.php @@ -138,7 +138,7 @@ final class SIUnitType extends AbstractType implements DataMapperInterface //Check if we need to make this thing small if (isset($options['attr']['class'])) { - $view->vars['sm'] = false !== strpos($options['attr']['class'], 'form-control-sm'); + $view->vars['sm'] = str_contains($options['attr']['class'], 'form-control-sm'); } $view->vars['unit'] = $options['unit']; diff --git a/src/Helpers/LabelResponse.php b/src/Helpers/LabelResponse.php index 3ce26516..98451e2f 100644 --- a/src/Helpers/LabelResponse.php +++ b/src/Helpers/LabelResponse.php @@ -110,7 +110,7 @@ class LabelResponse extends Response */ public function setContentDisposition(string $disposition, string $filename, string $filenameFallback = ''): self { - if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) { + if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || str_contains($filename, '%'))) { $encoding = mb_detect_encoding($filename, null, true) ?: '8bit'; for ($i = 0, $filenameLength = mb_strlen($filename, $encoding); $i < $filenameLength; ++$i) { diff --git a/src/Services/Attachments/AttachmentPathResolver.php b/src/Services/Attachments/AttachmentPathResolver.php index 9617024e..635d0bc6 100644 --- a/src/Services/Attachments/AttachmentPathResolver.php +++ b/src/Services/Attachments/AttachmentPathResolver.php @@ -134,7 +134,7 @@ class AttachmentPathResolver $count = 0; //When path is a footprint we have to first run the string through our lecagy german mapping functions - if (strpos($placeholder_path, '%FOOTPRINTS%') !== false) { + if (str_contains($placeholder_path, '%FOOTPRINTS%')) { $placeholder_path = $this->convertOldFootprintPath($placeholder_path); } @@ -151,7 +151,7 @@ class AttachmentPathResolver } //Path is invalid if path is directory traversal - if (false !== strpos($placeholder_path, '..')) { + if (str_contains($placeholder_path, '..')) { return null; } diff --git a/src/Services/Attachments/AttachmentURLGenerator.php b/src/Services/Attachments/AttachmentURLGenerator.php index 742cce81..cbeab36a 100644 --- a/src/Services/Attachments/AttachmentURLGenerator.php +++ b/src/Services/Attachments/AttachmentURLGenerator.php @@ -78,7 +78,7 @@ class AttachmentURLGenerator } //Our absolute path must begin with public path, or we can not use it for asset pathes. - if (0 !== strpos($absolute_path, $public_path)) { + if (!str_starts_with($absolute_path, $public_path)) { return null; } diff --git a/src/Services/Attachments/FileTypeFilterTools.php b/src/Services/Attachments/FileTypeFilterTools.php index 6fc0a162..1b06655a 100644 --- a/src/Services/Attachments/FileTypeFilterTools.php +++ b/src/Services/Attachments/FileTypeFilterTools.php @@ -108,7 +108,7 @@ class FileTypeFilterTools } //Convert *.jpg to .jpg - if (0 === strpos($element, '*.')) { + if (str_starts_with($element, '*.')) { $element = str_replace('*.', '.', $element); } @@ -119,7 +119,7 @@ class FileTypeFilterTools $element = 'video/*'; } elseif ('audio' === $element || 'audio/' === $element) { $element = 'audio/*'; - } elseif (!preg_match('#^[-\w.]+/[-\w.*]+#', $element) && 0 !== strpos($element, '.')) { + } elseif (!preg_match('#^[-\w.]+/[-\w.*]+#', $element) && !str_starts_with($element, '.')) { //Convert jpg to .jpg $element = '.'.$element; } @@ -147,7 +147,7 @@ class FileTypeFilterTools foreach ($elements as $element) { $element = trim($element); - if (0 === strpos($element, '.')) { + if (str_starts_with($element, '.')) { //We found an explicit specified file extension -> add it to list $extensions[] = substr($element, 1); } elseif ('image/*' === $element) { diff --git a/src/Services/LabelSystem/BarcodeGenerator.php b/src/Services/LabelSystem/BarcodeGenerator.php index d88210ca..801154a3 100644 --- a/src/Services/LabelSystem/BarcodeGenerator.php +++ b/src/Services/LabelSystem/BarcodeGenerator.php @@ -79,7 +79,7 @@ final class BarcodeGenerator $repr = 'data:'; $repr .= $mime; - if (0 === strpos($mime, 'text/')) { + if (str_starts_with($mime, 'text/')) { $repr .= ','.rawurlencode($data); } else { $repr .= ';base64,'.base64_encode($data); diff --git a/src/Services/Trees/TreeViewGenerator.php b/src/Services/Trees/TreeViewGenerator.php index 29d09f9b..e5bb4ddc 100644 --- a/src/Services/Trees/TreeViewGenerator.php +++ b/src/Services/Trees/TreeViewGenerator.php @@ -130,7 +130,7 @@ class TreeViewGenerator } //Translate text if text starts with $$ - if (0 === strpos($item->getText(), '$$')) { + if (str_starts_with($item->getText(), '$$')) { $item->setText($this->translator->trans(substr($item->getText(), 2))); } } diff --git a/src/Services/UserSystem/PermissionManager.php b/src/Services/UserSystem/PermissionManager.php index 1e3209fa..47e42635 100644 --- a/src/Services/UserSystem/PermissionManager.php +++ b/src/Services/UserSystem/PermissionManager.php @@ -215,7 +215,7 @@ class PermissionManager //Set every op listed in also Set foreach ($op['alsoSet'] as $set_also) { //If the alsoSet value contains a dot then we set the operation of another permission - if (false !== strpos($set_also, '.')) { + if (str_contains($set_also, '.')) { [$set_perm, $set_op] = explode('.', $set_also); } else { //Else we set the operation of the same permission