From 9ff51aafc518460e6c45996f09e9fe74e5f8d2e8 Mon Sep 17 00:00:00 2001
From: Morris Jobke <hey@morrisjobke.de>
Date: Thu, 25 Jan 2018 22:26:47 +0100
Subject: [PATCH] Use index based string access for substr with length of 1

Signed-off-by: Morris Jobke <hey@morrisjobke.de>
---
 apps/files_external/lib/Lib/Storage/OwnCloud.php   |  5 +----
 apps/files_external/lib/Lib/Storage/SFTP.php       |  9 ++-------
 apps/files_external/lib/Lib/Storage/SMB.php        | 10 +++-------
 lib/private/App/AppStore/Version/VersionParser.php |  5 ++---
 lib/private/Archive/TAR.php                        |  9 ++-------
 lib/private/Avatar.php                             |  2 +-
 lib/private/Files/Filesystem.php                   |  4 ++--
 lib/private/Files/Storage/DAV.php                  | 10 +++-------
 lib/private/Files/View.php                         |  6 +++---
 lib/private/Installer.php                          |  2 +-
 lib/private/Settings/Personal/PersonalInfo.php     |  2 +-
 lib/private/legacy/image.php                       |  6 +++---
 12 files changed, 24 insertions(+), 46 deletions(-)

diff --git a/apps/files_external/lib/Lib/Storage/OwnCloud.php b/apps/files_external/lib/Lib/Storage/OwnCloud.php
index d846fa811a0..d56e6b66145 100644
--- a/apps/files_external/lib/Lib/Storage/OwnCloud.php
+++ b/apps/files_external/lib/Lib/Storage/OwnCloud.php
@@ -61,10 +61,7 @@ class OwnCloud extends \OC\Files\Storage\DAV{
 		}
 
 		if (isset($params['root'])){
-			$root = $params['root'];
-			if (substr($root, 0, 1) !== '/'){
-				$root = '/' . $root;
-			}
+			$root = '/' . ltrim($params['root'], '/');
 		}
 		else{
 			$root = '/';
diff --git a/apps/files_external/lib/Lib/Storage/SFTP.php b/apps/files_external/lib/Lib/Storage/SFTP.php
index 22162fa61cf..6bd77dd2496 100644
--- a/apps/files_external/lib/Lib/Storage/SFTP.php
+++ b/apps/files_external/lib/Lib/Storage/SFTP.php
@@ -103,13 +103,8 @@ class SFTP extends \OC\Files\Storage\Common {
 		$this->root
 			= isset($params['root']) ? $this->cleanPath($params['root']) : '/';
 
-		if ($this->root[0] !== '/') {
-			 $this->root = '/' . $this->root;
-		}
-
-		if (substr($this->root, -1, 1) !== '/') {
-			$this->root .= '/';
-		}
+		$this->root = '/' . ltrim($this->root, '/');
+		$this->root = rtrim($this->root, '/') . '/';
 	}
 
 	/**
diff --git a/apps/files_external/lib/Lib/Storage/SMB.php b/apps/files_external/lib/Lib/Storage/SMB.php
index d8bbe8c4718..58a6f65990f 100644
--- a/apps/files_external/lib/Lib/Storage/SMB.php
+++ b/apps/files_external/lib/Lib/Storage/SMB.php
@@ -84,13 +84,9 @@ class SMB extends Common implements INotifyStorage {
 			}
 			$this->share = $this->server->getShare(trim($params['share'], '/'));
 
-			$this->root = isset($params['root']) ? $params['root'] : '/';
-			if (!$this->root || $this->root[0] !== '/') {
-				$this->root = '/' . $this->root;
-			}
-			if (substr($this->root, -1, 1) !== '/') {
-				$this->root .= '/';
-			}
+			$this->root = $params['root'] ?? '/';
+			$this->root = '/' . ltrim($this->root, '/');
+			$this->root = rtrim($this->root, '/') . '/';
 		} else {
 			throw new \Exception('Invalid configuration');
 		}
diff --git a/lib/private/App/AppStore/Version/VersionParser.php b/lib/private/App/AppStore/Version/VersionParser.php
index 3924b8aea16..cdbb62ffbc5 100644
--- a/lib/private/App/AppStore/Version/VersionParser.php
+++ b/lib/private/App/AppStore/Version/VersionParser.php
@@ -63,11 +63,10 @@ class VersionParser {
 				if(!$this->isValidVersionString($firstVersionNumber)) {
 					break;
 				}
-				if(substr($firstVersion, 0, 1) === '>') {
+				if(strpos($firstVersion, '>') === 0) {
 					return new Version($firstVersionNumber, '');
-				} else {
-					return new Version('', $firstVersionNumber);
 				}
+				return new Version('', $firstVersionNumber);
 			case 2:
 				if(!$this->isValidVersionString($firstVersionNumber) || !$this->isValidVersionString($secondVersionNumber)) {
 					break;
diff --git a/lib/private/Archive/TAR.php b/lib/private/Archive/TAR.php
index 2c34125afe6..f37da6fc52d 100644
--- a/lib/private/Archive/TAR.php
+++ b/lib/private/Archive/TAR.php
@@ -91,9 +91,7 @@ class TAR extends Archive {
 	 */
 	public function addFolder($path) {
 		$tmpBase = \OC::$server->getTempManager()->getTemporaryFolder();
-		if (substr($path, -1, 1) != '/') {
-			$path .= '/';
-		}
+		$path = rtrim($path, '/') . '/';
 		if ($this->fileExists($path)) {
 			return false;
 		}
@@ -297,10 +295,7 @@ class TAR extends Archive {
 		if ((array_search($path, $files) !== false) or (array_search($path . '/', $files) !== false)) {
 			return true;
 		} else {
-			$folderPath = $path;
-			if (substr($folderPath, -1, 1) != '/') {
-				$folderPath .= '/';
-			}
+			$folderPath = rtrim($path, '/') . '/';
 			$pathLength = strlen($folderPath);
 			foreach ($files as $file) {
 				if (strlen($file) > $pathLength and substr($file, 0, $pathLength) == $folderPath) {
diff --git a/lib/private/Avatar.php b/lib/private/Avatar.php
index b3df607a32b..4cacc801689 100644
--- a/lib/private/Avatar.php
+++ b/lib/private/Avatar.php
@@ -263,7 +263,7 @@ class Avatar implements IAvatar {
 	 * @return string
 	 */
 	private function generateAvatar($userDisplayName, $size) {
-		$text = strtoupper(substr($userDisplayName, 0, 1));
+		$text = strtoupper($userDisplayName[0]);
 		$backgroundColor = $this->avatarBackgroundColor($userDisplayName);
 
 		$im = imagecreatetruecolor($size, $size);
diff --git a/lib/private/Files/Filesystem.php b/lib/private/Files/Filesystem.php
index caf23afba11..95703eab925 100644
--- a/lib/private/Files/Filesystem.php
+++ b/lib/private/Files/Filesystem.php
@@ -839,8 +839,8 @@ class Filesystem {
 		$path = preg_replace('#/{2,}#', '/', $path);
 
 		//remove trailing slash
-		if ($stripTrailingSlash and strlen($path) > 1 and substr($path, -1, 1) === '/') {
-			$path = substr($path, 0, -1);
+		if ($stripTrailingSlash and strlen($path) > 1) {
+			$path = rtrim($path, '/');
 		}
 
 		// remove trailing '/.'
diff --git a/lib/private/Files/Storage/DAV.php b/lib/private/Files/Storage/DAV.php
index 43347aa0b8f..f9c6175308c 100644
--- a/lib/private/Files/Storage/DAV.php
+++ b/lib/private/Files/Storage/DAV.php
@@ -118,13 +118,9 @@ class DAV extends Common {
 					$this->certPath = $certPath;
 				}
 			}
-			$this->root = isset($params['root']) ? $params['root'] : '/';
-			if (!$this->root || $this->root[0] != '/') {
-				$this->root = '/' . $this->root;
-			}
-			if (substr($this->root, -1, 1) != '/') {
-				$this->root .= '/';
-			}
+			$this->root = $params['root'] ?? '/';
+			$this->root = '/' . ltrim($this->root, '/');
+			$this->root = rtrim($this->root, '/') . '/';
 		} else {
 			throw new \Exception('Invalid webdav storage configuration');
 		}
diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php
index fb0b116666e..1553444e05d 100644
--- a/lib/private/Files/View.php
+++ b/lib/private/Files/View.php
@@ -698,7 +698,7 @@ class View {
 			// do not allow deleting the root
 			return false;
 		}
-		$postFix = (substr($path, -1, 1) === '/') ? '/' : '';
+		$postFix = (substr($path, -1) === '/') ? '/' : '';
 		$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
 		$mount = Filesystem::getMountManager()->find($absolutePath . $postFix);
 		if ($mount and $mount->getInternalPath($absolutePath) === '') {
@@ -1067,7 +1067,7 @@ class View {
 	 * @return bool|null|string
 	 */
 	public function hash($type, $path, $raw = false) {
-		$postFix = (substr($path, -1, 1) === '/') ? '/' : '';
+		$postFix = (substr($path, -1) === '/') ? '/' : '';
 		$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
 		if (Filesystem::isValidPath($path)) {
 			$path = $this->getRelativePath($absolutePath);
@@ -1119,7 +1119,7 @@ class View {
 	 * \OC\Files\Storage\Storage for delegation to a storage backend for execution
 	 */
 	private function basicOperation($operation, $path, $hooks = [], $extraParam = null) {
-		$postFix = (substr($path, -1, 1) === '/') ? '/' : '';
+		$postFix = (substr($path, -1) === '/') ? '/' : '';
 		$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
 		if (Filesystem::isValidPath($path)
 			and !Filesystem::isFileBlacklisted($path)
diff --git a/lib/private/Installer.php b/lib/private/Installer.php
index ed654c2b241..ea8ee5f01e5 100644
--- a/lib/private/Installer.php
+++ b/lib/private/Installer.php
@@ -519,7 +519,7 @@ class Installer {
 		foreach(\OC::$APPSROOTS as $app_dir) {
 			if($dir = opendir( $app_dir['path'] )) {
 				while( false !== ( $filename = readdir( $dir ))) {
-					if( substr( $filename, 0, 1 ) != '.' and is_dir($app_dir['path']."/$filename") ) {
+					if( $filename[0] !== '.' and is_dir($app_dir['path']."/$filename") ) {
 						if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) {
 							if(!Installer::isInstalled($filename)) {
 								$info=OC_App::getAppInfo($filename);
diff --git a/lib/private/Settings/Personal/PersonalInfo.php b/lib/private/Settings/Personal/PersonalInfo.php
index dbc05cbda2f..ad277b43194 100644
--- a/lib/private/Settings/Personal/PersonalInfo.php
+++ b/lib/private/Settings/Personal/PersonalInfo.php
@@ -208,7 +208,7 @@ class PersonalInfo implements ISettings {
 			$l = \OC::$server->getL10N('settings', $lang);
 			// TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version
 			$potentialName = (string) $l->t('__language_name__');
-			if($l->getLanguageCode() === $lang && substr($potentialName, 0, 1) !== '_') {//first check if the language name is in the translation file
+			if($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file
 				$ln = array('code' => $lang, 'name' => $potentialName);
 			} elseif ($lang === 'en') {
 				$ln = ['code' => $lang, 'name' => 'English (US)'];
diff --git a/lib/private/legacy/image.php b/lib/private/legacy/image.php
index 6ad9426a717..eeee3b24073 100644
--- a/lib/private/legacy/image.php
+++ b/lib/private/legacy/image.php
@@ -784,16 +784,16 @@ class OC_Image implements \OCP\IImage {
 						$color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3);
 						break;
 					case 8:
-						$color = @unpack('n', $vide . substr($data, $p, 1));
+						$color = @unpack('n', $vide . ($data[$p] ?? ''));
 						$color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1];
 						break;
 					case 4:
-						$color = @unpack('n', $vide . substr($data, floor($p), 1));
+						$color = @unpack('n', $vide . ($data[floor($p)] ?? ''));
 						$color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F;
 						$color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1];
 						break;
 					case 1:
-						$color = @unpack('n', $vide . substr($data, floor($p), 1));
+						$color = @unpack('n', $vide . ($data[floor($p)] ?? ''));
 						switch (($p * 8) % 8) {
 							case 0:
 								$color[1] = $color[1] >> 7;
-- 
GitLab