Ayesh / git-php-redirect

Redirects git.php.net URLs to GitHub

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

confused about code snippet

divinity76 opened this issue · comments

this particular part of the code:

		if (isset($params['h'], $params['a']) && preg_match('|refs/heads/(?<branch>[a-z0-9.-]+)|i', $params['h'], $matches)) {
			return match ($params['a']) {
				'shortlog', 'log' => $url . '/commits/' . $matches['branch'],
				'tree'            => $url . '/tree/' . $matches['branch'],
				default           => $url,
			};
		}

is that a typo? or perhaps some php8+ syntax? when running it in php 7.4.3, i get

PHP Parse error: syntax error, unexpected ',' in /home/hans/projects/misc/git-php-redirect/src/Redirector.php on line 49

but that might be because i'm rolling php7 instead of php8

Hi @divinity76 - It is indeed using the new match syntax in PHP 8.0, so doesn't work on PHP 7.

It is equivalent to:

if ($params['a'] === 'shortlog' || $params['a'] === log) {
  return $url . '/commits/' . $matches['branch'];
}
if ($params['a'] === 'tree') {
  return $url . '/tree/' . $matches['branch'];
}

return $url;