Run Rector on your code to see what it can do for you:

 {
     public function run(string $param)
     {        
-        $r = preg_replace( '#^/(foo|bar)/(\w+)/.+$#', '$2', $param, 1, $count );
-		if ( $count !== 1 ) {
-			$r = preg_replace( '#^/hello/(\w+)/.+$#', '$1', $param, 1 );
+        if ( $count !== 1 ) {
+			return preg_replace( '#^/hello/(\w+)/.+$#', '$1', $param, 1 );
 		}
 
-		return $r;
+		return preg_replace( '#^/(foo|bar)/(\w+)/.+$#', '$2', $param, 1, $count );
     }
 }
PHP snippet to change
Config  rector.php
Rector version: 3edca6 - released at 2025-03-28 17:26