From d060180140923ac054b252f0cbd821063a53f5b7 Mon Sep 17 00:00:00 2001
From: Lukas Reschke <lukas@owncloud.com>
Date: Thu, 23 Oct 2014 23:27:15 +0200
Subject: [PATCH] Use function outside of loop

Otherwise the function is executed n times which is a lot of overhead
---
 apps/files_external/lib/config.php                         | 7 ++++---
 apps/user_ldap/lib/wizard.php                              | 3 ++-
 apps/user_ldap/settings.php                                | 4 ++--
 .../appframework/middleware/middlewaredispatcher.php       | 5 +++--
 lib/private/arrayparser.php                                | 4 +++-
 lib/private/db/statementwrapper.php                        | 3 ++-
 lib/private/ocsclient.php                                  | 5 +++--
 lib/private/vobject.php                                    | 3 ++-
 8 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php
index 92bb891ca2..5378137e1d 100644
--- a/apps/files_external/lib/config.php
+++ b/apps/files_external/lib/config.php
@@ -716,10 +716,11 @@ class OC_Mount_Config {
 				}
 			}
 
-			if (count($dependencyGroup) > 0) {
+			$dependencyGroupCount = count($dependencyGroup);
+			if ($dependencyGroupCount > 0) {
 				$backends = '';
-				for ($i = 0; $i < count($dependencyGroup); $i++) {
-					if ($i > 0 && $i === count($dependencyGroup) - 1) {
+				for ($i = 0; $i < $dependencyGroupCount; $i++) {
+					if ($i > 0 && $i === $dependencyGroupCount - 1) {
 						$backends .= $l->t(' and ');
 					} elseif ($i > 0) {
 						$backends .= ', ';
diff --git a/apps/user_ldap/lib/wizard.php b/apps/user_ldap/lib/wizard.php
index e2a85ea5eb..1d7701440e 100644
--- a/apps/user_ldap/lib/wizard.php
+++ b/apps/user_ldap/lib/wizard.php
@@ -685,7 +685,8 @@ class Wizard extends LDAPUtility {
 			$this->ldap->getDN($cr, $er);
 			$attrs = $this->ldap->getAttributes($cr, $er);
 			$result = array();
-			for($i = 0; $i < count($possibleAttrs); $i++) {
+			$possibleAttrsCount = count($possibleAttrs);
+			for($i = 0; $i < $possibleAttrsCount; $i++) {
 				if(isset($attrs[$possibleAttrs[$i]])) {
 					$result[$possibleAttrs[$i]] = $attrs[$possibleAttrs[$i]]['count'];
 				}
diff --git a/apps/user_ldap/settings.php b/apps/user_ldap/settings.php
index 1e588b1cd8..ca61a53b19 100644
--- a/apps/user_ldap/settings.php
+++ b/apps/user_ldap/settings.php
@@ -54,8 +54,8 @@ $wizTabs[] = array('tpl' => 'part.wizard-server',      'cap' => $l->t('Server'))
 $wizTabs[] = array('tpl' => 'part.wizard-userfilter',  'cap' => $l->t('User Filter'));
 $wizTabs[] = array('tpl' => 'part.wizard-loginfilter', 'cap' => $l->t('Login Filter'));
 $wizTabs[] = array('tpl' => 'part.wizard-groupfilter', 'cap' => $l->t('Group Filter'));
-
-for($i = 0; $i < count($wizTabs); $i++) {
+$wizTabsCount = count($wizTabs);
+for($i = 0; $i < $wizTabsCount; $i++) {
 	$tab = new OCP\Template('user_ldap', $wizTabs[$i]['tpl']);
 	if($i === 0) {
 		$tab->assign('serverConfigurationPrefixes', $prefixes);
diff --git a/lib/private/appframework/middleware/middlewaredispatcher.php b/lib/private/appframework/middleware/middlewaredispatcher.php
index dcb63a8e55..41eef4aedb 100644
--- a/lib/private/appframework/middleware/middlewaredispatcher.php
+++ b/lib/private/appframework/middleware/middlewaredispatcher.php
@@ -82,8 +82,9 @@ class MiddlewareDispatcher {
 	 */
 	public function beforeController(Controller $controller, $methodName){
 		// we need to count so that we know which middlewares we have to ask in
-		// case theres an exception
-		for($i=0; $i<count($this->middlewares); $i++){
+		// case there is an exception
+		$middlewareCount = count($this->middlewares);
+		for($i = 0; $i < $middlewareCount; $i++){
 			$this->middlewareCounter++;
 			$middleware = $this->middlewares[$i];
 			$middleware->beforeController($controller, $methodName);
diff --git a/lib/private/arrayparser.php b/lib/private/arrayparser.php
index a5e1f6653f..dab1817c2e 100644
--- a/lib/private/arrayparser.php
+++ b/lib/private/arrayparser.php
@@ -182,7 +182,9 @@ class ArrayParser {
 		if (substr($body, -1, 1) !== ',') {
 			$body .= ',';
 		}
-		for ($i = 0; $i < strlen($body); $i++) {
+
+		$bodyLength = strlen($body);
+		for ($i = 0; $i < $bodyLength; $i++) {
 			$char = substr($body, $i, 1);
 			if ($char === '\\') {
 				if ($escaped) {
diff --git a/lib/private/db/statementwrapper.php b/lib/private/db/statementwrapper.php
index 93fabc147c..ad63de98e9 100644
--- a/lib/private/db/statementwrapper.php
+++ b/lib/private/db/statementwrapper.php
@@ -89,9 +89,10 @@ class OC_DB_StatementWrapper {
 			$cArg = 0;
 
 			$inSubstring = false;
+			$queryLength = strlen($query);
 
 			// Create new query
-			for ($i = 0; $i < strlen ($query); $i++) {
+			for ($i = 0; $i < $queryLength; $i++) {
 				if ($inSubstring == false) {
 					// Defines when we should start inserting values
 					if (substr ($query, $i, 9) == 'SUBSTRING') {
diff --git a/lib/private/ocsclient.php b/lib/private/ocsclient.php
index 8ceb43f4c1..351027d801 100644
--- a/lib/private/ocsclient.php
+++ b/lib/private/ocsclient.php
@@ -129,8 +129,9 @@ class OC_OCSClient{
 		$data = simplexml_load_string($xml);
 		libxml_disable_entity_loader($loadEntities);
 
-		$tmp=$data->data->content;
-		for($i = 0; $i < count($tmp); $i++) {
+		$tmp = $data->data->content;
+		$tmpCount = count($tmp);
+		for($i = 0; $i < $tmpCount; $i++) {
 			$app=array();
 			$app['id']=(string)$tmp[$i]->id;
 			$app['name']=(string)$tmp[$i]->name;
diff --git a/lib/private/vobject.php b/lib/private/vobject.php
index 94e3470ff0..9d121c17d7 100644
--- a/lib/private/vobject.php
+++ b/lib/private/vobject.php
@@ -72,7 +72,8 @@ class OC_VObject{
 	 */
 	public static function unescapeSemicolons($value) {
 		$array = explode(';', $value);
-		for($i=0;$i<count($array);$i++) {
+		$arrayCount = count($array);
+		for($i = 0; $i < $arrayCount; $i++) {
 			if(substr($array[$i], -2, 2)=="\\\\") {
 				if(isset($array[$i+1])) {
 					$array[$i] = substr($array[$i], 0, count($array[$i])-2).';'.$array[$i+1];
-- 
GitLab