|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace ShipMonk\PHPStan\DeadCode\Provider; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\FuncCall; |
| 7 | +use PhpParser\Node\Name; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodRef; |
| 10 | +use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodUsage; |
| 11 | +use ShipMonk\PHPStan\DeadCode\Graph\UsageOrigin; |
| 12 | +use function in_array; |
| 13 | + |
| 14 | +/** |
| 15 | + * See: https://php.net/manual/en/class.streamwrapper.php |
| 16 | + */ |
| 17 | +class StreamWrapperUsageProvider implements MemberUsageProvider |
| 18 | +{ |
| 19 | + |
| 20 | + private const STREAM_WRAPPER_METHODS = [ |
| 21 | + 'dir_closedir', |
| 22 | + 'dir_opendir', |
| 23 | + 'dir_readdir', |
| 24 | + 'dir_rewinddir', |
| 25 | + 'mkdir', |
| 26 | + 'rename', |
| 27 | + 'rmdir', |
| 28 | + 'stream_cast', |
| 29 | + 'stream_close', |
| 30 | + 'stream_eof', |
| 31 | + 'stream_flush', |
| 32 | + 'stream_lock', |
| 33 | + 'stream_metadata', |
| 34 | + 'stream_open', |
| 35 | + 'stream_read', |
| 36 | + 'stream_seek', |
| 37 | + 'stream_set_option', |
| 38 | + 'stream_stat', |
| 39 | + 'stream_tell', |
| 40 | + 'stream_truncate', |
| 41 | + 'stream_write', |
| 42 | + 'unlink', |
| 43 | + 'url_stat', |
| 44 | + ]; |
| 45 | + |
| 46 | + private bool $enabled; |
| 47 | + |
| 48 | + public function __construct( |
| 49 | + bool $enabled |
| 50 | + ) |
| 51 | + { |
| 52 | + $this->enabled = $enabled; |
| 53 | + } |
| 54 | + |
| 55 | + public function getUsages( |
| 56 | + Node $node, |
| 57 | + Scope $scope |
| 58 | + ): array |
| 59 | + { |
| 60 | + if (!$this->enabled) { |
| 61 | + return []; |
| 62 | + } |
| 63 | + |
| 64 | + if (!$node instanceof FuncCall) { |
| 65 | + return []; |
| 66 | + } |
| 67 | + |
| 68 | + return $this->processFunctionCall($node, $scope); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return list<ClassMethodUsage> |
| 73 | + */ |
| 74 | + private function processFunctionCall( |
| 75 | + FuncCall $node, |
| 76 | + Scope $scope |
| 77 | + ): array |
| 78 | + { |
| 79 | + $functionNames = $this->getFunctionNames($node, $scope); |
| 80 | + |
| 81 | + if (in_array('stream_wrapper_register', $functionNames, true)) { |
| 82 | + return $this->handleStreamWrapperRegister($node, $scope); |
| 83 | + } |
| 84 | + |
| 85 | + return []; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @return list<string> |
| 90 | + */ |
| 91 | + private function getFunctionNames( |
| 92 | + FuncCall $node, |
| 93 | + Scope $scope |
| 94 | + ): array |
| 95 | + { |
| 96 | + if ($node->name instanceof Name) { |
| 97 | + return [$node->name->toString()]; |
| 98 | + } |
| 99 | + |
| 100 | + $functionNames = []; |
| 101 | + foreach ($scope->getType($node->name)->getConstantStrings() as $constantString) { |
| 102 | + $functionNames[] = $constantString->getValue(); |
| 103 | + } |
| 104 | + |
| 105 | + return $functionNames; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return list<ClassMethodUsage> |
| 110 | + */ |
| 111 | + private function handleStreamWrapperRegister( |
| 112 | + FuncCall $node, |
| 113 | + Scope $scope |
| 114 | + ): array |
| 115 | + { |
| 116 | + $secondArg = $node->getArgs()[1] ?? null; |
| 117 | + if ($secondArg === null) { |
| 118 | + return []; |
| 119 | + } |
| 120 | + |
| 121 | + $argType = $scope->getType($secondArg->value); |
| 122 | + |
| 123 | + $usages = []; |
| 124 | + $classNames = []; |
| 125 | + foreach ($argType->getConstantStrings() as $constantString) { |
| 126 | + $classNames[] = $constantString->getValue(); |
| 127 | + } |
| 128 | + |
| 129 | + foreach ($classNames as $className) { |
| 130 | + foreach (self::STREAM_WRAPPER_METHODS as $methodName) { |
| 131 | + $usages[] = new ClassMethodUsage( |
| 132 | + UsageOrigin::createRegular($node, $scope), |
| 133 | + new ClassMethodRef( |
| 134 | + $className, |
| 135 | + $methodName, |
| 136 | + false, |
| 137 | + ), |
| 138 | + ); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return $usages; |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments