Skip to content

Commit

Permalink
Improve types, handle nullable paths in a few more places.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Nov 29, 2024
1 parent ecdcccb commit bb76472
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
10 changes: 3 additions & 7 deletions src/ConfigPaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ public function overrideDirs(array $overrides)

/**
* Get the current home directory.
*
* @return string|null
*/
public function homeDir()
public function homeDir(): ?string
{
if ($homeDir = $this->getEnv('HOME') ?: $this->windowsHomeDir()) {
return \strtr($homeDir, '\\', '/');
Expand All @@ -75,7 +73,7 @@ public function homeDir()
return null;
}

private function windowsHomeDir()
private function windowsHomeDir(): ?string
{
if (\defined('PHP_WINDOWS_VERSION_MAJOR')) {
$homeDrive = $this->getEnv('HOMEDRIVE');
Expand Down Expand Up @@ -234,10 +232,8 @@ public function pathDirs(): array
* If $PATH is unset/empty it defaults to '/usr/sbin:/usr/bin:/sbin:/bin'.
*
* @param string $command the executable to locate
*
* @return string
*/
public function which($command)
public function which($command): ?string
{
foreach ($this->pathDirs() as $path) {
$fullpath = $path.\DIRECTORY_SEPARATOR.$command;
Expand Down
14 changes: 10 additions & 4 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,11 +676,12 @@ public function getHistoryFile(): ?string
$this->setHistoryFile($files[0]);
} else {
// fallback: create our own history file
$currentConfDir = $this->configPaths->currentConfigDir();
if ($currentConfDir === null) {
$configDir = $this->configPaths->currentConfigDir();
if ($configDir === null) {
return null;
}
$this->setHistoryFile($this->configPaths->currentConfigDir().'/psysh_history');

$this->setHistoryFile($configDir.'/psysh_history');
}

return $this->historyFile;
Expand Down Expand Up @@ -1658,7 +1659,12 @@ public function setUpdateCheck(string $interval)
*/
public function getUpdateCheckCacheFile()
{
return ConfigPaths::touchFileWithMkdir($this->configPaths->currentConfigDir().'/update_check.json');
$configDir = $this->configPaths->currentConfigDir();
if ($configDir === null) {
return false;
}

return ConfigPaths::touchFileWithMkdir($configDir.'/update_check.json');
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Readline/Libedit.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public static function supportsBracketedPaste(): bool
*/
public function listHistory(): array
{
if ($this->historyFile === false) {
return [];
}

$history = \file_get_contents($this->historyFile);
if (!$history) {
return [];
Expand Down

0 comments on commit bb76472

Please sign in to comment.