|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace phpseclib\phpseclib3Rector; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Name; |
| 9 | +use PhpParser\NodeTraverser; |
| 10 | +use PhpParser\Node\Expr\Assign; |
| 11 | +use PhpParser\Node\Expr\FuncCall; |
| 12 | +use PhpParser\Node\Expr\New_; |
| 13 | +use PhpParser\Node\Expr\StaticCall; |
| 14 | +use PhpParser\Node\Expr\Variable; |
| 15 | +use PhpParser\Node\Expr\MethodCall; |
| 16 | +use PhpParser\Node\Expr\Cast\String_; |
| 17 | +use PhpParser\Node\Stmt\UseUse; |
| 18 | +use PhpParser\Node\Stmt\Expression; |
| 19 | + |
| 20 | +use Rector\Rector\AbstractRector; |
| 21 | + |
| 22 | +final class CreateKey extends AbstractRector |
| 23 | +{ |
| 24 | + private ?bool $hasExtract = null; |
| 25 | + private ?string $currentFilePath = null; |
| 26 | + private array $rsaVarsUsedInExtract = []; |
| 27 | + |
| 28 | + public function getNodeTypes(): array |
| 29 | + { |
| 30 | + return [ |
| 31 | + UseUse::class, |
| 32 | + Expression::class, |
| 33 | + ]; |
| 34 | + } |
| 35 | + |
| 36 | + // We only continue when there is an extract(->createKey()) |
| 37 | + private function containsExtract(array $nodes): bool |
| 38 | + { |
| 39 | + foreach ($nodes as $node) { |
| 40 | + if ($node instanceof Expression && ($node->expr instanceof FuncCall || $node->expr instanceof MethodCall)) { |
| 41 | + $funcName = $node->expr->name; |
| 42 | + if ($funcName instanceof Name && $funcName->toString() === 'extract') { |
| 43 | + $arg = $node->expr->args[0]->value ?? null; |
| 44 | + |
| 45 | + if ( |
| 46 | + $arg instanceof MethodCall && |
| 47 | + $this->isName($arg->name, 'createKey') && |
| 48 | + $arg->var instanceof Variable |
| 49 | + ) { |
| 50 | + return true; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Recurse into class/method bodies |
| 56 | + if (property_exists($node, 'stmts') && is_array($node->stmts)) { |
| 57 | + if ($this->containsExtract($node->stmts)) { |
| 58 | + return true; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + public function refactor(Node $node): null|Node|int|array |
| 66 | + { |
| 67 | + $filePath = $this->file->getFilePath(); |
| 68 | + if ($this->currentFilePath !== $filePath) { |
| 69 | + $this->hasExtract = null; |
| 70 | + $this->currentFilePath = $filePath; |
| 71 | + } |
| 72 | + |
| 73 | + if ($this->hasExtract === null) { |
| 74 | + $this->hasExtract = $this->containsExtract($this->file->getNewStmts()); |
| 75 | + } |
| 76 | + |
| 77 | + // This is a job for PublicKeyLoaderRule |
| 78 | + if (! $this->hasExtract) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + // Detect extract($rsa->createKey(...)) |
| 83 | + if ( |
| 84 | + $node instanceof Expression && |
| 85 | + $node->expr instanceof FuncCall && |
| 86 | + $this->isName($node->expr->name, 'extract') |
| 87 | + ) { |
| 88 | + $arg = $node->expr->args[0]->value ?? null; |
| 89 | + if ( |
| 90 | + $arg instanceof MethodCall && |
| 91 | + $this->isName($arg->name, 'createKey') && |
| 92 | + $arg->var instanceof Variable && |
| 93 | + is_string($arg->var->name) |
| 94 | + ) { |
| 95 | + $this->rsaVarsUsedInExtract[$arg->var->name] = true; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Replace use phpseclib\Crypt\RSA -> use phpseclib3\Crypt\RSA |
| 100 | + if ($node instanceof UseUse) { |
| 101 | + if ($this->isName($node->name, 'phpseclib\Crypt\RSA')) { |
| 102 | + $node->name = new Name('phpseclib3\Crypt\RSA'); |
| 103 | + $node->alias = null; |
| 104 | + return $node; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (! $node instanceof Expression) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + $expr = $node->expr; |
| 112 | + |
| 113 | + // Remove new RSA() assignment |
| 114 | + if ($expr instanceof Assign && |
| 115 | + $expr->var instanceof Variable && |
| 116 | + $expr->expr instanceof New_ && |
| 117 | + $this->isName($node->expr->expr->class, 'phpseclib\Crypt\RSA') |
| 118 | + ) { |
| 119 | + $varName = is_string($expr->var->name) ? $expr->var->name : null; |
| 120 | + // new RSA() is removed only when it is used in extract() |
| 121 | + if ($varName !== null && isset($this->rsaVarsUsedInExtract[$varName])) { |
| 122 | + return NodeTraverser::REMOVE_NODE; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Match: extract(...) |
| 127 | + if (! $expr instanceof FuncCall) { |
| 128 | + return null; |
| 129 | + } |
| 130 | + if (! $this->isName($expr->name, 'extract')) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + // extract() must have arguments |
| 134 | + if (! isset($expr->args[0])) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + // Argument must be a method -> $rsa->createKey(...) |
| 138 | + $arg = $expr->args[0]->value; |
| 139 | + if (! ($arg instanceof MethodCall) || ! ($this->isName($arg->name, 'createKey'))) { |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + // Extract variable name from: $rsa->createKey(...) |
| 144 | + $varName = $arg->var instanceof Variable ? $arg->var->name : null; |
| 145 | + if ( |
| 146 | + ! is_string($varName) || |
| 147 | + ! isset($this->rsaVarsUsedInExtract[$varName]) |
| 148 | + ) { |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + // refactor to $rsa = RSA::createKey(2048); |
| 153 | + $originalPrivateKeyExpr = new Expression( |
| 154 | + new Assign( |
| 155 | + // or $varName if you want to keep the original one |
| 156 | + new Variable('privateKey'), |
| 157 | + new StaticCall( |
| 158 | + new Name('RSA'), |
| 159 | + 'createKey', |
| 160 | + $arg->args |
| 161 | + ) |
| 162 | + ) |
| 163 | + ); |
| 164 | + |
| 165 | + // $publickey = $privatekey->getPublicKey(); |
| 166 | + $publicKeyExpr = new Expression( |
| 167 | + new Assign( |
| 168 | + new Variable('publicKey'), |
| 169 | + new MethodCall( |
| 170 | + new Variable('privateKey'), |
| 171 | + 'getPublicKey', |
| 172 | + ) |
| 173 | + ) |
| 174 | + ); |
| 175 | + |
| 176 | + // $privatekey = (string) $privatekey; |
| 177 | + $privateKeyString = new Expression( |
| 178 | + new Assign( |
| 179 | + new Variable('privateKey'), |
| 180 | + new String_( |
| 181 | + new Variable('privateKey'), |
| 182 | + ) |
| 183 | + ) |
| 184 | + ); |
| 185 | + // $publickey = (string) $publickey; |
| 186 | + $publicKeySting = new Expression( |
| 187 | + new Assign( |
| 188 | + new Variable('publicKey'), |
| 189 | + new String_( |
| 190 | + new Variable('publicKey'), |
| 191 | + ) |
| 192 | + ) |
| 193 | + ); |
| 194 | + |
| 195 | + return [ |
| 196 | + $originalPrivateKeyExpr, |
| 197 | + $publicKeyExpr, |
| 198 | + $privateKeyString, |
| 199 | + $publicKeySting, |
| 200 | + ]; |
| 201 | + } |
| 202 | +} |
0 commit comments