-
Notifications
You must be signed in to change notification settings - Fork 16
Support stream_wrapper_register #266
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
Merged
janedbal
merged 5 commits into
master
from
claude/add-dead-code-detector-support-01NVmtLbA3x2nKU7BTkX1zUu
Nov 24, 2025
+179
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4f5fa59
Add RegisterCallbackUsageProvider for PHP callback registration funct…
claude eec9a4a
Fix RegisterCallbackUsageProvider - now fully working for stream_wrap…
claude 24e2c26
Add PHPStan ignore comments for array item access
claude d781969
Fix fatal error in register-callback test fixture
claude 5f8e5e1
Simplify implementation, keep only needed stream-wrapper support.
janedbal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace ShipMonk\PHPStan\DeadCode\Provider; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Name; | ||
| use PHPStan\Analyser\Scope; | ||
| use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodRef; | ||
| use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodUsage; | ||
| use ShipMonk\PHPStan\DeadCode\Graph\UsageOrigin; | ||
| use function in_array; | ||
|
|
||
| /** | ||
| * See: https://php.net/manual/en/class.streamwrapper.php | ||
| */ | ||
| class StreamWrapperUsageProvider implements MemberUsageProvider | ||
| { | ||
|
|
||
| private const STREAM_WRAPPER_METHODS = [ | ||
| 'dir_closedir', | ||
| 'dir_opendir', | ||
| 'dir_readdir', | ||
| 'dir_rewinddir', | ||
| 'mkdir', | ||
| 'rename', | ||
| 'rmdir', | ||
| 'stream_cast', | ||
| 'stream_close', | ||
| 'stream_eof', | ||
| 'stream_flush', | ||
| 'stream_lock', | ||
| 'stream_metadata', | ||
| 'stream_open', | ||
| 'stream_read', | ||
| 'stream_seek', | ||
| 'stream_set_option', | ||
| 'stream_stat', | ||
| 'stream_tell', | ||
| 'stream_truncate', | ||
| 'stream_write', | ||
| 'unlink', | ||
| 'url_stat', | ||
| ]; | ||
|
|
||
| private bool $enabled; | ||
|
|
||
| public function __construct( | ||
| bool $enabled | ||
| ) | ||
| { | ||
| $this->enabled = $enabled; | ||
| } | ||
|
|
||
| public function getUsages( | ||
| Node $node, | ||
| Scope $scope | ||
| ): array | ||
| { | ||
| if (!$this->enabled) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!$node instanceof FuncCall) { | ||
| return []; | ||
| } | ||
|
|
||
| return $this->processFunctionCall($node, $scope); | ||
| } | ||
|
|
||
| /** | ||
| * @return list<ClassMethodUsage> | ||
| */ | ||
| private function processFunctionCall( | ||
| FuncCall $node, | ||
| Scope $scope | ||
| ): array | ||
| { | ||
| $functionNames = $this->getFunctionNames($node, $scope); | ||
|
|
||
| if (in_array('stream_wrapper_register', $functionNames, true)) { | ||
| return $this->handleStreamWrapperRegister($node, $scope); | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * @return list<string> | ||
| */ | ||
| private function getFunctionNames( | ||
| FuncCall $node, | ||
| Scope $scope | ||
| ): array | ||
| { | ||
| if ($node->name instanceof Name) { | ||
| return [$node->name->toString()]; | ||
| } | ||
|
|
||
| $functionNames = []; | ||
| foreach ($scope->getType($node->name)->getConstantStrings() as $constantString) { | ||
| $functionNames[] = $constantString->getValue(); | ||
| } | ||
|
|
||
| return $functionNames; | ||
| } | ||
|
|
||
| /** | ||
| * @return list<ClassMethodUsage> | ||
| */ | ||
| private function handleStreamWrapperRegister( | ||
| FuncCall $node, | ||
| Scope $scope | ||
| ): array | ||
| { | ||
| $secondArg = $node->getArgs()[1] ?? null; | ||
| if ($secondArg === null) { | ||
| return []; | ||
| } | ||
|
|
||
| $argType = $scope->getType($secondArg->value); | ||
|
|
||
| $usages = []; | ||
| $classNames = []; | ||
| foreach ($argType->getConstantStrings() as $constantString) { | ||
| $classNames[] = $constantString->getValue(); | ||
| } | ||
|
|
||
| foreach ($classNames as $className) { | ||
| foreach (self::STREAM_WRAPPER_METHODS as $methodName) { | ||
| $usages[] = new ClassMethodUsage( | ||
| UsageOrigin::createRegular($node, $scope), | ||
| new ClassMethodRef( | ||
| $className, | ||
| $methodName, | ||
| false, | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return $usages; | ||
| } | ||
|
|
||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace StreamWrapper; | ||
|
|
||
| class MyStreamWrapper | ||
| { | ||
| public function stream_open($path, $mode, $options, &$opened_path) {} | ||
| public function stream_read($count) {} | ||
| public function unrelated($path, $mode, $options, &$opened_path) {} // error: Unused StreamWrapper\MyStreamWrapper::unrelated | ||
| } | ||
|
|
||
| function testAll() { | ||
| stream_wrapper_register('myprotocol', MyStreamWrapper::class); | ||
| } |
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.
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.
smart. I like it.