-
iRedds Senior Member
   
-
Posts: 662
Threads: 36
Joined: Apr 2019
Reputation:
45
I can't think of a case where this code would make sense.
PHP Code: $replacekey = str_replace('/(.*)', '', $routeKey); $handler = preg_replace('#^' . $routeKey . '$#u', $handler, $uri); $handler = str_replace($replacekey, str_replace('/', '\\', $replacekey), $handler);
PHP Code: // for route $routes->get('path/(:segment)/(:any)', 'Controller::method/$1/$2');
// $routeKey = path/([^/]+)/(.*) $replacekey = str_replace('/(.*)', '', $routeKey); // path/([^/]+)
$handler = preg_replace('#^' . $routeKey . '$#u', $handler, $uri); // \App\Controllers\Controller::method/1/2
$handler = str_replace($replacekey, str_replace('/', '\\', $replacekey), $handler); // replacing "path/([^/]+)" with "path\([^\]+)" in "\App\Controllers\Controller::method/1/2"
-
kenjis Administrator
      
-
Posts: 3,671
Threads: 96
Joined: Oct 2014
Reputation:
230
05-16-2022, 08:33 PM
(This post was last modified: 05-16-2022, 08:43 PM by kenjis.)
The last `str_replace()` is not covered by PHPUnit tests.
RouterTest passes if I remove the line.
PHP Code: $routeKey: api/posts/(.*) $replacekey: api/posts $handler: \App\Controllers\Api\PostController::show/50 $handler: \App\Controllers\Api\PostController::show/50
PHP Code: $routeKey: api/posts/(.*)/edit $replacekey: api/posts/edit $handler: \App\Controllers\Api\PostController::edit/50 $handler: \App\Controllers\Api\PostController::edit/50
PHP Code: $routeKey: api/posts/(.*) $replacekey: api/posts $handler: \App\Controllers\Api\PostController::update/50 $handler: \App\Controllers\Api\PostController::update/50
It seems the code came from https://github.com/codeigniter4/CodeIgniter4/pull/344
-
iRedds Senior Member
   
-
Posts: 662
Threads: 36
Joined: Apr 2019
Reputation:
45
05-16-2022, 08:55 PM
(This post was last modified: 05-16-2022, 08:56 PM by iRedds.)
Perhaps some separate logic was planned here.
But I don't see the point in the first and second line.
That is, the condition can be reduced to a single preg_replace() call.
-
kenjis Administrator
      
-
Posts: 3,671
Threads: 96
Joined: Oct 2014
Reputation:
230
05-16-2022, 09:05 PM
(This post was last modified: 05-16-2022, 09:11 PM by kenjis.)
From the time of the first PR, it looks the test passes without it.
Code: --- a/system/Router/Router.php
+++ b/system/Router/Router.php
@@ -398,9 +398,9 @@ class Router implements RouterInterface
// ex: $routes->resource('Admin/Admins');
if (strpos($val, '$') !== false && strpos($key, '(') !== false && strpos($key, '/') !== false)
{
- $replacekey = str_replace('/(.*)', '', $key);
+// $replacekey = str_replace('/(.*)', '', $key);
$val = preg_replace('#^'.$key.'$#', $val, $uri);
- $val = str_replace($replacekey, str_replace("/", "\\",$replacekey), $val);
+// $val = str_replace($replacekey, str_replace("/", "\\",$replacekey), $val);
}
elseif (strpos($val, '$') !== false && strpos($key, '(') !== false)
{
(05-16-2022, 08:55 PM)iRedds Wrote: Perhaps some separate logic was planned here.
But I don't see the point in the first and second line.
That is, the condition can be reduced to a single preg_replace() call.
Agreed.
We can remove the if statement.
|