updated packages
This commit is contained in:
+21
-1
@@ -4,6 +4,9 @@ namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\NullableType;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
class Property implements PhpParser\Builder
|
||||
@@ -14,6 +17,9 @@ class Property implements PhpParser\Builder
|
||||
protected $default = null;
|
||||
protected $attributes = [];
|
||||
|
||||
/** @var null|Identifier|Name|NullableType */
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* Creates a property builder.
|
||||
*
|
||||
@@ -95,6 +101,19 @@ class Property implements PhpParser\Builder
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the property type for PHP 7.4+.
|
||||
*
|
||||
* @param string|Name|NullableType|Identifier $type
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setType($type) {
|
||||
$this->type = BuilderHelpers::normalizeType($type);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the built class node.
|
||||
*
|
||||
@@ -106,7 +125,8 @@ class Property implements PhpParser\Builder
|
||||
[
|
||||
new Stmt\PropertyProperty($this->name, $this->default)
|
||||
],
|
||||
$this->attributes
|
||||
$this->attributes,
|
||||
$this->type
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ use PhpParser\Node\Expr\BinaryOp\Concat;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Use_;
|
||||
|
||||
class BuilderFactory
|
||||
@@ -77,7 +76,7 @@ class BuilderFactory
|
||||
* @return Builder\TraitUseAdaptation The create trait use adaptation builder
|
||||
*/
|
||||
public function traitUseAdaptation($trait, $method = null) : Builder\TraitUseAdaptation {
|
||||
if (is_null($method)) {
|
||||
if ($method === null) {
|
||||
$method = $trait;
|
||||
$trait = null;
|
||||
}
|
||||
|
||||
@@ -41,7 +41,6 @@ class PrintableNewAnonClassNode extends Expr
|
||||
assert($class instanceof Node\Stmt\Class_);
|
||||
// We don't assert that $class->name is null here, to allow consumers to assign unique names
|
||||
// to anonymous classes for their own purposes. We simplify ignore the name here.
|
||||
assert($class->name === null);
|
||||
return new self(
|
||||
$newNode->args, $class->extends, $class->implements,
|
||||
$class->stmts, $newNode->getAttributes()
|
||||
|
||||
+99
-30
@@ -4,27 +4,54 @@ namespace PhpParser\Lexer;
|
||||
|
||||
use PhpParser\Error;
|
||||
use PhpParser\ErrorHandler;
|
||||
use PhpParser\Parser;
|
||||
|
||||
class Emulative extends \PhpParser\Lexer
|
||||
{
|
||||
const PHP_7_3 = '7.3.0dev';
|
||||
const PHP_7_4 = '7.4.0dev';
|
||||
|
||||
const FLEXIBLE_DOC_STRING_REGEX = <<<'REGEX'
|
||||
/<<<[ \t]*(['"]?)([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\1\r?\n
|
||||
(?:.*\r?\n)*?
|
||||
(?<indentation>\h*)\2(?![a-zA-Z_\x80-\xff])(?<separator>(?:;?[\r\n])?)/x
|
||||
REGEX;
|
||||
|
||||
const T_COALESCE_EQUAL = 1007;
|
||||
|
||||
/**
|
||||
* @var array Patches used to reverse changes introduced in the code
|
||||
* @var mixed[] Patches used to reverse changes introduced in the code
|
||||
*/
|
||||
private $patches;
|
||||
private $patches = [];
|
||||
|
||||
/**
|
||||
* @param mixed[] $options
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
parent::__construct($options);
|
||||
|
||||
// add emulated tokens here
|
||||
$this->tokenMap[self::T_COALESCE_EQUAL] = Parser\Tokens::T_COALESCE_EQUAL;
|
||||
}
|
||||
|
||||
public function startLexing(string $code, ErrorHandler $errorHandler = null) {
|
||||
$this->patches = [];
|
||||
$preparedCode = $this->prepareCode($code);
|
||||
if (null === $preparedCode) {
|
||||
|
||||
if ($this->isEmulationNeeded($code) === false) {
|
||||
// Nothing to emulate, yay
|
||||
parent::startLexing($code, $errorHandler);
|
||||
return;
|
||||
}
|
||||
|
||||
$collector = new ErrorHandler\Collecting();
|
||||
|
||||
// 1. emulation of heredoc and nowdoc new syntax
|
||||
$preparedCode = $this->processHeredocNowdoc($code);
|
||||
parent::startLexing($preparedCode, $collector);
|
||||
|
||||
// 2. emulation of ??= token
|
||||
$this->processCoaleseEqual($code);
|
||||
$this->fixupTokens();
|
||||
|
||||
$errors = $collector->getErrors();
|
||||
@@ -36,30 +63,60 @@ class Emulative extends \PhpParser\Lexer
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares code for emulation. If nothing has to be emulated null is returned.
|
||||
*
|
||||
* @param string $code
|
||||
* @return null|string
|
||||
*/
|
||||
private function prepareCode(string $code) {
|
||||
private function isCoalesceEqualEmulationNeeded(string $code): bool
|
||||
{
|
||||
// skip version where this works without emulation
|
||||
if (version_compare(\PHP_VERSION, self::PHP_7_4, '>=')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return strpos($code, '??=') !== false;
|
||||
}
|
||||
|
||||
private function processCoaleseEqual(string $code)
|
||||
{
|
||||
if ($this->isCoalesceEqualEmulationNeeded($code) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
// We need to manually iterate and manage a count because we'll change
|
||||
// the tokens array on the way
|
||||
$line = 1;
|
||||
for ($i = 0, $c = count($this->tokens); $i < $c; ++$i) {
|
||||
if (isset($this->tokens[$i + 1])) {
|
||||
if ($this->tokens[$i][0] === T_COALESCE && $this->tokens[$i + 1] === '=') {
|
||||
array_splice($this->tokens, $i, 2, [
|
||||
[self::T_COALESCE_EQUAL, '??=', $line]
|
||||
]);
|
||||
$c--;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (\is_array($this->tokens[$i])) {
|
||||
$line += substr_count($this->tokens[$i][1], "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function isHeredocNowdocEmulationNeeded(string $code): bool
|
||||
{
|
||||
// skip version where this works without emulation
|
||||
if (version_compare(\PHP_VERSION, self::PHP_7_3, '>=')) {
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strpos($code, '<<<') === false) {
|
||||
// Definitely doesn't contain heredoc/nowdoc
|
||||
return null;
|
||||
return strpos($code, '<<<') !== false;
|
||||
}
|
||||
|
||||
private function processHeredocNowdoc(string $code): string
|
||||
{
|
||||
if ($this->isHeredocNowdocEmulationNeeded($code) === false) {
|
||||
return $code;
|
||||
}
|
||||
|
||||
$flexibleDocStringRegex = <<<'REGEX'
|
||||
/<<<[ \t]*(['"]?)([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\1\r?\n
|
||||
(?:.*\r?\n)*?
|
||||
(?<indentation>\h*)\2(?![a-zA-Z_\x80-\xff])(?<separator>(?:;?[\r\n])?)/x
|
||||
REGEX;
|
||||
if (!preg_match_all($flexibleDocStringRegex, $code, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE)) {
|
||||
if (!preg_match_all(self::FLEXIBLE_DOC_STRING_REGEX, $code, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE)) {
|
||||
// No heredoc/nowdoc found
|
||||
return null;
|
||||
return $code;
|
||||
}
|
||||
|
||||
// Keep track of how much we need to adjust string offsets due to the modifications we
|
||||
@@ -93,19 +150,31 @@ REGEX;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($this->patches)) {
|
||||
// We did not end up emulating anything
|
||||
return null;
|
||||
}
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
private function fixupTokens() {
|
||||
assert(count($this->patches) > 0);
|
||||
private function isEmulationNeeded(string $code): bool
|
||||
{
|
||||
if ($this->isHeredocNowdocEmulationNeeded($code)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->isCoalesceEqualEmulationNeeded($code)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function fixupTokens()
|
||||
{
|
||||
if (\count($this->patches) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Load first patch
|
||||
$patchIdx = 0;
|
||||
|
||||
list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
|
||||
|
||||
// We use a manual loop over the tokens, because we modify the array on the fly
|
||||
@@ -200,4 +269,4 @@ REGEX;
|
||||
$error->setAttributes($attrs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,11 @@ use PhpParser\Node\Expr\Cast;
|
||||
|
||||
class Double extends Cast
|
||||
{
|
||||
// For use in "kind" attribute
|
||||
const KIND_DOUBLE = 1; // "double" syntax
|
||||
const KIND_FLOAT = 2; // "float" syntax
|
||||
const KIND_REAL = 3; // "real" syntax
|
||||
|
||||
public function getType() : string {
|
||||
return 'Expr_Cast_Double';
|
||||
}
|
||||
|
||||
+8
-8
@@ -6,7 +6,7 @@ use PhpParser\NodeAbstract;
|
||||
|
||||
class Param extends NodeAbstract
|
||||
{
|
||||
/** @var null|Identifier|Name|NullableType Typehint */
|
||||
/** @var null|Identifier|Name|NullableType Type declaration */
|
||||
public $type;
|
||||
/** @var bool Whether parameter is passed by reference */
|
||||
public $byRef;
|
||||
@@ -20,12 +20,12 @@ class Param extends NodeAbstract
|
||||
/**
|
||||
* Constructs a parameter node.
|
||||
*
|
||||
* @param Expr\Variable|Expr\Error $var Parameter variable
|
||||
* @param null|Expr $default Default value
|
||||
* @param null|string|Name|NullableType $type Typehint
|
||||
* @param bool $byRef Whether is passed by reference
|
||||
* @param bool $variadic Whether this is a variadic argument
|
||||
* @param array $attributes Additional attributes
|
||||
* @param Expr\Variable|Expr\Error $var Parameter variable
|
||||
* @param null|Expr $default Default value
|
||||
* @param null|string|Identifier|Name|NullableType $type Type declaration
|
||||
* @param bool $byRef Whether is passed by reference
|
||||
* @param bool $variadic Whether this is a variadic argument
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(
|
||||
$var, Expr $default = null, $type = null,
|
||||
@@ -42,7 +42,7 @@ class Param extends NodeAbstract
|
||||
public function getSubNodeNames() : array {
|
||||
return ['type', 'byRef', 'variadic', 'var', 'default'];
|
||||
}
|
||||
|
||||
|
||||
public function getType() : string {
|
||||
return 'Param';
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
namespace PhpParser\Node\Stmt;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\NullableType;
|
||||
|
||||
class Property extends Node\Stmt
|
||||
{
|
||||
@@ -10,22 +13,26 @@ class Property extends Node\Stmt
|
||||
public $flags;
|
||||
/** @var PropertyProperty[] Properties */
|
||||
public $props;
|
||||
/** @var null|Identifier|Name|NullableType Type declaration */
|
||||
public $type;
|
||||
|
||||
/**
|
||||
* Constructs a class property list node.
|
||||
*
|
||||
* @param int $flags Modifiers
|
||||
* @param PropertyProperty[] $props Properties
|
||||
* @param array $attributes Additional attributes
|
||||
* @param int $flags Modifiers
|
||||
* @param PropertyProperty[] $props Properties
|
||||
* @param array $attributes Additional attributes
|
||||
* @param null|string|Identifier|Name|NullableType $type Type declaration
|
||||
*/
|
||||
public function __construct(int $flags, array $props, array $attributes = []) {
|
||||
public function __construct(int $flags, array $props, array $attributes = [], $type = null) {
|
||||
parent::__construct($attributes);
|
||||
$this->flags = $flags;
|
||||
$this->props = $props;
|
||||
$this->type = \is_string($type) ? new Identifier($type) : $type;
|
||||
}
|
||||
|
||||
public function getSubNodeNames() : array {
|
||||
return ['flags', 'props'];
|
||||
return ['flags', 'type', 'props'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,7 +71,7 @@ class Property extends Node\Stmt
|
||||
public function isStatic() : bool {
|
||||
return (bool) ($this->flags & Class_::MODIFIER_STATIC);
|
||||
}
|
||||
|
||||
|
||||
public function getType() : string {
|
||||
return 'Stmt_Property';
|
||||
}
|
||||
|
||||
+3
-5
@@ -2,8 +2,6 @@
|
||||
|
||||
namespace PhpParser;
|
||||
|
||||
use PhpParser\Node;
|
||||
|
||||
abstract class NodeAbstract implements Node, \JsonSerializable
|
||||
{
|
||||
protected $attributes;
|
||||
@@ -159,11 +157,11 @@ abstract class NodeAbstract implements Node, \JsonSerializable
|
||||
}
|
||||
|
||||
public function getAttribute(string $key, $default = null) {
|
||||
if (!array_key_exists($key, $this->attributes)) {
|
||||
return $default;
|
||||
} else {
|
||||
if (array_key_exists($key, $this->attributes)) {
|
||||
return $this->attributes[$key];
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function getAttributes() : array {
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace PhpParser\NodeVisitor;
|
||||
|
||||
use PhpParser\Error;
|
||||
use PhpParser\ErrorHandler;
|
||||
use PhpParser\NameContext;
|
||||
use PhpParser\Node;
|
||||
@@ -94,6 +93,10 @@ class NameResolver extends NodeVisitorAbstract
|
||||
|| $node instanceof Expr\Closure
|
||||
) {
|
||||
$this->resolveSignature($node);
|
||||
} elseif ($node instanceof Stmt\Property) {
|
||||
if (null !== $node->type) {
|
||||
$node->type = $this->resolveType($node->type);
|
||||
}
|
||||
} elseif ($node instanceof Stmt\Const_) {
|
||||
foreach ($node->consts as $const) {
|
||||
$this->addNamespacedName($const);
|
||||
|
||||
+917
-903
File diff suppressed because it is too large
Load Diff
+742
-724
File diff suppressed because it is too large
Load Diff
+112
-111
@@ -30,115 +30,116 @@ final class Tokens
|
||||
const T_SL_EQUAL = 278;
|
||||
const T_SR_EQUAL = 279;
|
||||
const T_POW_EQUAL = 280;
|
||||
const T_COALESCE = 281;
|
||||
const T_BOOLEAN_OR = 282;
|
||||
const T_BOOLEAN_AND = 283;
|
||||
const T_IS_EQUAL = 284;
|
||||
const T_IS_NOT_EQUAL = 285;
|
||||
const T_IS_IDENTICAL = 286;
|
||||
const T_IS_NOT_IDENTICAL = 287;
|
||||
const T_SPACESHIP = 288;
|
||||
const T_IS_SMALLER_OR_EQUAL = 289;
|
||||
const T_IS_GREATER_OR_EQUAL = 290;
|
||||
const T_SL = 291;
|
||||
const T_SR = 292;
|
||||
const T_INSTANCEOF = 293;
|
||||
const T_INC = 294;
|
||||
const T_DEC = 295;
|
||||
const T_INT_CAST = 296;
|
||||
const T_DOUBLE_CAST = 297;
|
||||
const T_STRING_CAST = 298;
|
||||
const T_ARRAY_CAST = 299;
|
||||
const T_OBJECT_CAST = 300;
|
||||
const T_BOOL_CAST = 301;
|
||||
const T_UNSET_CAST = 302;
|
||||
const T_POW = 303;
|
||||
const T_NEW = 304;
|
||||
const T_CLONE = 305;
|
||||
const T_EXIT = 306;
|
||||
const T_IF = 307;
|
||||
const T_ELSEIF = 308;
|
||||
const T_ELSE = 309;
|
||||
const T_ENDIF = 310;
|
||||
const T_LNUMBER = 311;
|
||||
const T_DNUMBER = 312;
|
||||
const T_STRING = 313;
|
||||
const T_STRING_VARNAME = 314;
|
||||
const T_VARIABLE = 315;
|
||||
const T_NUM_STRING = 316;
|
||||
const T_INLINE_HTML = 317;
|
||||
const T_CHARACTER = 318;
|
||||
const T_BAD_CHARACTER = 319;
|
||||
const T_ENCAPSED_AND_WHITESPACE = 320;
|
||||
const T_CONSTANT_ENCAPSED_STRING = 321;
|
||||
const T_ECHO = 322;
|
||||
const T_DO = 323;
|
||||
const T_WHILE = 324;
|
||||
const T_ENDWHILE = 325;
|
||||
const T_FOR = 326;
|
||||
const T_ENDFOR = 327;
|
||||
const T_FOREACH = 328;
|
||||
const T_ENDFOREACH = 329;
|
||||
const T_DECLARE = 330;
|
||||
const T_ENDDECLARE = 331;
|
||||
const T_AS = 332;
|
||||
const T_SWITCH = 333;
|
||||
const T_ENDSWITCH = 334;
|
||||
const T_CASE = 335;
|
||||
const T_DEFAULT = 336;
|
||||
const T_BREAK = 337;
|
||||
const T_CONTINUE = 338;
|
||||
const T_GOTO = 339;
|
||||
const T_FUNCTION = 340;
|
||||
const T_CONST = 341;
|
||||
const T_RETURN = 342;
|
||||
const T_TRY = 343;
|
||||
const T_CATCH = 344;
|
||||
const T_FINALLY = 345;
|
||||
const T_THROW = 346;
|
||||
const T_USE = 347;
|
||||
const T_INSTEADOF = 348;
|
||||
const T_GLOBAL = 349;
|
||||
const T_STATIC = 350;
|
||||
const T_ABSTRACT = 351;
|
||||
const T_FINAL = 352;
|
||||
const T_PRIVATE = 353;
|
||||
const T_PROTECTED = 354;
|
||||
const T_PUBLIC = 355;
|
||||
const T_VAR = 356;
|
||||
const T_UNSET = 357;
|
||||
const T_ISSET = 358;
|
||||
const T_EMPTY = 359;
|
||||
const T_HALT_COMPILER = 360;
|
||||
const T_CLASS = 361;
|
||||
const T_TRAIT = 362;
|
||||
const T_INTERFACE = 363;
|
||||
const T_EXTENDS = 364;
|
||||
const T_IMPLEMENTS = 365;
|
||||
const T_OBJECT_OPERATOR = 366;
|
||||
const T_LIST = 367;
|
||||
const T_ARRAY = 368;
|
||||
const T_CALLABLE = 369;
|
||||
const T_CLASS_C = 370;
|
||||
const T_TRAIT_C = 371;
|
||||
const T_METHOD_C = 372;
|
||||
const T_FUNC_C = 373;
|
||||
const T_LINE = 374;
|
||||
const T_FILE = 375;
|
||||
const T_COMMENT = 376;
|
||||
const T_DOC_COMMENT = 377;
|
||||
const T_OPEN_TAG = 378;
|
||||
const T_OPEN_TAG_WITH_ECHO = 379;
|
||||
const T_CLOSE_TAG = 380;
|
||||
const T_WHITESPACE = 381;
|
||||
const T_START_HEREDOC = 382;
|
||||
const T_END_HEREDOC = 383;
|
||||
const T_DOLLAR_OPEN_CURLY_BRACES = 384;
|
||||
const T_CURLY_OPEN = 385;
|
||||
const T_PAAMAYIM_NEKUDOTAYIM = 386;
|
||||
const T_NAMESPACE = 387;
|
||||
const T_NS_C = 388;
|
||||
const T_DIR = 389;
|
||||
const T_NS_SEPARATOR = 390;
|
||||
const T_ELLIPSIS = 391;
|
||||
const T_COALESCE_EQUAL = 281;
|
||||
const T_COALESCE = 282;
|
||||
const T_BOOLEAN_OR = 283;
|
||||
const T_BOOLEAN_AND = 284;
|
||||
const T_IS_EQUAL = 285;
|
||||
const T_IS_NOT_EQUAL = 286;
|
||||
const T_IS_IDENTICAL = 287;
|
||||
const T_IS_NOT_IDENTICAL = 288;
|
||||
const T_SPACESHIP = 289;
|
||||
const T_IS_SMALLER_OR_EQUAL = 290;
|
||||
const T_IS_GREATER_OR_EQUAL = 291;
|
||||
const T_SL = 292;
|
||||
const T_SR = 293;
|
||||
const T_INSTANCEOF = 294;
|
||||
const T_INC = 295;
|
||||
const T_DEC = 296;
|
||||
const T_INT_CAST = 297;
|
||||
const T_DOUBLE_CAST = 298;
|
||||
const T_STRING_CAST = 299;
|
||||
const T_ARRAY_CAST = 300;
|
||||
const T_OBJECT_CAST = 301;
|
||||
const T_BOOL_CAST = 302;
|
||||
const T_UNSET_CAST = 303;
|
||||
const T_POW = 304;
|
||||
const T_NEW = 305;
|
||||
const T_CLONE = 306;
|
||||
const T_EXIT = 307;
|
||||
const T_IF = 308;
|
||||
const T_ELSEIF = 309;
|
||||
const T_ELSE = 310;
|
||||
const T_ENDIF = 311;
|
||||
const T_LNUMBER = 312;
|
||||
const T_DNUMBER = 313;
|
||||
const T_STRING = 314;
|
||||
const T_STRING_VARNAME = 315;
|
||||
const T_VARIABLE = 316;
|
||||
const T_NUM_STRING = 317;
|
||||
const T_INLINE_HTML = 318;
|
||||
const T_CHARACTER = 319;
|
||||
const T_BAD_CHARACTER = 320;
|
||||
const T_ENCAPSED_AND_WHITESPACE = 321;
|
||||
const T_CONSTANT_ENCAPSED_STRING = 322;
|
||||
const T_ECHO = 323;
|
||||
const T_DO = 324;
|
||||
const T_WHILE = 325;
|
||||
const T_ENDWHILE = 326;
|
||||
const T_FOR = 327;
|
||||
const T_ENDFOR = 328;
|
||||
const T_FOREACH = 329;
|
||||
const T_ENDFOREACH = 330;
|
||||
const T_DECLARE = 331;
|
||||
const T_ENDDECLARE = 332;
|
||||
const T_AS = 333;
|
||||
const T_SWITCH = 334;
|
||||
const T_ENDSWITCH = 335;
|
||||
const T_CASE = 336;
|
||||
const T_DEFAULT = 337;
|
||||
const T_BREAK = 338;
|
||||
const T_CONTINUE = 339;
|
||||
const T_GOTO = 340;
|
||||
const T_FUNCTION = 341;
|
||||
const T_CONST = 342;
|
||||
const T_RETURN = 343;
|
||||
const T_TRY = 344;
|
||||
const T_CATCH = 345;
|
||||
const T_FINALLY = 346;
|
||||
const T_THROW = 347;
|
||||
const T_USE = 348;
|
||||
const T_INSTEADOF = 349;
|
||||
const T_GLOBAL = 350;
|
||||
const T_STATIC = 351;
|
||||
const T_ABSTRACT = 352;
|
||||
const T_FINAL = 353;
|
||||
const T_PRIVATE = 354;
|
||||
const T_PROTECTED = 355;
|
||||
const T_PUBLIC = 356;
|
||||
const T_VAR = 357;
|
||||
const T_UNSET = 358;
|
||||
const T_ISSET = 359;
|
||||
const T_EMPTY = 360;
|
||||
const T_HALT_COMPILER = 361;
|
||||
const T_CLASS = 362;
|
||||
const T_TRAIT = 363;
|
||||
const T_INTERFACE = 364;
|
||||
const T_EXTENDS = 365;
|
||||
const T_IMPLEMENTS = 366;
|
||||
const T_OBJECT_OPERATOR = 367;
|
||||
const T_LIST = 368;
|
||||
const T_ARRAY = 369;
|
||||
const T_CALLABLE = 370;
|
||||
const T_CLASS_C = 371;
|
||||
const T_TRAIT_C = 372;
|
||||
const T_METHOD_C = 373;
|
||||
const T_FUNC_C = 374;
|
||||
const T_LINE = 375;
|
||||
const T_FILE = 376;
|
||||
const T_COMMENT = 377;
|
||||
const T_DOC_COMMENT = 378;
|
||||
const T_OPEN_TAG = 379;
|
||||
const T_OPEN_TAG_WITH_ECHO = 380;
|
||||
const T_CLOSE_TAG = 381;
|
||||
const T_WHITESPACE = 382;
|
||||
const T_START_HEREDOC = 383;
|
||||
const T_END_HEREDOC = 384;
|
||||
const T_DOLLAR_OPEN_CURLY_BRACES = 385;
|
||||
const T_CURLY_OPEN = 386;
|
||||
const T_PAAMAYIM_NEKUDOTAYIM = 387;
|
||||
const T_NAMESPACE = 388;
|
||||
const T_NS_C = 389;
|
||||
const T_DIR = 390;
|
||||
const T_NS_SEPARATOR = 391;
|
||||
const T_ELLIPSIS = 392;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace PhpParser;
|
||||
* turn is based on work by Masato Bito.
|
||||
*/
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Cast\Double;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Scalar\Encapsed;
|
||||
@@ -680,6 +681,20 @@ abstract class ParserAbstract implements Parser
|
||||
return $this->startAttributeStack[$pos] + $this->endAttributeStack[$pos];
|
||||
}
|
||||
|
||||
protected function getFloatCastKind(string $cast): int
|
||||
{
|
||||
$cast = strtolower($cast);
|
||||
if (strpos($cast, 'float') !== false) {
|
||||
return Double::KIND_FLOAT;
|
||||
}
|
||||
|
||||
if (strpos($cast, 'real') !== false) {
|
||||
return Double::KIND_REAL;
|
||||
}
|
||||
|
||||
return Double::KIND_DOUBLE;
|
||||
}
|
||||
|
||||
protected function parseLNumber($str, $attributes, $allowInvalidOctal = false) {
|
||||
try {
|
||||
return LNumber::fromString($str, $attributes, $allowInvalidOctal);
|
||||
|
||||
@@ -260,6 +260,10 @@ class Standard extends PrettyPrinterAbstract
|
||||
return $this->pInfixOp(AssignOp\Pow::class, $node->var, ' **= ', $node->expr);
|
||||
}
|
||||
|
||||
protected function pExpr_AssignOp_Coalesce(AssignOp\Coalesce $node) {
|
||||
return $this->pInfixOp(AssignOp\Coalesce::class, $node->var, ' ??= ', $node->expr);
|
||||
}
|
||||
|
||||
// Binary expressions
|
||||
|
||||
protected function pExpr_BinaryOp_Plus(BinaryOp\Plus $node) {
|
||||
@@ -435,7 +439,15 @@ class Standard extends PrettyPrinterAbstract
|
||||
}
|
||||
|
||||
protected function pExpr_Cast_Double(Cast\Double $node) {
|
||||
return $this->pPrefixOp(Cast\Double::class, '(double) ', $node->expr);
|
||||
$kind = $node->getAttribute('kind', Cast\Double::KIND_DOUBLE);
|
||||
if ($kind === Cast\Double::KIND_DOUBLE) {
|
||||
$cast = '(double)';
|
||||
} elseif ($kind === Cast\Double::KIND_FLOAT) {
|
||||
$cast = '(float)';
|
||||
} elseif ($kind === Cast\Double::KIND_REAL) {
|
||||
$cast = '(real)';
|
||||
}
|
||||
return $this->pPrefixOp(Cast\Double::class, $cast . ' ', $node->expr);
|
||||
}
|
||||
|
||||
protected function pExpr_Cast_String(Cast\String_ $node) {
|
||||
@@ -680,7 +692,9 @@ class Standard extends PrettyPrinterAbstract
|
||||
}
|
||||
|
||||
protected function pStmt_Property(Stmt\Property $node) {
|
||||
return (0 === $node->flags ? 'var ' : $this->pModifiers($node->flags)) . $this->pCommaSeparated($node->props) . ';';
|
||||
return (0 === $node->flags ? 'var ' : $this->pModifiers($node->flags))
|
||||
. ($node->type ? $this->p($node->type) . ' ' : '')
|
||||
. $this->pCommaSeparated($node->props) . ';';
|
||||
}
|
||||
|
||||
protected function pStmt_PropertyProperty(Stmt\PropertyProperty $node) {
|
||||
|
||||
+29
-25
@@ -9,7 +9,6 @@ use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\AssignOp;
|
||||
use PhpParser\Node\Expr\BinaryOp;
|
||||
use PhpParser\Node\Expr\Cast;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Scalar;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
@@ -83,6 +82,7 @@ abstract class PrettyPrinterAbstract
|
||||
AssignOp\ShiftLeft::class => [160, 1],
|
||||
AssignOp\ShiftRight::class => [160, 1],
|
||||
AssignOp\Pow::class => [160, 1],
|
||||
AssignOp\Coalesce::class => [160, 1],
|
||||
Expr\YieldFrom::class => [165, 1],
|
||||
Expr\Print_::class => [168, 1],
|
||||
BinaryOp\LogicalAnd::class => [170, -1],
|
||||
@@ -617,12 +617,14 @@ abstract class PrettyPrinterAbstract
|
||||
return $this->pFallback($fallbackNode);
|
||||
}
|
||||
|
||||
list($findToken, $extraLeft, $extraRight) = $this->insertionMap[$key];
|
||||
list($findToken, $beforeToken, $extraLeft, $extraRight) = $this->insertionMap[$key];
|
||||
if (null !== $findToken) {
|
||||
$subStartPos = $this->origTokens->findRight($pos, $findToken) + 1;
|
||||
$subStartPos = $this->origTokens->findRight($pos, $findToken)
|
||||
+ (int) !$beforeToken;
|
||||
} else {
|
||||
$subStartPos = $pos;
|
||||
}
|
||||
|
||||
if (null === $extraLeft && null !== $extraRight) {
|
||||
// If inserting on the right only, skipping whitespace looks better
|
||||
$subStartPos = $this->origTokens->skipRightWhitespace($subStartPos);
|
||||
@@ -1155,7 +1157,7 @@ abstract class PrettyPrinterAbstract
|
||||
Expr\Assign::class, Expr\AssignRef::class, AssignOp\Plus::class, AssignOp\Minus::class,
|
||||
AssignOp\Mul::class, AssignOp\Div::class, AssignOp\Concat::class, AssignOp\Mod::class,
|
||||
AssignOp\BitwiseAnd::class, AssignOp\BitwiseOr::class, AssignOp\BitwiseXor::class,
|
||||
AssignOp\ShiftLeft::class, AssignOp\ShiftRight::class, AssignOp\Pow::class,
|
||||
AssignOp\ShiftLeft::class, AssignOp\ShiftRight::class, AssignOp\Pow::class, AssignOp\Coalesce::class
|
||||
];
|
||||
foreach ($assignOps as $assignOp) {
|
||||
$this->fixupMap[$assignOp] = [
|
||||
@@ -1209,6 +1211,7 @@ abstract class PrettyPrinterAbstract
|
||||
'Stmt_Function->returnType' => $stripColon,
|
||||
'Stmt_If->else' => $stripLeft,
|
||||
'Stmt_Namespace->name' => $stripLeft,
|
||||
'Stmt_Property->type' => $stripRight,
|
||||
'Stmt_PropertyProperty->default' => $stripEquals,
|
||||
'Stmt_Return->expr' => $stripBoth,
|
||||
'Stmt_StaticVar->default' => $stripEquals,
|
||||
@@ -1226,28 +1229,29 @@ abstract class PrettyPrinterAbstract
|
||||
|
||||
// TODO: "yield" where both key and value are inserted doesn't work
|
||||
$this->insertionMap = [
|
||||
'Expr_ArrayDimFetch->dim' => ['[', null, null],
|
||||
'Expr_ArrayItem->key' => [null, null, ' => '],
|
||||
'Expr_Closure->returnType' => [')', ' : ', null],
|
||||
'Expr_Ternary->if' => ['?', ' ', ' '],
|
||||
'Expr_Yield->key' => [\T_YIELD, null, ' => '],
|
||||
'Expr_Yield->value' => [\T_YIELD, ' ', null],
|
||||
'Param->type' => [null, null, ' '],
|
||||
'Param->default' => [null, ' = ', null],
|
||||
'Stmt_Break->num' => [\T_BREAK, ' ', null],
|
||||
'Stmt_ClassMethod->returnType' => [')', ' : ', null],
|
||||
'Stmt_Class->extends' => [null, ' extends ', null],
|
||||
'Expr_ArrayDimFetch->dim' => ['[', false, null, null],
|
||||
'Expr_ArrayItem->key' => [null, false, null, ' => '],
|
||||
'Expr_Closure->returnType' => [')', false, ' : ', null],
|
||||
'Expr_Ternary->if' => ['?', false, ' ', ' '],
|
||||
'Expr_Yield->key' => [\T_YIELD, false, null, ' => '],
|
||||
'Expr_Yield->value' => [\T_YIELD, false, ' ', null],
|
||||
'Param->type' => [null, false, null, ' '],
|
||||
'Param->default' => [null, false, ' = ', null],
|
||||
'Stmt_Break->num' => [\T_BREAK, false, ' ', null],
|
||||
'Stmt_ClassMethod->returnType' => [')', false, ' : ', null],
|
||||
'Stmt_Class->extends' => [null, false, ' extends ', null],
|
||||
'Expr_PrintableNewAnonClass->extends' => [null, ' extends ', null],
|
||||
'Stmt_Continue->num' => [\T_CONTINUE, ' ', null],
|
||||
'Stmt_Foreach->keyVar' => [\T_AS, null, ' => '],
|
||||
'Stmt_Function->returnType' => [')', ' : ', null],
|
||||
'Stmt_If->else' => [null, ' ', null],
|
||||
'Stmt_Namespace->name' => [\T_NAMESPACE, ' ', null],
|
||||
'Stmt_PropertyProperty->default' => [null, ' = ', null],
|
||||
'Stmt_Return->expr' => [\T_RETURN, ' ', null],
|
||||
'Stmt_StaticVar->default' => [null, ' = ', null],
|
||||
//'Stmt_TraitUseAdaptation_Alias->newName' => [T_AS, ' ', null], // TODO
|
||||
'Stmt_TryCatch->finally' => [null, ' ', null],
|
||||
'Stmt_Continue->num' => [\T_CONTINUE, false, ' ', null],
|
||||
'Stmt_Foreach->keyVar' => [\T_AS, false, null, ' => '],
|
||||
'Stmt_Function->returnType' => [')', false, ' : ', null],
|
||||
'Stmt_If->else' => [null, false, ' ', null],
|
||||
'Stmt_Namespace->name' => [\T_NAMESPACE, false, ' ', null],
|
||||
'Stmt_Property->type' => [\T_VARIABLE, true, null, ' '],
|
||||
'Stmt_PropertyProperty->default' => [null, false, ' = ', null],
|
||||
'Stmt_Return->expr' => [\T_RETURN, false, ' ', null],
|
||||
'Stmt_StaticVar->default' => [null, false, ' = ', null],
|
||||
//'Stmt_TraitUseAdaptation_Alias->newName' => [T_AS, false, ' ', null], // TODO
|
||||
'Stmt_TryCatch->finally' => [null, false, ' ', null],
|
||||
|
||||
// 'Expr_Exit->expr': Complicated due to optional ()
|
||||
// 'Stmt_Case->cond': Conversion from default to case
|
||||
|
||||
Reference in New Issue
Block a user