SONARPHP-1797 S4833: suppress false positives for require/include of non-namespaced files#1643
Merged
nils-werner-sonarsource merged 3 commits intomasterfrom Feb 27, 2026
Merged
Conversation
…no namespaced symbols The rule previously flagged all require/include calls for PHP files, even when the included file contained no namespaced class, function, or trait — making the `use` keyword replacement impossible. The fix uses the project-wide `ProjectSymbolData` (already built during analysis) to resolve the included file path and check whether any namespaced class is declared in that file. If none is found (or if the path is dynamic), the issue is suppressed. Dynamic paths and unknown/unresolvable files are treated as compliant. `SymbolTableImpl` now exposes a `projectSymbolData()` getter so checks can query the full project symbol index. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
…on logic With the new symbol-based approach, S4833 no longer flags require/include when the path is dynamic or the target file has no namespaced classes. Update ruling snapshots to remove false positives from: - Dynamic paths (__DIR__ concatenation, variables, expressions) - Static paths whose target files contain no namespaced symbols Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
karim-ouerghemmi-sonarsource
approved these changes
Feb 27, 2026
Contributor
karim-ouerghemmi-sonarsource
left a comment
There was a problem hiding this comment.
LGTM!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.




Summary
require/includeon files that contain no namespaced symbols (config arrays, HTML templates, procedural scripts), where theusekeyword cannot replace the includeProjectSymbolDatasymbol index: if the included file is not known to define any namespaced class, the issue is suppressed$varor__DIR__ . '/file.php') are now treated as compliant since the path cannot be resolvedSymbolTableImplexposes a newprojectSymbolData()getter to make the project symbol index accessible to checksImplementation approach
Under PSR-4/Composer conventions, every namespaced class lives in its own file. The
ProjectSymbolDataalready contains a map of all classes found across the project, each with its source file path (ClassSymbolData.location().filePath()). On first visit, the check builds aSet<String>of all such file paths once per analysis run. When arequire/includeis encountered, the included path is resolved relative to the current file and looked up in this set.Test plan
RequireIncludeInstructionsUsageCheckNamespaceDetection.phpcovers: files with namespace (Noncompliant), files without namespace (Compliant), non-existent files (Compliant), dynamic paths (Compliant), return-value-used (Compliant)test()updated to pass included files as additional analysis inputs soProjectSymbolDatais populated./gradlew :php-checks:test :php-frontend:testFixes: https://sonarsource.atlassian.net/browse/SONARPHP-1797
🤖 Generated with Claude Code