Make PHPStan happy.

This commit is contained in:
flash 2023-09-08 00:54:19 +00:00
parent 8b0f960c86
commit 737c99280e
3 changed files with 59 additions and 37 deletions

View file

@ -36,6 +36,44 @@ class InfoRoutes extends RouteHandler {
); );
} }
private static function findDocPath(string $projectPath, string $name): string {
// User provided case, without extension
$attempt = sprintf('%s/%s', $projectPath, $name);
if(is_file($attempt))
return $attempt;
// User provided case, with extension
$attempt = sprintf('%s/%s.md', $projectPath, $name);
if(is_file($attempt))
return $attempt;
$name = strtoupper($name);
// Uppercase, without extension
$attempt = sprintf('%s/%s', $projectPath, $name);
if(is_file($attempt))
return $attempt;
// Uppercase, with extension
$attempt = sprintf('%s/%s.md', $projectPath, $name);
if(is_file($attempt))
return $attempt;
$name = strtolower($name);
// Lowercase, without extension
$attempt = sprintf('%s/%s', $projectPath, $name);
if(is_file($attempt))
return $attempt;
// Lowercase, with extension
$attempt = sprintf('%s/%s.md', $projectPath, $name);
if(is_file($attempt))
return $attempt;
return '';
}
#[Route('GET', '/info/:project/:name')] #[Route('GET', '/info/:project/:name')]
public function getProjectPage($response, $request, string $project, string $name) { public function getProjectPage($response, $request, string $project, string $name) {
if(!array_key_exists($project, self::PROJECT_PATHS)) if(!array_key_exists($project, self::PROJECT_PATHS))
@ -46,37 +84,19 @@ class InfoRoutes extends RouteHandler {
$projectPath = self::PROJECT_PATHS[$project]; $projectPath = self::PROJECT_PATHS[$project];
$titleSuffix = array_key_exists($project, self::PROJECT_SUFFIXES) ? self::PROJECT_SUFFIXES[$project] : ''; $titleSuffix = array_key_exists($project, self::PROJECT_SUFFIXES) ? self::PROJECT_SUFFIXES[$project] : '';
$attempts = 0; $path = self::findDocPath($projectPath, $name);
$licenceHack = false;
for(;;) {
$path = match(++$attempts) {
1 => sprintf('%s/%s', $projectPath, $name),
2 => sprintf('%s/%s.md', $projectPath, $name),
3 => sprintf('%s/%s', $projectPath, strtoupper($name)),
4 => sprintf('%s/%s.md', $projectPath, strtoupper($name)),
5 => sprintf('%s/%s', $projectPath, strtolower($name)),
6 => sprintf('%s/%s.md', $projectPath, strtolower($name)),
default => '',
};
if($path === '') { if($path === '') {
if(!$licenceHack) { $isBritishLicense = strtolower($name) === 'licence';
$isBritish = strtolower($name) === 'licence'; $isAmericanLicence = strtolower($name) === 'license';
$isAmerican = strtolower($name) === 'license';
if($isBritish || $isAmerican) { if($isBritishLicense || $isAmericanLicence) {
$attempts = 0; $name = $isAmericanLicence ? 'licence' : 'license';
$licenceHack = true; $path = self::findDocPath($projectPath, $name);
$name = $isAmerican ? 'licence' : 'license';
continue;
}
}
if($path === '')
return 404;
} else
return 404; return 404;
}
if(is_file($path))
break;
} }
return $this->serveMarkdownDocument($path, $titleSuffix); return $this->serveMarkdownDocument($path, $titleSuffix);

View file

@ -1,6 +1,7 @@
<?php <?php
namespace Misuzu; namespace Misuzu;
use Index\Environment;
use Index\Data\IDbConnection; use Index\Data\IDbConnection;
use Index\Data\Migration\IDbMigrationRepo; use Index\Data\Migration\IDbMigrationRepo;
use Index\Data\Migration\DbMigrationManager; use Index\Data\Migration\DbMigrationManager;
@ -202,21 +203,21 @@ class MisuzuContext {
['eeprom.app:s', '', 'eeprom_app'], ['eeprom.app:s', '', 'eeprom_app'],
]); ]);
$isDebug = Environment::isDebug();
$globals['assets'] = $this->getWebAssetInfo(); $globals['assets'] = $this->getWebAssetInfo();
$globals['auth_info'] = $this->authInfo; $globals['auth_info'] = $this->authInfo;
$globals['active_ban_info'] = $this->usersCtx->tryGetActiveBan($this->authInfo->getUserInfo()); $globals['active_ban_info'] = $this->usersCtx->tryGetActiveBan($this->authInfo->getUserInfo());
$globals['display_timings_info'] = MSZ_DEBUG $globals['display_timings_info'] = $isDebug || $this->authInfo->getPerms('global')->check(Perm::G_TIMINGS_VIEW);
|| $this->authInfo->getPerms('global')->check(Perm::G_TIMINGS_VIEW);
$templating = new SasaeEnvironment( $this->templating = new SasaeEnvironment(
MSZ_TEMPLATES, MSZ_TEMPLATES,
cache: MSZ_DEBUG ? null : ['Misuzu', GitInfo::hash(true)], cache: $isDebug ? null : ['Misuzu', GitInfo::hash(true)],
debug: MSZ_DEBUG debug: $isDebug
); );
$templating->addExtension(new MisuzuSasaeExtension($this)); $this->templating->addExtension(new MisuzuSasaeExtension($this));
$templating->addGlobal('globals', $globals); $this->templating->addGlobal('globals', $globals);
Template::init($templating); Template::init($this->templating);
} }
public function startRouter(): void { public function startRouter(): void {

View file

@ -5,6 +5,7 @@ use stdClass;
use InvalidArgumentException; use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use Index\DateTime; use Index\DateTime;
use Index\Environment;
use Index\Data\DbStatementCache; use Index\Data\DbStatementCache;
use Index\Data\DbTools; use Index\Data\DbTools;
use Index\Data\IDbConnection; use Index\Data\IDbConnection;
@ -370,7 +371,7 @@ class Permissions {
} }
private static function precalculatePermissionsLog(string $fmt, ...$args): void { private static function precalculatePermissionsLog(string $fmt, ...$args): void {
if(!MSZ_CLI) if(!Environment::isConsole())
return; return;
echo DateTime::now()->format('[H:i:s.u] '); echo DateTime::now()->format('[H:i:s.u] ');