-
Notifications
You must be signed in to change notification settings - Fork 3
HP-2667: Do not prevent exception when name is empty #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughAdded input validation to Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant FileStorage
Note right of FileStorage: New/changed behavior: getFullPath may return null
Caller->>FileStorage: getFullPath(name)
alt name is empty or non-string
FileStorage-->>Caller: null
Caller->>FileStorage: has(name)
FileStorage-->>Caller: false
Caller->>FileStorage: remove(name)
FileStorage-->>Caller: true
else valid name
FileStorage-->>Caller: fullPath
Caller->>FileStorage: has(name)
FileStorage-->>Caller: file_exists(fullPath) result
Caller->>FileStorage: remove(name)
FileStorage-->>Caller: unlink(fullPath) result
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/FileStorage.php (2)
30-37: Reuse$pathinhas()and align null-handling withget()
has()computes$pathand then ignores it, callinggetFullPath($name)again. Also,get()doesn’t short‑circuit onnulllikehas()/remove(), which means it still callsis_file(null)for empty names. Reusing$pathand adding the same null guard inget()keeps behaviour consistent and avoids redundant work and warnings.public function has($name) { - $path = $this->getFullPath($name); - if ($path === null) { - return false; - } - return is_file($this->getFullPath($name)); + $path = $this->getFullPath($name); + if ($path === null) { + return false; + } + + return is_file($path); } public function get($name) { - $path = $this->getFullPath($name); - - return is_file($path) ? file_get_contents($this->getFullPath($name)) : null; + $path = $this->getFullPath($name); + if ($path === null) { + return null; + } + + return is_file($path) ? file_get_contents($path) : null; }Also applies to: 39-44
74-83:remove()returningtruefor empty/invalid names may hide bugsWith the new guard,
remove(null)/remove('')now returnstrue, signalling success even though nothing could be deleted and the input is arguably invalid. That can mask caller bugs and seems at odds with the idea of not hiding exceptional situations for empty names.You might want either to fail (e.g. throw
InvalidArgumentException) or at least returnfalseso callers can detect the problem.Example options:
- if ($path === null) { - return true; - } + if ($path === null) { + return false; + }or, if you prefer to treat this as programmer error:
- if ($path === null) { - return true; - } + if ($path === null) { + throw new \InvalidArgumentException('Name cannot be empty'); + }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/FileStorage.php(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/FileStorage.php (1)
src/StorageInterface.php (1)
has(15-15)
src/FileStorage.php
Outdated
|
|
||
| protected function getFullPath($name) | ||
| { | ||
| if ($name === null || $name === '' || !is_string($name)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is hard to read
if (is_string($name) && !empty(name)) {
return $this->path . '/' . $name[0] . '/' . $name;
}
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.