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