| Total Complexity | 293 |
| Total Lines | 1061 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PhpRender often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PhpRender, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class PhpRender extends PrettyPrinterAbstract |
||
| 23 | { |
||
| 24 | // Special nodes |
||
| 25 | |||
| 26 | protected function pParam(Node\Param $node) { |
||
| 27 | return ($this->pModifiers($node->flags)) |
||
| 28 | . ($node->type ? $this->p($node->type) . ' ' : '') |
||
| 29 | . ($node->byRef ? '&' : '') |
||
| 30 | . ($node->variadic ? '...' : '') |
||
| 31 | . $this->p($node->var) |
||
| 32 | . ($node->default ? ' = ' . $this->p($node->default) : ''); |
||
| 33 | } |
||
| 34 | |||
| 35 | protected function pArg(Node\Arg $node) { |
||
| 36 | return ($node->name ? $node->name->toString() . ': ' : '') |
||
| 37 | . ($node->byRef ? '&' : '') . ($node->unpack ? '...' : '') |
||
| 38 | . $this->p($node->value); |
||
| 39 | } |
||
| 40 | |||
| 41 | protected function pConst(Node\Const_ $node) { |
||
| 42 | return $node->name . ' = ' . $this->p($node->value); |
||
| 43 | } |
||
| 44 | |||
| 45 | protected function pNullableType(Node\NullableType $node) { |
||
| 46 | return '?' . $this->p($node->type); |
||
| 47 | } |
||
| 48 | |||
| 49 | protected function pUnionType(Node\UnionType $node) { |
||
| 50 | return $this->pImplode($node->types, '|'); |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function pIdentifier(Node\Identifier $node) { |
||
| 54 | return $node->name; |
||
| 55 | } |
||
| 56 | |||
| 57 | protected function pVarLikeIdentifier(Node\VarLikeIdentifier $node) { |
||
| 58 | return '$' . $node->name; |
||
| 59 | } |
||
| 60 | |||
| 61 | // Names |
||
| 62 | |||
| 63 | protected function pName(Name $node) { |
||
| 64 | return implode('\\', $node->parts); |
||
| 65 | } |
||
| 66 | |||
| 67 | protected function pName_FullyQualified(Name\FullyQualified $node) { |
||
| 68 | return '\\' . implode('\\', $node->parts); |
||
| 69 | } |
||
| 70 | |||
| 71 | protected function pName_Relative(Name\Relative $node) { |
||
| 72 | return 'namespace\\' . implode('\\', $node->parts); |
||
| 73 | } |
||
| 74 | |||
| 75 | // Magic Constants |
||
| 76 | |||
| 77 | protected function pScalar_MagicConst_Class(MagicConst\Class_ $node) { |
||
|
|
|||
| 78 | return '__CLASS__'; |
||
| 79 | } |
||
| 80 | |||
| 81 | protected function pScalar_MagicConst_Dir(MagicConst\Dir $node) { |
||
| 82 | return '__DIR__'; |
||
| 83 | } |
||
| 84 | |||
| 85 | protected function pScalar_MagicConst_File(MagicConst\File $node) { |
||
| 86 | return '__FILE__'; |
||
| 87 | } |
||
| 88 | |||
| 89 | protected function pScalar_MagicConst_Function(MagicConst\Function_ $node) { |
||
| 90 | return '__FUNCTION__'; |
||
| 91 | } |
||
| 92 | |||
| 93 | protected function pScalar_MagicConst_Line(MagicConst\Line $node) { |
||
| 94 | return '__LINE__'; |
||
| 95 | } |
||
| 96 | |||
| 97 | protected function pScalar_MagicConst_Method(MagicConst\Method $node) { |
||
| 98 | return '__METHOD__'; |
||
| 99 | } |
||
| 100 | |||
| 101 | protected function pScalar_MagicConst_Namespace(MagicConst\Namespace_ $node) { |
||
| 102 | return '__NAMESPACE__'; |
||
| 103 | } |
||
| 104 | |||
| 105 | protected function pScalar_MagicConst_Trait(MagicConst\Trait_ $node) { |
||
| 106 | return '__TRAIT__'; |
||
| 107 | } |
||
| 108 | |||
| 109 | // Scalars |
||
| 110 | |||
| 111 | protected function pScalar_String(Scalar\String_ $node) { |
||
| 112 | $kind = $node->getAttribute('kind', Scalar\String_::KIND_SINGLE_QUOTED); |
||
| 113 | switch ($kind) { |
||
| 114 | case Scalar\String_::KIND_NOWDOC: |
||
| 115 | $label = $node->getAttribute('docLabel'); |
||
| 116 | if ($label && !$this->containsEndLabel($node->value, $label)) { |
||
| 117 | if ($node->value === '') { |
||
| 118 | return "<<<'$label'\n$label" . $this->docStringEndToken; |
||
| 119 | } |
||
| 120 | |||
| 121 | return "<<<'$label'\n$node->value\n$label" |
||
| 122 | . $this->docStringEndToken; |
||
| 123 | } |
||
| 124 | /* break missing intentionally */ |
||
| 125 | case Scalar\String_::KIND_SINGLE_QUOTED: |
||
| 126 | return $this->pSingleQuotedString($node->value); |
||
| 127 | case Scalar\String_::KIND_HEREDOC: |
||
| 128 | $label = $node->getAttribute('docLabel'); |
||
| 129 | if ($label && !$this->containsEndLabel($node->value, $label)) { |
||
| 130 | if ($node->value === '') { |
||
| 131 | return "<<<$label\n$label" . $this->docStringEndToken; |
||
| 132 | } |
||
| 133 | |||
| 134 | $escaped = $this->escapeString($node->value, null); |
||
| 135 | return "<<<$label\n" . $escaped . "\n$label" |
||
| 136 | . $this->docStringEndToken; |
||
| 137 | } |
||
| 138 | /* break missing intentionally */ |
||
| 139 | case Scalar\String_::KIND_DOUBLE_QUOTED: |
||
| 140 | return '"' . $this->escapeString($node->value, '"') . '"'; |
||
| 141 | } |
||
| 142 | throw new \Exception('Invalid string kind'); |
||
| 143 | } |
||
| 144 | |||
| 145 | protected function pScalar_Encapsed(Scalar\Encapsed $node) { |
||
| 146 | if ($node->getAttribute('kind') === Scalar\String_::KIND_HEREDOC) { |
||
| 147 | $label = $node->getAttribute('docLabel'); |
||
| 148 | if ($label && !$this->encapsedContainsEndLabel($node->parts, $label)) { |
||
| 149 | if (count($node->parts) === 1 |
||
| 150 | && $node->parts[0] instanceof Scalar\EncapsedStringPart |
||
| 151 | && $node->parts[0]->value === '' |
||
| 152 | ) { |
||
| 153 | return "<<<$label\n$label" . $this->docStringEndToken; |
||
| 154 | } |
||
| 155 | |||
| 156 | return "<<<$label\n" . $this->pEncapsList($node->parts, null) . "\n$label" |
||
| 157 | . $this->docStringEndToken; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | return '"' . $this->pEncapsList($node->parts, '"') . '"'; |
||
| 161 | } |
||
| 162 | |||
| 163 | protected function pScalar_LNumber(Scalar\LNumber $node) { |
||
| 164 | if ($node->value === -\PHP_INT_MAX-1) { |
||
| 165 | // PHP_INT_MIN cannot be represented as a literal, |
||
| 166 | // because the sign is not part of the literal |
||
| 167 | return '(-' . \PHP_INT_MAX . '-1)'; |
||
| 168 | } |
||
| 169 | |||
| 170 | $kind = $node->getAttribute('kind', Scalar\LNumber::KIND_DEC); |
||
| 171 | if (Scalar\LNumber::KIND_DEC === $kind) { |
||
| 172 | return (string) $node->value; |
||
| 173 | } |
||
| 174 | |||
| 175 | if ($node->value < 0) { |
||
| 176 | $sign = '-'; |
||
| 177 | $str = (string) -$node->value; |
||
| 178 | } else { |
||
| 179 | $sign = ''; |
||
| 180 | $str = (string) $node->value; |
||
| 181 | } |
||
| 182 | switch ($kind) { |
||
| 183 | case Scalar\LNumber::KIND_BIN: |
||
| 184 | return $sign . '0b' . base_convert($str, 10, 2); |
||
| 185 | case Scalar\LNumber::KIND_OCT: |
||
| 186 | return $sign . '0' . base_convert($str, 10, 8); |
||
| 187 | case Scalar\LNumber::KIND_HEX: |
||
| 188 | return $sign . '0x' . base_convert($str, 10, 16); |
||
| 189 | } |
||
| 190 | throw new \Exception('Invalid number kind'); |
||
| 191 | } |
||
| 192 | |||
| 193 | protected function pScalar_DNumber(Scalar\DNumber $node) { |
||
| 194 | if (!is_finite($node->value)) { |
||
| 195 | if ($node->value === \INF) { |
||
| 196 | return '\INF'; |
||
| 197 | } elseif ($node->value === -\INF) { |
||
| 198 | return '-\INF'; |
||
| 199 | } else { |
||
| 200 | return '\NAN'; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | // Try to find a short full-precision representation |
||
| 205 | $stringValue = sprintf('%.16G', $node->value); |
||
| 206 | if ($node->value !== (double) $stringValue) { |
||
| 207 | $stringValue = sprintf('%.17G', $node->value); |
||
| 208 | } |
||
| 209 | |||
| 210 | // %G is locale dependent and there exists no locale-independent alternative. We don't want |
||
| 211 | // mess with switching locales here, so let's assume that a comma is the only non-standard |
||
| 212 | // decimal separator we may encounter... |
||
| 213 | $stringValue = str_replace(',', '.', $stringValue); |
||
| 214 | |||
| 215 | // ensure that number is really printed as float |
||
| 216 | return preg_match('/^-?[0-9]+$/', $stringValue) ? $stringValue . '.0' : $stringValue; |
||
| 217 | } |
||
| 218 | |||
| 219 | protected function pScalar_EncapsedStringPart(Scalar\EncapsedStringPart $node) { |
||
| 220 | throw new \LogicException('Cannot directly print EncapsedStringPart'); |
||
| 221 | } |
||
| 222 | |||
| 223 | // Assignments |
||
| 224 | |||
| 225 | protected function pExpr_Assign(Expr\Assign $node) { |
||
| 226 | return $this->pInfixOp(Expr\Assign::class, $node->var, ' = ', $node->expr); |
||
| 227 | } |
||
| 228 | |||
| 229 | protected function pExpr_AssignRef(Expr\AssignRef $node) { |
||
| 230 | return $this->pInfixOp(Expr\AssignRef::class, $node->var, ' =& ', $node->expr); |
||
| 231 | } |
||
| 232 | |||
| 233 | protected function pExpr_AssignOp_Plus(AssignOp\Plus $node) { |
||
| 234 | return $this->pInfixOp(AssignOp\Plus::class, $node->var, ' += ', $node->expr); |
||
| 235 | } |
||
| 236 | |||
| 237 | protected function pExpr_AssignOp_Minus(AssignOp\Minus $node) { |
||
| 238 | return $this->pInfixOp(AssignOp\Minus::class, $node->var, ' -= ', $node->expr); |
||
| 239 | } |
||
| 240 | |||
| 241 | protected function pExpr_AssignOp_Mul(AssignOp\Mul $node) { |
||
| 242 | return $this->pInfixOp(AssignOp\Mul::class, $node->var, ' *= ', $node->expr); |
||
| 243 | } |
||
| 244 | |||
| 245 | protected function pExpr_AssignOp_Div(AssignOp\Div $node) { |
||
| 246 | return $this->pInfixOp(AssignOp\Div::class, $node->var, ' /= ', $node->expr); |
||
| 247 | } |
||
| 248 | |||
| 249 | protected function pExpr_AssignOp_Concat(AssignOp\Concat $node) { |
||
| 250 | return $this->pInfixOp(AssignOp\Concat::class, $node->var, ' .= ', $node->expr); |
||
| 251 | } |
||
| 252 | |||
| 253 | protected function pExpr_AssignOp_Mod(AssignOp\Mod $node) { |
||
| 254 | return $this->pInfixOp(AssignOp\Mod::class, $node->var, ' %= ', $node->expr); |
||
| 255 | } |
||
| 256 | |||
| 257 | protected function pExpr_AssignOp_BitwiseAnd(AssignOp\BitwiseAnd $node) { |
||
| 259 | } |
||
| 260 | |||
| 261 | protected function pExpr_AssignOp_BitwiseOr(AssignOp\BitwiseOr $node) { |
||
| 262 | return $this->pInfixOp(AssignOp\BitwiseOr::class, $node->var, ' |= ', $node->expr); |
||
| 263 | } |
||
| 264 | |||
| 265 | protected function pExpr_AssignOp_BitwiseXor(AssignOp\BitwiseXor $node) { |
||
| 266 | return $this->pInfixOp(AssignOp\BitwiseXor::class, $node->var, ' ^= ', $node->expr); |
||
| 267 | } |
||
| 268 | |||
| 269 | protected function pExpr_AssignOp_ShiftLeft(AssignOp\ShiftLeft $node) { |
||
| 270 | return $this->pInfixOp(AssignOp\ShiftLeft::class, $node->var, ' <<= ', $node->expr); |
||
| 271 | } |
||
| 272 | |||
| 273 | protected function pExpr_AssignOp_ShiftRight(AssignOp\ShiftRight $node) { |
||
| 274 | return $this->pInfixOp(AssignOp\ShiftRight::class, $node->var, ' >>= ', $node->expr); |
||
| 275 | } |
||
| 276 | |||
| 277 | protected function pExpr_AssignOp_Pow(AssignOp\Pow $node) { |
||
| 278 | return $this->pInfixOp(AssignOp\Pow::class, $node->var, ' **= ', $node->expr); |
||
| 279 | } |
||
| 280 | |||
| 281 | protected function pExpr_AssignOp_Coalesce(AssignOp\Coalesce $node) { |
||
| 282 | return $this->pInfixOp(AssignOp\Coalesce::class, $node->var, ' ??= ', $node->expr); |
||
| 283 | } |
||
| 284 | |||
| 285 | // Binary expressions |
||
| 286 | |||
| 287 | protected function pExpr_BinaryOp_Plus(BinaryOp\Plus $node) { |
||
| 288 | return $this->pInfixOp(BinaryOp\Plus::class, $node->left, ' + ', $node->right); |
||
| 289 | } |
||
| 290 | |||
| 291 | protected function pExpr_BinaryOp_Minus(BinaryOp\Minus $node) { |
||
| 292 | return $this->pInfixOp(BinaryOp\Minus::class, $node->left, ' - ', $node->right); |
||
| 293 | } |
||
| 294 | |||
| 295 | protected function pExpr_BinaryOp_Mul(BinaryOp\Mul $node) { |
||
| 296 | return $this->pInfixOp(BinaryOp\Mul::class, $node->left, ' * ', $node->right); |
||
| 297 | } |
||
| 298 | |||
| 299 | protected function pExpr_BinaryOp_Div(BinaryOp\Div $node) { |
||
| 300 | return $this->pInfixOp(BinaryOp\Div::class, $node->left, ' / ', $node->right); |
||
| 301 | } |
||
| 302 | |||
| 303 | protected function pExpr_BinaryOp_Concat(BinaryOp\Concat $node) { |
||
| 304 | return $this->pInfixOp(BinaryOp\Concat::class, $node->left, ' . ', $node->right); |
||
| 305 | } |
||
| 306 | |||
| 307 | protected function pExpr_BinaryOp_Mod(BinaryOp\Mod $node) { |
||
| 308 | return $this->pInfixOp(BinaryOp\Mod::class, $node->left, ' % ', $node->right); |
||
| 309 | } |
||
| 310 | |||
| 311 | protected function pExpr_BinaryOp_BooleanAnd(BinaryOp\BooleanAnd $node) { |
||
| 312 | return $this->pInfixOp(BinaryOp\BooleanAnd::class, $node->left, ' && ', $node->right); |
||
| 313 | } |
||
| 314 | |||
| 315 | protected function pExpr_BinaryOp_BooleanOr(BinaryOp\BooleanOr $node) { |
||
| 316 | return $this->pInfixOp(BinaryOp\BooleanOr::class, $node->left, ' || ', $node->right); |
||
| 317 | } |
||
| 318 | |||
| 319 | protected function pExpr_BinaryOp_BitwiseAnd(BinaryOp\BitwiseAnd $node) { |
||
| 320 | return $this->pInfixOp(BinaryOp\BitwiseAnd::class, $node->left, ' & ', $node->right); |
||
| 321 | } |
||
| 322 | |||
| 323 | protected function pExpr_BinaryOp_BitwiseOr(BinaryOp\BitwiseOr $node) { |
||
| 324 | return $this->pInfixOp(BinaryOp\BitwiseOr::class, $node->left, ' | ', $node->right); |
||
| 325 | } |
||
| 326 | |||
| 327 | protected function pExpr_BinaryOp_BitwiseXor(BinaryOp\BitwiseXor $node) { |
||
| 328 | return $this->pInfixOp(BinaryOp\BitwiseXor::class, $node->left, ' ^ ', $node->right); |
||
| 329 | } |
||
| 330 | |||
| 331 | protected function pExpr_BinaryOp_ShiftLeft(BinaryOp\ShiftLeft $node) { |
||
| 332 | return $this->pInfixOp(BinaryOp\ShiftLeft::class, $node->left, ' << ', $node->right); |
||
| 333 | } |
||
| 334 | |||
| 335 | protected function pExpr_BinaryOp_ShiftRight(BinaryOp\ShiftRight $node) { |
||
| 336 | return $this->pInfixOp(BinaryOp\ShiftRight::class, $node->left, ' >> ', $node->right); |
||
| 337 | } |
||
| 338 | |||
| 339 | protected function pExpr_BinaryOp_Pow(BinaryOp\Pow $node) { |
||
| 340 | return $this->pInfixOp(BinaryOp\Pow::class, $node->left, ' ** ', $node->right); |
||
| 341 | } |
||
| 342 | |||
| 343 | protected function pExpr_BinaryOp_LogicalAnd(BinaryOp\LogicalAnd $node) { |
||
| 344 | return $this->pInfixOp(BinaryOp\LogicalAnd::class, $node->left, ' and ', $node->right); |
||
| 345 | } |
||
| 346 | |||
| 347 | protected function pExpr_BinaryOp_LogicalOr(BinaryOp\LogicalOr $node) { |
||
| 348 | return $this->pInfixOp(BinaryOp\LogicalOr::class, $node->left, ' or ', $node->right); |
||
| 349 | } |
||
| 350 | |||
| 351 | protected function pExpr_BinaryOp_LogicalXor(BinaryOp\LogicalXor $node) { |
||
| 352 | return $this->pInfixOp(BinaryOp\LogicalXor::class, $node->left, ' xor ', $node->right); |
||
| 353 | } |
||
| 354 | |||
| 355 | protected function pExpr_BinaryOp_Equal(BinaryOp\Equal $node) { |
||
| 356 | return $this->pInfixOp(BinaryOp\Equal::class, $node->left, ' == ', $node->right); |
||
| 357 | } |
||
| 358 | |||
| 359 | protected function pExpr_BinaryOp_NotEqual(BinaryOp\NotEqual $node) { |
||
| 360 | return $this->pInfixOp(BinaryOp\NotEqual::class, $node->left, ' != ', $node->right); |
||
| 361 | } |
||
| 362 | |||
| 363 | protected function pExpr_BinaryOp_Identical(BinaryOp\Identical $node) { |
||
| 364 | return $this->pInfixOp(BinaryOp\Identical::class, $node->left, ' === ', $node->right); |
||
| 365 | } |
||
| 366 | |||
| 367 | protected function pExpr_BinaryOp_NotIdentical(BinaryOp\NotIdentical $node) { |
||
| 368 | return $this->pInfixOp(BinaryOp\NotIdentical::class, $node->left, ' !== ', $node->right); |
||
| 369 | } |
||
| 370 | |||
| 371 | protected function pExpr_BinaryOp_Spaceship(BinaryOp\Spaceship $node) { |
||
| 372 | return $this->pInfixOp(BinaryOp\Spaceship::class, $node->left, ' <=> ', $node->right); |
||
| 373 | } |
||
| 374 | |||
| 375 | protected function pExpr_BinaryOp_Greater(BinaryOp\Greater $node) { |
||
| 376 | return $this->pInfixOp(BinaryOp\Greater::class, $node->left, ' > ', $node->right); |
||
| 377 | } |
||
| 378 | |||
| 379 | protected function pExpr_BinaryOp_GreaterOrEqual(BinaryOp\GreaterOrEqual $node) { |
||
| 380 | return $this->pInfixOp(BinaryOp\GreaterOrEqual::class, $node->left, ' >= ', $node->right); |
||
| 381 | } |
||
| 382 | |||
| 383 | protected function pExpr_BinaryOp_Smaller(BinaryOp\Smaller $node) { |
||
| 384 | return $this->pInfixOp(BinaryOp\Smaller::class, $node->left, ' < ', $node->right); |
||
| 385 | } |
||
| 386 | |||
| 387 | protected function pExpr_BinaryOp_SmallerOrEqual(BinaryOp\SmallerOrEqual $node) { |
||
| 388 | return $this->pInfixOp(BinaryOp\SmallerOrEqual::class, $node->left, ' <= ', $node->right); |
||
| 389 | } |
||
| 390 | |||
| 391 | protected function pExpr_BinaryOp_Coalesce(BinaryOp\Coalesce $node) { |
||
| 392 | return $this->pInfixOp(BinaryOp\Coalesce::class, $node->left, ' ?? ', $node->right); |
||
| 393 | } |
||
| 394 | |||
| 395 | protected function pExpr_Instanceof(Expr\Instanceof_ $node) { |
||
| 396 | [$precedence, $associativity] = $this->precedenceMap[Expr\Instanceof_::class]; |
||
| 397 | return $this->pPrec($node->expr, $precedence, $associativity, -1) |
||
| 398 | . ' instanceof ' |
||
| 399 | . $this->pNewVariable($node->class); |
||
| 400 | } |
||
| 401 | |||
| 402 | // Unary expressions |
||
| 403 | |||
| 404 | protected function pExpr_BooleanNot(Expr\BooleanNot $node) { |
||
| 405 | return $this->pPrefixOp(Expr\BooleanNot::class, '!', $node->expr); |
||
| 406 | } |
||
| 407 | |||
| 408 | protected function pExpr_BitwiseNot(Expr\BitwiseNot $node) { |
||
| 409 | return $this->pPrefixOp(Expr\BitwiseNot::class, '~', $node->expr); |
||
| 410 | } |
||
| 411 | |||
| 412 | protected function pExpr_UnaryMinus(Expr\UnaryMinus $node) { |
||
| 413 | if ($node->expr instanceof Expr\UnaryMinus || $node->expr instanceof Expr\PreDec) { |
||
| 414 | // Enforce -(-$expr) instead of --$expr |
||
| 415 | return '-(' . $this->p($node->expr) . ')'; |
||
| 416 | } |
||
| 417 | return $this->pPrefixOp(Expr\UnaryMinus::class, '-', $node->expr); |
||
| 418 | } |
||
| 419 | |||
| 420 | protected function pExpr_UnaryPlus(Expr\UnaryPlus $node) { |
||
| 421 | if ($node->expr instanceof Expr\UnaryPlus || $node->expr instanceof Expr\PreInc) { |
||
| 422 | // Enforce +(+$expr) instead of ++$expr |
||
| 423 | return '+(' . $this->p($node->expr) . ')'; |
||
| 424 | } |
||
| 425 | return $this->pPrefixOp(Expr\UnaryPlus::class, '+', $node->expr); |
||
| 426 | } |
||
| 427 | |||
| 428 | protected function pExpr_PreInc(Expr\PreInc $node) { |
||
| 429 | return $this->pPrefixOp(Expr\PreInc::class, '++', $node->var); |
||
| 430 | } |
||
| 431 | |||
| 432 | protected function pExpr_PreDec(Expr\PreDec $node) { |
||
| 433 | return $this->pPrefixOp(Expr\PreDec::class, '--', $node->var); |
||
| 434 | } |
||
| 435 | |||
| 436 | protected function pExpr_PostInc(Expr\PostInc $node) { |
||
| 437 | return $this->pPostfixOp(Expr\PostInc::class, $node->var, '++'); |
||
| 438 | } |
||
| 439 | |||
| 440 | protected function pExpr_PostDec(Expr\PostDec $node) { |
||
| 441 | return $this->pPostfixOp(Expr\PostDec::class, $node->var, '--'); |
||
| 442 | } |
||
| 443 | |||
| 444 | protected function pExpr_ErrorSuppress(Expr\ErrorSuppress $node) { |
||
| 445 | return $this->pPrefixOp(Expr\ErrorSuppress::class, '@', $node->expr); |
||
| 446 | } |
||
| 447 | |||
| 448 | protected function pExpr_YieldFrom(Expr\YieldFrom $node) { |
||
| 449 | return $this->pPrefixOp(Expr\YieldFrom::class, 'yield from ', $node->expr); |
||
| 450 | } |
||
| 451 | |||
| 452 | protected function pExpr_Print(Expr\Print_ $node) { |
||
| 453 | return $this->pPrefixOp(Expr\Print_::class, 'print ', $node->expr); |
||
| 454 | } |
||
| 455 | |||
| 456 | // Casts |
||
| 457 | |||
| 458 | protected function pExpr_Cast_Int(Cast\Int_ $node) { |
||
| 459 | return $this->pPrefixOp(Cast\Int_::class, '(int) ', $node->expr); |
||
| 460 | } |
||
| 461 | |||
| 462 | protected function pExpr_Cast_Double(Cast\Double $node) { |
||
| 463 | $kind = $node->getAttribute('kind', Cast\Double::KIND_DOUBLE); |
||
| 464 | if ($kind === Cast\Double::KIND_DOUBLE) { |
||
| 465 | $cast = '(double)'; |
||
| 466 | } elseif ($kind === Cast\Double::KIND_FLOAT) { |
||
| 467 | $cast = '(float)'; |
||
| 468 | } elseif ($kind === Cast\Double::KIND_REAL) { |
||
| 469 | $cast = '(real)'; |
||
| 470 | } |
||
| 471 | return $this->pPrefixOp(Cast\Double::class, $cast . ' ', $node->expr); |
||
| 472 | } |
||
| 473 | |||
| 474 | protected function pExpr_Cast_String(Cast\String_ $node) { |
||
| 475 | return $this->pPrefixOp(Cast\String_::class, '(string) ', $node->expr); |
||
| 476 | } |
||
| 477 | |||
| 478 | protected function pExpr_Cast_Array(Cast\Array_ $node) { |
||
| 479 | return $this->pPrefixOp(Cast\Array_::class, '(array) ', $node->expr); |
||
| 480 | } |
||
| 481 | |||
| 482 | protected function pExpr_Cast_Object(Cast\Object_ $node) { |
||
| 483 | return $this->pPrefixOp(Cast\Object_::class, '(object) ', $node->expr); |
||
| 484 | } |
||
| 485 | |||
| 486 | protected function pExpr_Cast_Bool(Cast\Bool_ $node) { |
||
| 487 | return $this->pPrefixOp(Cast\Bool_::class, '(bool) ', $node->expr); |
||
| 488 | } |
||
| 489 | |||
| 490 | protected function pExpr_Cast_Unset(Cast\Unset_ $node) { |
||
| 491 | return $this->pPrefixOp(Cast\Unset_::class, '(unset) ', $node->expr); |
||
| 492 | } |
||
| 493 | |||
| 494 | // Function calls and similar constructs |
||
| 495 | |||
| 496 | protected function pExpr_FuncCall(Expr\FuncCall $node) { |
||
| 497 | return $this->pCallLhs($node->name) |
||
| 498 | . '(' . $this->pMaybeMultiline($node->args) . ')'; |
||
| 499 | } |
||
| 500 | |||
| 501 | protected function pExpr_MethodCall(Expr\MethodCall $node) { |
||
| 502 | $code = $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name) |
||
| 503 | . '(' . $this->pMaybeMultiline($node->args) . ')'; |
||
| 504 | $token = '\'__' . md5($code) . '__\''; |
||
| 505 | $this->options['builder']->closures[$token] = $code; |
||
| 506 | return $token; |
||
| 507 | } |
||
| 508 | |||
| 509 | protected function pExpr_NullsafeMethodCall(Expr\NullsafeMethodCall $node) { |
||
| 510 | return $this->pDereferenceLhs($node->var) . '?->' . $this->pObjectProperty($node->name) |
||
| 511 | . '(' . $this->pMaybeMultiline($node->args) . ')'; |
||
| 512 | } |
||
| 513 | |||
| 514 | protected function pExpr_StaticCall(Expr\StaticCall $node) { |
||
| 515 | $code = $this->pDereferenceLhs($node->class) . '::' |
||
| 516 | . ($node->name instanceof Expr |
||
| 517 | ? ($node->name instanceof Expr\Variable |
||
| 518 | ? $this->p($node->name) |
||
| 519 | : '{' . $this->p($node->name) . '}') |
||
| 520 | : $node->name) |
||
| 521 | . '(' . $this->pMaybeMultiline($node->args) . ')'; |
||
| 522 | $token = '\'__' . md5($code) . '__\''; |
||
| 523 | $this->options['builder']->closures[$token] = $code; |
||
| 524 | return $token; |
||
| 525 | } |
||
| 526 | |||
| 527 | protected function pExpr_Empty(Expr\Empty_ $node) { |
||
| 528 | return 'empty(' . $this->p($node->expr) . ')'; |
||
| 529 | } |
||
| 530 | |||
| 531 | protected function pExpr_Isset(Expr\Isset_ $node) { |
||
| 532 | return 'isset(' . $this->pCommaSeparated($node->vars) . ')'; |
||
| 533 | } |
||
| 534 | |||
| 535 | protected function pExpr_Eval(Expr\Eval_ $node) { |
||
| 536 | $code = 'eval(' . $this->p($node->expr) . ')'; |
||
| 537 | $token = '\'__' . md5($code) . '__\''; |
||
| 538 | $this->options['builder']->closures[$token] = $code; |
||
| 539 | return $token; |
||
| 540 | } |
||
| 541 | |||
| 542 | protected function pExpr_Include(Expr\Include_ $node) { |
||
| 543 | static $map = [ |
||
| 544 | Expr\Include_::TYPE_INCLUDE => 'include', |
||
| 545 | Expr\Include_::TYPE_INCLUDE_ONCE => 'include_once', |
||
| 546 | Expr\Include_::TYPE_REQUIRE => 'require', |
||
| 547 | Expr\Include_::TYPE_REQUIRE_ONCE => 'require_once', |
||
| 548 | ]; |
||
| 549 | |||
| 550 | $code = $map[$node->type] . ' ' . $this->p($node->expr); |
||
| 551 | $token = '\'__' . md5($code) . '__\''; |
||
| 552 | $this->options['builder']->closures[$token] = $code; |
||
| 553 | |||
| 554 | return $token; |
||
| 555 | } |
||
| 556 | |||
| 557 | protected function pExpr_List(Expr\List_ $node) { |
||
| 558 | return 'list(' . $this->pCommaSeparated($node->items) . ')'; |
||
| 559 | } |
||
| 560 | |||
| 561 | // Other |
||
| 562 | |||
| 563 | protected function pExpr_Error(Expr\Error $node) { |
||
| 564 | throw new \LogicException('Cannot pretty-print AST with Error nodes'); |
||
| 565 | } |
||
| 566 | |||
| 567 | protected function pExpr_Variable(Expr\Variable $node) { |
||
| 568 | if ($node->name instanceof Expr) { |
||
| 569 | return '${' . $this->p($node->name) . '}'; |
||
| 570 | } else { |
||
| 571 | return '$' . $node->name; |
||
| 572 | } |
||
| 573 | } |
||
| 574 | |||
| 575 | protected function pExpr_Array(Expr\Array_ $node) { |
||
| 576 | $syntax = $node->getAttribute('kind', |
||
| 577 | $this->options['shortArraySyntax'] ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG); |
||
| 578 | if ($syntax === Expr\Array_::KIND_SHORT) { |
||
| 579 | return '[' . $this->pMaybeMultiline($node->items, true) . ']'; |
||
| 580 | } else { |
||
| 581 | return 'array(' . $this->pMaybeMultiline($node->items, true) . ')'; |
||
| 582 | } |
||
| 583 | } |
||
| 584 | |||
| 585 | protected function pExpr_ArrayItem(Expr\ArrayItem $node) { |
||
| 586 | return (null !== $node->key ? $this->p($node->key) . ' => ' : '') |
||
| 587 | . ($node->byRef ? '&' : '') |
||
| 588 | . ($node->unpack ? '...' : '') |
||
| 589 | . $this->p($node->value); |
||
| 590 | } |
||
| 591 | |||
| 592 | protected function pExpr_ArrayDimFetch(Expr\ArrayDimFetch $node) { |
||
| 593 | return $this->pDereferenceLhs($node->var) |
||
| 594 | . '[' . (null !== $node->dim ? $this->p($node->dim) : '') . ']'; |
||
| 595 | } |
||
| 596 | |||
| 597 | protected function pExpr_ConstFetch(Expr\ConstFetch $node) { |
||
| 598 | return $this->p($node->name); |
||
| 599 | } |
||
| 600 | |||
| 601 | protected function pExpr_ClassConstFetch(Expr\ClassConstFetch $node) { |
||
| 602 | return $this->pDereferenceLhs($node->class) . '::' . $this->p($node->name); |
||
| 603 | } |
||
| 604 | |||
| 605 | protected function pExpr_PropertyFetch(Expr\PropertyFetch $node) { |
||
| 606 | return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name); |
||
| 607 | } |
||
| 608 | |||
| 609 | protected function pExpr_NullsafePropertyFetch(Expr\NullsafePropertyFetch $node) { |
||
| 610 | return $this->pDereferenceLhs($node->var) . '?->' . $this->pObjectProperty($node->name); |
||
| 611 | } |
||
| 612 | |||
| 613 | protected function pExpr_StaticPropertyFetch(Expr\StaticPropertyFetch $node) { |
||
| 614 | return $this->pDereferenceLhs($node->class) . '::$' . $this->pObjectProperty($node->name); |
||
| 615 | } |
||
| 616 | |||
| 617 | protected function pExpr_ShellExec(Expr\ShellExec $node) { |
||
| 618 | return '`' . $this->pEncapsList($node->parts, '`') . '`'; |
||
| 619 | } |
||
| 620 | |||
| 621 | protected function pExpr_Closure(Expr\Closure $node) { |
||
| 622 | $code = ($node->static ? 'static ' : '') |
||
| 623 | . 'function ' . ($node->byRef ? '&' : '') |
||
| 624 | . '(' . $this->pCommaSeparated($node->params) . ')' |
||
| 625 | . (!empty($node->uses) ? ' use(' . $this->pCommaSeparated($node->uses) . ')' : '') |
||
| 626 | . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') |
||
| 627 | . ' {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
| 628 | $token = '\'__' . md5($code) . '__\''; |
||
| 629 | $this->options['builder']->closures[$token] = $code; |
||
| 630 | return $token; |
||
| 631 | } |
||
| 632 | |||
| 633 | protected function pExpr_Match(Expr\Match_ $node) { |
||
| 634 | return 'match (' . $this->p($node->cond) . ') {' |
||
| 635 | . $this->pCommaSeparatedMultiline($node->arms, true) |
||
| 636 | . $this->nl |
||
| 637 | . '}'; |
||
| 638 | } |
||
| 639 | |||
| 640 | protected function pMatchArm(Node\MatchArm $node) { |
||
| 641 | return ($node->conds ? $this->pCommaSeparated($node->conds) : 'default') |
||
| 642 | . ' => ' . $this->p($node->body); |
||
| 643 | } |
||
| 644 | |||
| 645 | protected function pExpr_ArrowFunction(Expr\ArrowFunction $node) { |
||
| 646 | $code = ($node->static ? 'static ' : '') |
||
| 647 | . 'fn' . ($node->byRef ? '&' : '') |
||
| 648 | . '(' . $this->pCommaSeparated($node->params) . ')' |
||
| 649 | . (null !== $node->returnType ? ': ' . $this->p($node->returnType) : '') |
||
| 650 | . ' => ' |
||
| 651 | . $this->p($node->expr); |
||
| 652 | $token = '\'__' . md5($code) . '__\''; |
||
| 653 | $this->options['builder']->closures[$token] = $code; |
||
| 654 | return $token; |
||
| 655 | } |
||
| 656 | |||
| 657 | protected function pExpr_ClosureUse(Expr\ClosureUse $node) { |
||
| 658 | return ($node->byRef ? '&' : '') . $this->p($node->var); |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Return new class expression |
||
| 663 | * @param Expr\New_ $node |
||
| 664 | * |
||
| 665 | * @return string |
||
| 666 | */ |
||
| 667 | protected function pExpr_New(Expr\New_ $node) { |
||
| 668 | if ($node->class instanceof Stmt\Class_) { |
||
| 669 | $args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : ''; |
||
| 670 | $code = 'new ' . $this->pClassCommon($node->class, $args); |
||
| 671 | $token = '\'__' . md5($code) . '__\''; |
||
| 672 | $this->options['builder']->closures[$token] = $code; |
||
| 673 | return $token; |
||
| 674 | } |
||
| 675 | $code = 'new ' . $this->pNewVariable($node->class) |
||
| 676 | . '(' . $this->pMaybeMultiline($node->args) . ')'; |
||
| 677 | if($this->pNewVariable($node->class) == 'ReverseBlockMerge'){ |
||
| 678 | return $code; |
||
| 679 | } |
||
| 680 | $token = '\'__' . md5($code) . '__\''; |
||
| 681 | $this->options['builder']->closures[$token] = $code; |
||
| 682 | return $token; |
||
| 683 | } |
||
| 684 | |||
| 685 | protected function pExpr_Clone(Expr\Clone_ $node) { |
||
| 686 | return 'clone ' . $this->p($node->expr); |
||
| 687 | } |
||
| 688 | |||
| 689 | protected function pExpr_Ternary(Expr\Ternary $node) { |
||
| 690 | // a bit of cheating: we treat the ternary as a binary op where the ?...: part is the operator. |
||
| 691 | // this is okay because the part between ? and : never needs parentheses. |
||
| 692 | return $this->pInfixOp(Expr\Ternary::class, |
||
| 693 | $node->cond, ' ?' . (null !== $node->if ? ' ' . $this->p($node->if) . ' ' : '') . ': ', $node->else |
||
| 694 | ); |
||
| 695 | } |
||
| 696 | |||
| 697 | protected function pExpr_Exit(Expr\Exit_ $node) { |
||
| 698 | $kind = $node->getAttribute('kind', Expr\Exit_::KIND_DIE); |
||
| 699 | return ($kind === Expr\Exit_::KIND_EXIT ? 'exit' : 'die') |
||
| 700 | . (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : ''); |
||
| 701 | } |
||
| 702 | |||
| 703 | protected function pExpr_Throw(Expr\Throw_ $node) { |
||
| 704 | return 'throw ' . $this->p($node->expr); |
||
| 705 | } |
||
| 706 | |||
| 707 | protected function pExpr_Yield(Expr\Yield_ $node) { |
||
| 708 | if ($node->value === null) { |
||
| 709 | return 'yield'; |
||
| 710 | } else { |
||
| 711 | // this is a bit ugly, but currently there is no way to detect whether the parentheses are necessary |
||
| 712 | return '(yield ' |
||
| 713 | . ($node->key !== null ? $this->p($node->key) . ' => ' : '') |
||
| 714 | . $this->p($node->value) |
||
| 715 | . ')'; |
||
| 716 | } |
||
| 717 | } |
||
| 718 | |||
| 719 | // Declarations |
||
| 720 | |||
| 721 | protected function pStmt_Namespace(Stmt\Namespace_ $node) { |
||
| 722 | if ($this->canUseSemicolonNamespaces) { |
||
| 723 | return 'namespace ' . $this->p($node->name) . ';' |
||
| 724 | . $this->nl . $this->pStmts($node->stmts, false); |
||
| 725 | } else { |
||
| 726 | return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '') |
||
| 727 | . ' {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
| 728 | } |
||
| 729 | } |
||
| 730 | |||
| 731 | protected function pStmt_Use(Stmt\Use_ $node) { |
||
| 732 | return 'use ' . $this->pUseType($node->type) |
||
| 733 | . $this->pCommaSeparated($node->uses) . ';'; |
||
| 734 | } |
||
| 735 | |||
| 736 | protected function pStmt_GroupUse(Stmt\GroupUse $node) { |
||
| 737 | return 'use ' . $this->pUseType($node->type) . $this->pName($node->prefix) |
||
| 738 | . '\{' . $this->pCommaSeparated($node->uses) . '};'; |
||
| 739 | } |
||
| 740 | |||
| 741 | protected function pStmt_UseUse(Stmt\UseUse $node) { |
||
| 742 | return $this->pUseType($node->type) . $this->p($node->name) |
||
| 743 | . (null !== $node->alias ? ' as ' . $node->alias : ''); |
||
| 744 | } |
||
| 745 | |||
| 746 | protected function pUseType($type) { |
||
| 747 | return $type === Stmt\Use_::TYPE_FUNCTION ? 'function ' |
||
| 748 | : ($type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : ''); |
||
| 749 | } |
||
| 750 | |||
| 751 | protected function pStmt_Interface(Stmt\Interface_ $node) { |
||
| 752 | return 'interface ' . $node->name |
||
| 753 | . (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '') |
||
| 754 | . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
| 755 | } |
||
| 756 | |||
| 757 | protected function pStmt_Class(Stmt\Class_ $node) { |
||
| 758 | return $this->pClassCommon($node, ' ' . $node->name); |
||
| 759 | } |
||
| 760 | |||
| 761 | protected function pStmt_Trait(Stmt\Trait_ $node) { |
||
| 762 | return 'trait ' . $node->name |
||
| 763 | . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
| 764 | } |
||
| 765 | |||
| 766 | protected function pStmt_TraitUse(Stmt\TraitUse $node) { |
||
| 767 | $class = $this->pCommaSeparated($node->traits) |
||
| 768 | . (empty($node->adaptations)? ';': ' {' . $this->pStmts($node->adaptations) . $this->nl . '}'); |
||
| 769 | return 'use ' . $class; |
||
| 770 | } |
||
| 771 | |||
| 772 | protected function pStmt_TraitUseAdaptation_Precedence(Stmt\TraitUseAdaptation\Precedence $node) { |
||
| 773 | return $this->p($node->trait) . '::' . $node->method |
||
| 774 | . ' insteadof ' . $this->pCommaSeparated($node->insteadof) . ';'; |
||
| 775 | } |
||
| 776 | |||
| 777 | protected function pStmt_TraitUseAdaptation_Alias(Stmt\TraitUseAdaptation\Alias $node) { |
||
| 778 | return (null !== $node->trait ? $this->p($node->trait) . '::' : '') |
||
| 779 | . $node->method . ' as' |
||
| 780 | . (null !== $node->newModifier ? ' ' . rtrim($this->pModifiers($node->newModifier), ' ') : '') |
||
| 781 | . (null !== $node->newName ? ' ' . $node->newName : '') |
||
| 782 | . ';'; |
||
| 783 | } |
||
| 784 | |||
| 785 | protected function pStmt_Property(Stmt\Property $node) { |
||
| 786 | return (0 === $node->flags ? 'var ' : $this->pModifiers($node->flags)) |
||
| 787 | . ($node->type ? $this->p($node->type) . ' ' : '') |
||
| 788 | . $this->pCommaSeparated($node->props) . ';'; |
||
| 789 | } |
||
| 790 | |||
| 791 | protected function pStmt_PropertyProperty(Stmt\PropertyProperty $node) { |
||
| 792 | return '$' . $node->name |
||
| 793 | . (null !== $node->default ? ' = ' . $this->p($node->default) : ''); |
||
| 794 | } |
||
| 795 | |||
| 796 | protected function pStmt_ClassMethod(Stmt\ClassMethod $node) { |
||
| 797 | $code = $this->pModifiers($node->flags) |
||
| 798 | . 'function ' . ($node->byRef ? '&' : '') . $node->name |
||
| 799 | . '(' . $this->pMaybeMultiline($node->params) . ')' |
||
| 800 | . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') |
||
| 801 | . (null !== $node->stmts |
||
| 802 | ? $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}' |
||
| 803 | : ';'); |
||
| 804 | $token = '\'__' . md5($code) . '__\''; |
||
| 805 | $this->options['builder']->closures[$token] = $code; |
||
| 806 | return $token; |
||
| 807 | } |
||
| 808 | |||
| 809 | protected function pStmt_ClassConst(Stmt\ClassConst $node) { |
||
| 810 | return $this->pModifiers($node->flags) |
||
| 811 | . 'const ' . $this->pCommaSeparated($node->consts) . ';'; |
||
| 812 | } |
||
| 813 | |||
| 814 | protected function pStmt_Function(Stmt\Function_ $node) { |
||
| 815 | $code = 'function ' . ($node->byRef ? '&' : '') . $node->name |
||
| 816 | . '(' . $this->pCommaSeparated($node->params) . ')' |
||
| 817 | . (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '') |
||
| 818 | . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
| 819 | $token = '\'__' . md5($code) . '__\''; |
||
| 820 | $this->options['builder']->closures[$token] = $code; |
||
| 821 | return $token; |
||
| 822 | } |
||
| 823 | |||
| 824 | protected function pStmt_Const(Stmt\Const_ $node) { |
||
| 825 | return 'const ' . $this->pCommaSeparated($node->consts) . ';'; |
||
| 826 | } |
||
| 827 | |||
| 828 | protected function pStmt_Declare(Stmt\Declare_ $node) { |
||
| 829 | return 'declare (' . $this->pCommaSeparated($node->declares) . ')' |
||
| 830 | . (null !== $node->stmts ? ' {' . $this->pStmts($node->stmts) . $this->nl . '}' : ';'); |
||
| 831 | } |
||
| 832 | |||
| 833 | protected function pStmt_DeclareDeclare(Stmt\DeclareDeclare $node) { |
||
| 834 | return $node->key . '=' . $this->p($node->value); |
||
| 835 | } |
||
| 836 | |||
| 837 | // Control flow |
||
| 838 | |||
| 839 | protected function pStmt_If(Stmt\If_ $node) { |
||
| 840 | return 'if (' . $this->p($node->cond) . ') {' |
||
| 841 | . $this->pStmts($node->stmts) . $this->nl . '}' |
||
| 842 | . ($node->elseifs ? ' ' . $this->pImplode($node->elseifs, ' ') : '') |
||
| 843 | . (null !== $node->else ? ' ' . $this->p($node->else) : ''); |
||
| 844 | } |
||
| 845 | |||
| 846 | protected function pStmt_ElseIf(Stmt\ElseIf_ $node) { |
||
| 847 | return 'elseif (' . $this->p($node->cond) . ') {' |
||
| 848 | . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
| 849 | } |
||
| 850 | |||
| 851 | protected function pStmt_Else(Stmt\Else_ $node) { |
||
| 852 | return 'else {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
| 853 | } |
||
| 854 | |||
| 855 | protected function pStmt_For(Stmt\For_ $node) { |
||
| 856 | return 'for (' |
||
| 857 | . $this->pCommaSeparated($node->init) . ';' . (!empty($node->cond) ? ' ' : '') |
||
| 858 | . $this->pCommaSeparated($node->cond) . ';' . (!empty($node->loop) ? ' ' : '') |
||
| 859 | . $this->pCommaSeparated($node->loop) |
||
| 860 | . ') {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
| 861 | } |
||
| 862 | |||
| 863 | protected function pStmt_Foreach(Stmt\Foreach_ $node) { |
||
| 864 | return 'foreach (' . $this->p($node->expr) . ' as ' |
||
| 865 | . (null !== $node->keyVar ? $this->p($node->keyVar) . ' => ' : '') |
||
| 866 | . ($node->byRef ? '&' : '') . $this->p($node->valueVar) . ') {' |
||
| 867 | . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
| 868 | } |
||
| 869 | |||
| 870 | protected function pStmt_While(Stmt\While_ $node) { |
||
| 871 | return 'while (' . $this->p($node->cond) . ') {' |
||
| 872 | . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
| 873 | } |
||
| 874 | |||
| 875 | protected function pStmt_Do(Stmt\Do_ $node) { |
||
| 876 | return 'do {' . $this->pStmts($node->stmts) . $this->nl |
||
| 877 | . '} while (' . $this->p($node->cond) . ');'; |
||
| 878 | } |
||
| 879 | |||
| 880 | protected function pStmt_Switch(Stmt\Switch_ $node) { |
||
| 881 | return 'switch (' . $this->p($node->cond) . ') {' |
||
| 882 | . $this->pStmts($node->cases) . $this->nl . '}'; |
||
| 883 | } |
||
| 884 | |||
| 885 | protected function pStmt_Case(Stmt\Case_ $node) { |
||
| 886 | return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':' |
||
| 887 | . $this->pStmts($node->stmts); |
||
| 888 | } |
||
| 889 | |||
| 890 | protected function pStmt_TryCatch(Stmt\TryCatch $node) { |
||
| 891 | return 'try {' . $this->pStmts($node->stmts) . $this->nl . '}' |
||
| 892 | . ($node->catches ? ' ' . $this->pImplode($node->catches, ' ') : '') |
||
| 893 | . ($node->finally !== null ? ' ' . $this->p($node->finally) : ''); |
||
| 894 | } |
||
| 895 | |||
| 896 | protected function pStmt_Catch(Stmt\Catch_ $node) { |
||
| 897 | return 'catch (' . $this->pImplode($node->types, '|') |
||
| 898 | . ($node->var !== null ? ' ' . $this->p($node->var) : '') |
||
| 899 | . ') {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
| 900 | } |
||
| 901 | |||
| 902 | protected function pStmt_Finally(Stmt\Finally_ $node) { |
||
| 903 | return 'finally {' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
| 904 | } |
||
| 905 | |||
| 906 | protected function pStmt_Break(Stmt\Break_ $node) { |
||
| 907 | return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';'; |
||
| 908 | } |
||
| 909 | |||
| 910 | protected function pStmt_Continue(Stmt\Continue_ $node) { |
||
| 911 | return 'continue' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';'; |
||
| 912 | } |
||
| 913 | |||
| 914 | protected function pStmt_Return(Stmt\Return_ $node) { |
||
| 915 | return 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';'; |
||
| 916 | } |
||
| 917 | |||
| 918 | protected function pStmt_Throw(Stmt\Throw_ $node) { |
||
| 919 | return 'throw ' . $this->p($node->expr) . ';'; |
||
| 920 | } |
||
| 921 | |||
| 922 | protected function pStmt_Label(Stmt\Label $node) { |
||
| 923 | return $node->name . ':'; |
||
| 924 | } |
||
| 925 | |||
| 926 | protected function pStmt_Goto(Stmt\Goto_ $node) { |
||
| 927 | return 'goto ' . $node->name . ';'; |
||
| 928 | } |
||
| 929 | |||
| 930 | // Other |
||
| 931 | |||
| 932 | protected function pStmt_Expression(Stmt\Expression $node) { |
||
| 933 | return $this->p($node->expr) . ';'; |
||
| 934 | } |
||
| 935 | |||
| 936 | protected function pStmt_Echo(Stmt\Echo_ $node) { |
||
| 937 | return 'echo ' . $this->pCommaSeparated($node->exprs) . ';'; |
||
| 938 | } |
||
| 939 | |||
| 940 | protected function pStmt_Static(Stmt\Static_ $node) { |
||
| 941 | return 'static ' . $this->pCommaSeparated($node->vars) . ';'; |
||
| 942 | } |
||
| 943 | |||
| 944 | protected function pStmt_Global(Stmt\Global_ $node) { |
||
| 945 | return 'global ' . $this->pCommaSeparated($node->vars) . ';'; |
||
| 946 | } |
||
| 947 | |||
| 948 | protected function pStmt_StaticVar(Stmt\StaticVar $node) { |
||
| 949 | return $this->p($node->var) |
||
| 950 | . (null !== $node->default ? ' = ' . $this->p($node->default) : ''); |
||
| 951 | } |
||
| 952 | |||
| 953 | protected function pStmt_Unset(Stmt\Unset_ $node) { |
||
| 954 | return 'unset(' . $this->pCommaSeparated($node->vars) . ');'; |
||
| 955 | } |
||
| 956 | |||
| 957 | protected function pStmt_InlineHTML(Stmt\InlineHTML $node) { |
||
| 958 | $newline = $node->getAttribute('hasLeadingNewline', true) ? "\n" : ''; |
||
| 959 | return '?>' . $newline . $node->value . '<?php '; |
||
| 960 | } |
||
| 961 | |||
| 962 | protected function pStmt_HaltCompiler(Stmt\HaltCompiler $node) { |
||
| 963 | return '__halt_compiler();' . $node->remaining; |
||
| 964 | } |
||
| 965 | |||
| 966 | protected function pStmt_Nop(Stmt\Nop $node) { |
||
| 967 | return ''; |
||
| 968 | } |
||
| 969 | |||
| 970 | // Helpers |
||
| 971 | |||
| 972 | protected function pClassCommon(Stmt\Class_ $node, $afterClassToken) { |
||
| 973 | return $this->pModifiers($node->flags) |
||
| 974 | . 'class' . $afterClassToken |
||
| 975 | . (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '') |
||
| 976 | . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '') |
||
| 977 | . $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'; |
||
| 978 | } |
||
| 979 | |||
| 980 | protected function pObjectProperty($node) { |
||
| 981 | if ($node instanceof Expr) { |
||
| 982 | return '{' . $this->p($node) . '}'; |
||
| 983 | } else { |
||
| 984 | return $node; |
||
| 985 | } |
||
| 986 | } |
||
| 987 | |||
| 988 | protected function pEncapsList(array $encapsList, $quote) { |
||
| 989 | $return = ''; |
||
| 990 | foreach ($encapsList as $element) { |
||
| 991 | if ($element instanceof Scalar\EncapsedStringPart) { |
||
| 992 | $return .= $this->escapeString($element->value, $quote); |
||
| 993 | } else { |
||
| 994 | $return .= '{' . $this->p($element) . '}'; |
||
| 995 | } |
||
| 996 | } |
||
| 997 | |||
| 998 | return $return; |
||
| 999 | } |
||
| 1000 | |||
| 1001 | protected function pSingleQuotedString(string $string) { |
||
| 1002 | return '\'' . addcslashes($string, '\'\\') . '\''; |
||
| 1003 | } |
||
| 1004 | |||
| 1005 | protected function escapeString($string, $quote) { |
||
| 1006 | if (null === $quote) { |
||
| 1007 | // For doc strings, don't escape newlines |
||
| 1008 | $escaped = addcslashes($string, "\t\f\v$\\"); |
||
| 1009 | } else { |
||
| 1010 | $escaped = addcslashes($string, "\n\r\t\f\v$" . $quote . "\\"); |
||
| 1011 | } |
||
| 1012 | |||
| 1013 | // Escape other control characters |
||
| 1014 | return preg_replace_callback('/([\0-\10\16-\37])(?=([0-7]?))/', function ($matches) { |
||
| 1015 | $oct = decoct(ord($matches[1])); |
||
| 1016 | if ($matches[2] !== '') { |
||
| 1017 | // If there is a trailing digit, use the full three character form |
||
| 1018 | return '\\' . str_pad($oct, 3, '0', \STR_PAD_LEFT); |
||
| 1019 | } |
||
| 1020 | return '\\' . $oct; |
||
| 1021 | }, $escaped); |
||
| 1022 | } |
||
| 1023 | |||
| 1024 | protected function containsEndLabel($string, $label, $atStart = true, $atEnd = true) { |
||
| 1025 | $start = $atStart ? '(?:^|[\r\n])' : '[\r\n]'; |
||
| 1026 | $end = $atEnd ? '(?:$|[;\r\n])' : '[;\r\n]'; |
||
| 1027 | return false !== strpos($string, $label) |
||
| 1028 | && preg_match('/' . $start . $label . $end . '/', $string); |
||
| 1029 | } |
||
| 1030 | |||
| 1031 | protected function encapsedContainsEndLabel(array $parts, $label) { |
||
| 1032 | foreach ($parts as $i => $part) { |
||
| 1033 | $atStart = $i === 0; |
||
| 1034 | $atEnd = $i === count($parts) - 1; |
||
| 1035 | if ($part instanceof Scalar\EncapsedStringPart |
||
| 1036 | && $this->containsEndLabel($part->value, $label, $atStart, $atEnd) |
||
| 1037 | ) { |
||
| 1038 | return true; |
||
| 1039 | } |
||
| 1040 | } |
||
| 1041 | return false; |
||
| 1042 | } |
||
| 1043 | |||
| 1044 | protected function pDereferenceLhs(Node $node) { |
||
| 1045 | if (!$this->dereferenceLhsRequiresParens($node)) { |
||
| 1046 | return $this->p($node); |
||
| 1047 | } else { |
||
| 1048 | return '(' . $this->p($node) . ')'; |
||
| 1049 | } |
||
| 1050 | } |
||
| 1051 | |||
| 1052 | protected function pCallLhs(Node $node) { |
||
| 1053 | if (!$this->callLhsRequiresParens($node)) { |
||
| 1054 | return $this->p($node); |
||
| 1055 | } else { |
||
| 1056 | return '(' . $this->p($node) . ')'; |
||
| 1057 | } |
||
| 1058 | } |
||
| 1059 | |||
| 1060 | protected function pNewVariable(Node $node) { |
||
| 1061 | // TODO: This is not fully accurate. |
||
| 1062 | return $this->pDereferenceLhs($node); |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * @param Node[] $nodes |
||
| 1067 | * @return bool |
||
| 1068 | */ |
||
| 1069 | private function hasNodeWithComments(array $nodes) { |
||
| 1076 | } |
||
| 1077 | |||
| 1078 | private function pMaybeMultiline(array $nodes, $trailingComma = false) { |
||
| 1079 | if (!$this->hasNodeWithComments($nodes)) { |
||
| 1080 | return $this->pCommaSeparated($nodes); |
||
| 1081 | } else { |
||
| 1082 | return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl; |
||
| 1083 | } |
||
| 1084 | } |
||
| 1085 | } |
||
| 1086 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.