Skip to content

Commit

Permalink
Improved types around getenv() and implicit false casts
Browse files Browse the repository at this point in the history
Fixes:

```
ERROR: RiskyTruthyFalsyComparison
at src/DocbookPage.php:76:13
Operand of type false|non-empty-string contains type non-empty-string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
        if (! $firstLine || ! str_starts_with($firstLine, '<h1>') || ! str_ends_with($firstLine, '</h1>')) {

ERROR: RiskyTruthyFalsyComparison
at src/Environment.php:30:16
Operand of type false|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
        return getenv($name) ?: null;
```
  • Loading branch information
Ocramius committed Jan 18, 2024
1 parent b35977f commit cf44f5f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/DocbookPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private function determineTitleFromContent(): string
{
$firstLine = strtok($this->content, "\n");

if (! $firstLine || ! str_starts_with($firstLine, '<h1>') || ! str_ends_with($firstLine, '</h1>')) {
if ($firstLine === false || ! str_starts_with($firstLine, '<h1>') || ! str_ends_with($firstLine, '</h1>')) {
throw new RuntimeException('First line of markdown file ' . $this->slug() . ' did not start with "# "...');
}

Expand Down
7 changes: 6 additions & 1 deletion src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ public static function require(string $name): string
return $value;
}

/** @return non-empty-string|null */
public static function optional(string $name): string|null
{
return getenv($name) ?: null;
$value = getenv($name);

return $value === false || $value === ''
? null
: $value;
}

/** @param list<string> $trueValues */
Expand Down

0 comments on commit cf44f5f

Please # to comment.