From abd27945f0081d833511b6fd023be7ff2ba1665a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 12 Jul 2020 09:49:25 +0700 Subject: [PATCH 1/2] optimize RouteCollection : use foreach instead of for with count when possible --- system/Router/RouteCollection.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 6684e76f4029..eb3baa0d8f12 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -831,12 +831,11 @@ public function resource(string $name, array $options = null): RouteCollectionIn if (isset($options['except'])) { $options['except'] = is_array($options['except']) ? $options['except'] : explode(',', $options['except']); - $c = count($methods); - for ($i = 0; $i < $c; $i ++) + foreach ($methods as &$method) { - if (in_array($methods[$i], $options['except'])) + if (in_array($method, $options['except'])) { - unset($methods[$i]); + unset($method); } } } @@ -946,12 +945,11 @@ public function presenter(string $name, array $options = null): RouteCollectionI if (isset($options['except'])) { $options['except'] = is_array($options['except']) ? $options['except'] : explode(',', $options['except']); - $c = count($methods); - for ($i = 0; $i < $c; $i ++) + foreach ($methods as &$method) { - if (in_array($methods[$i], $options['except'])) + if (in_array($method, $options['except'])) { - unset($methods[$i]); + unset($method); } } } From 983293ec333deb9df8578333dd91cb6c38ebca13 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 12 Jul 2020 11:21:41 +0700 Subject: [PATCH 2/2] fix unset reference variable --- system/Router/RouteCollection.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index eb3baa0d8f12..baf498f0e47d 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -831,11 +831,11 @@ public function resource(string $name, array $options = null): RouteCollectionIn if (isset($options['except'])) { $options['except'] = is_array($options['except']) ? $options['except'] : explode(',', $options['except']); - foreach ($methods as &$method) + foreach ($methods as $i => $method) { if (in_array($method, $options['except'])) { - unset($method); + unset($methods[$i]); } } } @@ -945,11 +945,11 @@ public function presenter(string $name, array $options = null): RouteCollectionI if (isset($options['except'])) { $options['except'] = is_array($options['except']) ? $options['except'] : explode(',', $options['except']); - foreach ($methods as &$method) + foreach ($methods as $i => $method) { if (in_array($method, $options['except'])) { - unset($method); + unset($methods[$i]); } } }