Add PHPStan query contract checks#104
Conversation
📝 WalkthroughWalkthroughThis PR adds a PHPStan extension for Ray.MediaQuery that enforces query contract validation through attributes. The extension scans decorated methods for ChangesPHPStan Rule Extension for Query Contracts
🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 PHPStan (2.2.1)PHPStan was skipped because the config uses disallowed 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.
🧹 Nitpick comments (1)
src-sa/src/Support/SqlFileResolver.php (1)
35-46: 💤 Low valueConsider guarding against
getcwd()failure.
getcwd()returnsstring|falsebut is cast to string without checking for failure. If the current working directory becomes inaccessible, castingfalseto string produces an empty string, leading to incorrect path resolution.While this scenario is rare in static analysis contexts, adding a guard would improve robustness.
🛡️ Defensive handling option
private function absolutePath(string $path): string { + $cwd = getcwd(); + if ($cwd === false) { + throw new \RuntimeException('Unable to determine current working directory'); + } + if ($path === '') { - return (string) getcwd(); + return $cwd; } if (preg_match('#^([A-Za-z]:)?/#', $path) === 1) { return $path; } - return (string) getcwd() . '/' . ltrim($path, '/'); + return $cwd . '/' . ltrim($path, '/'); }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src-sa/src/Support/SqlFileResolver.php` around lines 35 - 46, The absolutePath method should guard against getcwd() returning false: in absolutePath(string $path) call getcwd() once into a variable, check if it === false and handle it (throw a RuntimeException or use a sensible fallback like dirname(__DIR__) or PATH_ROOT) before casting/concatenating; update both the empty-path branch and the final return to use this safe cwd value and keep the existing path normalization (preg_match and ltrim) unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src-sa/src/Support/SqlFileResolver.php`:
- Around line 35-46: The absolutePath method should guard against getcwd()
returning false: in absolutePath(string $path) call getcwd() once into a
variable, check if it === false and handle it (throw a RuntimeException or use a
sensible fallback like dirname(__DIR__) or PATH_ROOT) before
casting/concatenating; update both the empty-path branch and the final return to
use this safe cwd value and keep the existing path normalization (preg_match and
ltrim) unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: dd2d0acb-1bb3-484d-893c-5138240779e2
📒 Files selected for processing (24)
.gitignorecomposer-require-checker.jsoncomposer.jsonphpcs.xmlphpstan.neonsrc-sa/README.mdsrc-sa/extension.neonsrc-sa/phpunit.xml.distsrc-sa/src/Rules/DbQueryContractRule.phpsrc-sa/src/Support/DbQueryMetadata.phpsrc-sa/src/Support/NamedParameterExtractor.phpsrc-sa/src/Support/SqlFileResolver.phpsrc-sa/tests/Rules/Data/FactoryWithoutFactoryMethod.phpsrc-sa/tests/Rules/Data/ValidInstanceFactory.phpsrc-sa/tests/Rules/Data/ValidPostQueryResult.phpsrc-sa/tests/Rules/Data/ValidStaticFactory.phpsrc-sa/tests/Rules/Data/invalid.phpsrc-sa/tests/Rules/Data/sql/empty.sqlsrc-sa/tests/Rules/Data/sql/valid.sqlsrc-sa/tests/Rules/Data/valid.phpsrc-sa/tests/Rules/DbQueryContractRuleTest.phpsrc-sa/tests/Support/NamedParameterExtractorTest.phpsrc-sa/tests/bootstrap.phpsrc-sa/tests/phpstan-rule-tests.neon
Summary
#[DbQuery]contracts undersrc-saSelf-review
git diff --checkTests
composer test:phpstancomposer sacomposer testscomposer validate --no-check-all --strictvendor-bin/tools/vendor/bin/composer-require-checker check ./composer.json --config-file=./composer-require-checker.jsonSummary by CodeRabbit
New Features
Documentation
Chores