update v 1.0.7.5

This commit is contained in:
Sujit Prasad
2016-06-13 20:41:55 +05:30
parent aa9786d829
commit 283d97e3ea
5078 changed files with 339851 additions and 175995 deletions

View File

@@ -84,15 +84,71 @@ class Standard extends PrettyPrinterAbstract
// Scalars
public function pScalar_String(Scalar\String_ $node) {
return '\'' . $this->pNoIndent(addcslashes($node->value, '\'\\')) . '\'';
$kind = $node->getAttribute('kind', Scalar\String_::KIND_SINGLE_QUOTED);
switch ($kind) {
case Scalar\String_::KIND_NOWDOC:
$label = $node->getAttribute('docLabel');
if ($label && !$this->containsEndLabel($node->value, $label)) {
if ($node->value === '') {
return $this->pNoIndent("<<<'$label'\n$label") . $this->docStringEndToken;
}
return $this->pNoIndent("<<<'$label'\n$node->value\n$label")
. $this->docStringEndToken;
}
/* break missing intentionally */
case Scalar\String_::KIND_SINGLE_QUOTED:
return '\'' . $this->pNoIndent(addcslashes($node->value, '\'\\')) . '\'';
case Scalar\String_::KIND_HEREDOC:
$label = $node->getAttribute('docLabel');
if ($label && !$this->containsEndLabel($node->value, $label)) {
if ($node->value === '') {
return $this->pNoIndent("<<<$label\n$label") . $this->docStringEndToken;
}
$escaped = $this->escapeString($node->value, null);
return $this->pNoIndent("<<<$label\n" . $escaped ."\n$label")
. $this->docStringEndToken;
}
/* break missing intentionally */
case Scalar\String_::KIND_DOUBLE_QUOTED:
return '"' . $this->escapeString($node->value, '"') . '"';
}
throw new \Exception('Invalid string kind');
}
public function pScalar_Encapsed(Scalar\Encapsed $node) {
if ($node->getAttribute('kind') === Scalar\String_::KIND_HEREDOC) {
$label = $node->getAttribute('docLabel');
if ($label && !$this->encapsedContainsEndLabel($node->parts, $label)) {
if (count($node->parts) === 1
&& $node->parts[0] instanceof Scalar\EncapsedStringPart
&& $node->parts[0]->value === ''
) {
return $this->pNoIndent("<<<$label\n$label") . $this->docStringEndToken;
}
return $this->pNoIndent(
"<<<$label\n" . $this->pEncapsList($node->parts, null) . "\n$label"
) . $this->docStringEndToken;
}
}
return '"' . $this->pEncapsList($node->parts, '"') . '"';
}
public function pScalar_LNumber(Scalar\LNumber $node) {
return (string) $node->value;
$str = (string) $node->value;
switch ($node->getAttribute('kind', Scalar\LNumber::KIND_DEC)) {
case Scalar\LNumber::KIND_BIN:
return '0b' . base_convert($str, 10, 2);
case Scalar\LNumber::KIND_OCT:
return '0' . base_convert($str, 10, 8);
case Scalar\LNumber::KIND_DEC:
return $str;
case Scalar\LNumber::KIND_HEX:
return '0x' . base_convert($str, 10, 16);
}
throw new \Exception('Invalid number kind');
}
public function pScalar_DNumber(Scalar\DNumber $node) {
@@ -356,19 +412,19 @@ class Standard extends PrettyPrinterAbstract
// Function calls and similar constructs
public function pExpr_FuncCall(Expr\FuncCall $node) {
return $this->p($node->name) . '(' . $this->pCommaSeparated($node->args) . ')';
return $this->pCallLhs($node->name)
. '(' . $this->pCommaSeparated($node->args) . ')';
}
public function pExpr_MethodCall(Expr\MethodCall $node) {
return $this->pVarOrNewExpr($node->var) . '->' . $this->pObjectProperty($node->name)
return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name)
. '(' . $this->pCommaSeparated($node->args) . ')';
}
public function pExpr_StaticCall(Expr\StaticCall $node) {
return $this->p($node->class) . '::'
return $this->pDereferenceLhs($node->class) . '::'
. ($node->name instanceof Expr
? ($node->name instanceof Expr\Variable
|| $node->name instanceof Expr\ArrayDimFetch
? $this->p($node->name)
: '{' . $this->p($node->name) . '}')
: $node->name)
@@ -422,7 +478,13 @@ class Standard extends PrettyPrinterAbstract
}
public function pExpr_Array(Expr\Array_ $node) {
return 'array(' . $this->pCommaSeparated($node->items) . ')';
$syntax = $node->getAttribute('kind',
$this->options['shortArraySyntax'] ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG);
if ($syntax === Expr\Array_::KIND_SHORT) {
return '[' . $this->pCommaSeparated($node->items) . ']';
} else {
return 'array(' . $this->pCommaSeparated($node->items) . ')';
}
}
public function pExpr_ArrayItem(Expr\ArrayItem $node) {
@@ -431,7 +493,7 @@ class Standard extends PrettyPrinterAbstract
}
public function pExpr_ArrayDimFetch(Expr\ArrayDimFetch $node) {
return $this->pVarOrNewExpr($node->var)
return $this->pDereferenceLhs($node->var)
. '[' . (null !== $node->dim ? $this->p($node->dim) : '') . ']';
}
@@ -444,11 +506,11 @@ class Standard extends PrettyPrinterAbstract
}
public function pExpr_PropertyFetch(Expr\PropertyFetch $node) {
return $this->pVarOrNewExpr($node->var) . '->' . $this->pObjectProperty($node->name);
return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name);
}
public function pExpr_StaticPropertyFetch(Expr\StaticPropertyFetch $node) {
return $this->p($node->class) . '::$' . $this->pObjectProperty($node->name);
return $this->pDereferenceLhs($node->class) . '::$' . $this->pObjectProperty($node->name);
}
public function pExpr_ShellExec(Expr\ShellExec $node) {
@@ -489,7 +551,9 @@ class Standard extends PrettyPrinterAbstract
}
public function pExpr_Exit(Expr\Exit_ $node) {
return 'die' . (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : '');
$kind = $node->getAttribute('kind', Expr\Exit_::KIND_DIE);
return ($kind === Expr\Exit_::KIND_EXIT ? 'exit' : 'die')
. (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : '');
}
public function pExpr_Yield(Expr\Yield_ $node) {
@@ -516,17 +580,25 @@ class Standard extends PrettyPrinterAbstract
}
public function pStmt_Use(Stmt\Use_ $node) {
return 'use '
. ($node->type === Stmt\Use_::TYPE_FUNCTION ? 'function ' : '')
. ($node->type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : '')
return 'use ' . $this->pUseType($node->type)
. $this->pCommaSeparated($node->uses) . ';';
}
public function pStmt_GroupUse(Stmt\GroupUse $node) {
return 'use ' . $this->pUseType($node->type) . $this->pName($node->prefix)
. '\{' . $this->pCommaSeparated($node->uses) . '};';
}
public function pStmt_UseUse(Stmt\UseUse $node) {
return $this->p($node->name)
return $this->pUseType($node->type) . $this->p($node->name)
. ($node->name->getLast() !== $node->alias ? ' as ' . $node->alias : '');
}
private function pUseType($type) {
return $type === Stmt\Use_::TYPE_FUNCTION ? 'function '
: ($type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : '');
}
public function pStmt_Interface(Stmt\Interface_ $node) {
return 'interface ' . $node->name
. (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '')
@@ -597,12 +669,12 @@ class Standard extends PrettyPrinterAbstract
}
public function pStmt_Declare(Stmt\Declare_ $node) {
return 'declare (' . $this->pCommaSeparated($node->declares) . ') {'
. $this->pStmts($node->stmts) . "\n" . '}';
return 'declare (' . $this->pCommaSeparated($node->declares) . ')'
. (null !== $node->stmts ? ' {' . $this->pStmts($node->stmts) . "\n" . '}' : ';');
}
public function pStmt_DeclareDeclare(Stmt\DeclareDeclare $node) {
return $node->key . ' = ' . $this->p($node->value);
return $node->key . '=' . $this->p($node->value);
}
// Control flow
@@ -726,6 +798,10 @@ class Standard extends PrettyPrinterAbstract
return '__halt_compiler();' . $node->remaining;
}
public function pStmt_Nop(Stmt\Nop $node) {
return '';
}
// Helpers
protected function pType($node) {
@@ -740,8 +816,7 @@ class Standard extends PrettyPrinterAbstract
. "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
}
/** @internal */
public function pObjectProperty($node) {
protected function pObjectProperty($node) {
if ($node instanceof Expr) {
return '{' . $this->p($node) . '}';
} else {
@@ -749,8 +824,7 @@ class Standard extends PrettyPrinterAbstract
}
}
/** @internal */
public function pModifiers($modifiers) {
protected function pModifiers($modifiers) {
return ($modifiers & Stmt\Class_::MODIFIER_PUBLIC ? 'public ' : '')
. ($modifiers & Stmt\Class_::MODIFIER_PROTECTED ? 'protected ' : '')
. ($modifiers & Stmt\Class_::MODIFIER_PRIVATE ? 'private ' : '')
@@ -759,12 +833,11 @@ class Standard extends PrettyPrinterAbstract
. ($modifiers & Stmt\Class_::MODIFIER_FINAL ? 'final ' : '');
}
/** @internal */
public function pEncapsList(array $encapsList, $quote) {
protected function pEncapsList(array $encapsList, $quote) {
$return = '';
foreach ($encapsList as $element) {
if (is_string($element)) {
$return .= addcslashes($element, "\n\r\t\f\v$" . $quote . "\\");
if ($element instanceof Scalar\EncapsedStringPart) {
$return .= $this->escapeString($element->value, $quote);
} else {
$return .= '{' . $this->p($element) . '}';
}
@@ -773,12 +846,66 @@ class Standard extends PrettyPrinterAbstract
return $return;
}
/** @internal */
public function pVarOrNewExpr(Node $node) {
if ($node instanceof Expr\New_) {
return '(' . $this->p($node) . ')';
} else {
protected function escapeString($string, $quote) {
if (null === $quote) {
// For doc strings, don't escape newlines
return addcslashes($string, "\t\f\v$\\");
}
return addcslashes($string, "\n\r\t\f\v$" . $quote . "\\");
}
protected function containsEndLabel($string, $label, $atStart = true, $atEnd = true) {
$start = $atStart ? '(?:^|[\r\n])' : '[\r\n]';
$end = $atEnd ? '(?:$|[;\r\n])' : '[;\r\n]';
return false !== strpos($string, $label)
&& preg_match('/' . $start . $label . $end . '/', $string);
}
protected function encapsedContainsEndLabel(array $parts, $label) {
foreach ($parts as $i => $part) {
$atStart = $i === 0;
$atEnd = $i === count($parts) - 1;
if ($part instanceof Scalar\EncapsedStringPart
&& $this->containsEndLabel($part->value, $label, $atStart, $atEnd)
) {
return true;
}
}
return false;
}
protected function pDereferenceLhs(Node $node) {
if ($node instanceof Expr\Variable
|| $node instanceof Name
|| $node instanceof Expr\ArrayDimFetch
|| $node instanceof Expr\PropertyFetch
|| $node instanceof Expr\StaticPropertyFetch
|| $node instanceof Expr\FuncCall
|| $node instanceof Expr\MethodCall
|| $node instanceof Expr\StaticCall
|| $node instanceof Expr\Array_
|| $node instanceof Scalar\String_
|| $node instanceof Expr\ConstFetch
|| $node instanceof Expr\ClassConstFetch
) {
return $this->p($node);
} else {
return '(' . $this->p($node) . ')';
}
}
protected function pCallLhs(Node $node) {
if ($node instanceof Name
|| $node instanceof Expr\Variable
|| $node instanceof Expr\ArrayDimFetch
|| $node instanceof Expr\FuncCall
|| $node instanceof Expr\MethodCall
|| $node instanceof Expr\StaticCall
|| $node instanceof Expr\Array_
) {
return $this->p($node);
} else {
return '(' . $this->p($node) . ')';
}
}
}