From 7bf31df0bc340872a7fb0ce1eb8d11d036189166 Mon Sep 17 00:00:00 2001 From: Hamid Dehnavi Date: Fri, 7 Jul 2023 04:37:57 +0330 Subject: [PATCH] Refactors "substr" calls to improve code readability Signed-off-by: Hamid Dehnavi --- apps/dav/lib/BulkUpload/MultipartRequestParser.php | 2 +- apps/dav/lib/CalDAV/BirthdayService.php | 2 +- apps/dav/lib/Connector/Sabre/FakeLockerPlugin.php | 2 +- apps/dav/lib/Connector/Sabre/Node.php | 2 +- apps/dav/lib/Connector/Sabre/Principal.php | 2 +- apps/dav/lib/DAV/CustomPropertiesBackend.php | 2 +- apps/dav/tests/unit/Connector/Sabre/FileTest.php | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/apps/dav/lib/BulkUpload/MultipartRequestParser.php b/apps/dav/lib/BulkUpload/MultipartRequestParser.php index 2541ea8f33337..7c977b42ccbca 100644 --- a/apps/dav/lib/BulkUpload/MultipartRequestParser.php +++ b/apps/dav/lib/BulkUpload/MultipartRequestParser.php @@ -82,7 +82,7 @@ private function parseBoundaryFromHeaders(string $contentType): string { $boundaryValue = trim($boundaryValue); // Remove potential quotes around boundary value. - if (substr($boundaryValue, 0, 1) === '"' && substr($boundaryValue, -1) === '"') { + if (str_starts_with($boundaryValue, '"') && str_ends_with($boundaryValue, '"')) { $boundaryValue = substr($boundaryValue, 1, -1); } diff --git a/apps/dav/lib/CalDAV/BirthdayService.php b/apps/dav/lib/CalDAV/BirthdayService.php index 95176283f11d4..5d1d55879ca3f 100644 --- a/apps/dav/lib/CalDAV/BirthdayService.php +++ b/apps/dav/lib/CalDAV/BirthdayService.php @@ -419,7 +419,7 @@ private function isGloballyEnabled():bool { * @return string|null */ private function principalToUserId(string $userPrincipal):?string { - if (substr($userPrincipal, 0, 17) === 'principals/users/') { + if (str_starts_with($userPrincipal, 'principals/users/')) { return substr($userPrincipal, 17); } return null; diff --git a/apps/dav/lib/Connector/Sabre/FakeLockerPlugin.php b/apps/dav/lib/Connector/Sabre/FakeLockerPlugin.php index b61e9dc0c39b6..c63b1cf50f2d5 100644 --- a/apps/dav/lib/Connector/Sabre/FakeLockerPlugin.php +++ b/apps/dav/lib/Connector/Sabre/FakeLockerPlugin.php @@ -108,7 +108,7 @@ public function validateTokens(RequestInterface $request, &$conditions) { if (isset($fileCondition['tokens'])) { foreach ($fileCondition['tokens'] as &$token) { if (isset($token['token'])) { - if (substr($token['token'], 0, 16) === 'opaquelocktoken:') { + if (str_starts_with($token['token'], 'opaquelocktoken:')) { $token['validToken'] = true; } } diff --git a/apps/dav/lib/Connector/Sabre/Node.php b/apps/dav/lib/Connector/Sabre/Node.php index c9407b127860d..5237064b46f01 100644 --- a/apps/dav/lib/Connector/Sabre/Node.php +++ b/apps/dav/lib/Connector/Sabre/Node.php @@ -303,7 +303,7 @@ public function getSharePermissions($user) { $mountpoint = $this->info->getMountPoint(); if (!($mountpoint instanceof MoveableMount)) { $mountpointpath = $mountpoint->getMountPoint(); - if (substr($mountpointpath, -1) === '/') { + if (str_ends_with($mountpointpath, '/')) { $mountpointpath = substr($mountpointpath, 0, -1); } diff --git a/apps/dav/lib/Connector/Sabre/Principal.php b/apps/dav/lib/Connector/Sabre/Principal.php index df1de17fe200b..9d9cfbe43cbf6 100644 --- a/apps/dav/lib/Connector/Sabre/Principal.php +++ b/apps/dav/lib/Connector/Sabre/Principal.php @@ -505,7 +505,7 @@ public function findByUri($uri, $principalPrefix) { return $this->principalPrefix . '/' . $user->getUID(); } } - if (substr($uri, 0, 10) === 'principal:') { + if (str_starts_with($uri, 'principal:')) { $principal = substr($uri, 10); $principal = $this->getPrincipalByPath($principal); if ($principal !== null) { diff --git a/apps/dav/lib/DAV/CustomPropertiesBackend.php b/apps/dav/lib/DAV/CustomPropertiesBackend.php index 311fe0ea561f0..e76a71aec637f 100644 --- a/apps/dav/lib/DAV/CustomPropertiesBackend.php +++ b/apps/dav/lib/DAV/CustomPropertiesBackend.php @@ -166,7 +166,7 @@ public function propFind($path, PropFind $propFind) { // substr of calendars/ => path is inside the CalDAV component // two '/' => this a calendar (no calendar-home nor calendar object) - if (substr($path, 0, 10) === 'calendars/' && substr_count($path, '/') === 2) { + if (str_starts_with($path, 'calendars/') && substr_count($path, '/') === 2) { $allRequestedProps = $propFind->getRequestedProperties(); $customPropertiesForShares = [ '{DAV:}displayname', diff --git a/apps/dav/tests/unit/Connector/Sabre/FileTest.php b/apps/dav/tests/unit/Connector/Sabre/FileTest.php index 884245afa3bb2..36dc550a8a392 100644 --- a/apps/dav/tests/unit/Connector/Sabre/FileTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/FileTest.php @@ -1134,7 +1134,7 @@ private function listPartFiles(\OC\Files\View $userView = null, $path = '') { $realPath = $storage->getSourcePath($internalPath); $dh = opendir($realPath); while (($file = readdir($dh)) !== false) { - if (substr($file, strlen($file) - 5, 5) === '.part') { + if (str_ends_with($file, '.part')) { $files[] = $file; } }