From 9e43acee2c68050c5188548e55987c9c6bc60bf9 Mon Sep 17 00:00:00 2001 From: nikic Date: Fri, 19 Oct 2012 15:17:08 +0200 Subject: [PATCH] Scalar_String::create() -> Scalar_String::parse() Directly creating the node isn't necessary anymore, the token only needs to be parsed. This makes it consistent with the other scalar parsing methods and removes the need to pass $arguments around. --- grammar/zend_language_parser.phpy | 4 ++-- lib/PHPParser/Node/Scalar/String.php | 15 ++++++--------- lib/PHPParser/Parser.php | 4 ++-- test/PHPParser/Tests/Node/Scalar/StringTest.php | 6 +++--- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/grammar/zend_language_parser.phpy b/grammar/zend_language_parser.phpy index e5212e6a..8e11c858 100644 --- a/grammar/zend_language_parser.phpy +++ b/grammar/zend_language_parser.phpy @@ -611,7 +611,7 @@ array_expr: scalar_dereference: array_expr '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; } | T_CONSTANT_ENCAPSED_STRING '[' dim_offset ']' - { $$ = Expr_ArrayDimFetch[Scalar_String::create($1, $attributes), $3]; } + { $$ = Expr_ArrayDimFetch[Scalar_String[Scalar_String::parse($1)], $3]; } | scalar_dereference '[' dim_offset ']' { $$ = Expr_ArrayDimFetch[$1, $3]; } /* alternative array syntax missing intentionally */ ; @@ -716,7 +716,7 @@ ctor_arguments: common_scalar: T_LNUMBER { $$ = Scalar_LNumber[Scalar_LNumber::parse($1)]; } | T_DNUMBER { $$ = Scalar_DNumber[Scalar_DNumber::parse($1)]; } - | T_CONSTANT_ENCAPSED_STRING { $$ = Scalar_String::create($1, $attributes); } + | T_CONSTANT_ENCAPSED_STRING { $$ = Scalar_String[Scalar_String::parse($1)]; } | T_LINE { $$ = Scalar_LineConst[]; } | T_FILE { $$ = Scalar_FileConst[]; } | T_DIR { $$ = Scalar_DirConst[]; } diff --git a/lib/PHPParser/Node/Scalar/String.php b/lib/PHPParser/Node/Scalar/String.php index d1f6d5fd..0465b5e5 100644 --- a/lib/PHPParser/Node/Scalar/String.php +++ b/lib/PHPParser/Node/Scalar/String.php @@ -32,30 +32,27 @@ class PHPParser_Node_Scalar_String extends PHPParser_Node_Scalar } /** - * Creates a String node from a string token (parses escape sequences). + * Parses a string token. * - * @param string $str String - * @param array $attributes Additional attributes + * @param string $str String token content * - * @return PHPParser_Node_Scalar_String String Node + * @return string The parsed string */ - public static function create($str, array $attributes = array()) { + public static function parse($str) { $bLength = 0; if ('b' === $str[0]) { $bLength = 1; } if ('\'' === $str[$bLength]) { - $str = str_replace( + return str_replace( array('\\\\', '\\\''), array( '\\', '\''), substr($str, $bLength + 1, -1) ); } else { - $str = self::parseEscapeSequences(substr($str, $bLength + 1, -1), '"'); + return self::parseEscapeSequences(substr($str, $bLength + 1, -1), '"'); } - - return new self($str, $attributes); } /** diff --git a/lib/PHPParser/Parser.php b/lib/PHPParser/Parser.php index 6c5de27e..207b719e 100644 --- a/lib/PHPParser/Parser.php +++ b/lib/PHPParser/Parser.php @@ -2147,7 +2147,7 @@ class PHPParser_Parser } protected function yyn267($attributes) { - $this->yyval = new PHPParser_Node_Expr_ArrayDimFetch(PHPParser_Node_Scalar_String::create($this->yyastk[$this->stackPos-(4-1)], $attributes), $this->yyastk[$this->stackPos-(4-3)], $attributes); + $this->yyval = new PHPParser_Node_Expr_ArrayDimFetch(new PHPParser_Node_Scalar_String(PHPParser_Node_Scalar_String::parse($this->yyastk[$this->stackPos-(4-1)]), $attributes), $this->yyastk[$this->stackPos-(4-3)], $attributes); } protected function yyn268($attributes) { @@ -2321,7 +2321,7 @@ class PHPParser_Parser } protected function yyn307($attributes) { - $this->yyval = PHPParser_Node_Scalar_String::create($this->yyastk[$this->stackPos-(1-1)], $attributes); + $this->yyval = new PHPParser_Node_Scalar_String(PHPParser_Node_Scalar_String::parse($this->yyastk[$this->stackPos-(1-1)]), $attributes); } protected function yyn308($attributes) { diff --git a/test/PHPParser/Tests/Node/Scalar/StringTest.php b/test/PHPParser/Tests/Node/Scalar/StringTest.php index cce76173..04dd35b6 100644 --- a/test/PHPParser/Tests/Node/Scalar/StringTest.php +++ b/test/PHPParser/Tests/Node/Scalar/StringTest.php @@ -13,12 +13,12 @@ class PHPParser_Tests_Node_Scalar_StringTest extends PHPUnit_Framework_TestCase } /** - * @dataProvider provideTestCreate + * @dataProvider provideTestParse */ public function testCreate($expected, $string) { $this->assertEquals( $expected, - PHPParser_Node_Scalar_String::create($string)->value + PHPParser_Node_Scalar_String::parse($string) ); } @@ -37,7 +37,7 @@ class PHPParser_Tests_Node_Scalar_StringTest extends PHPUnit_Framework_TestCase ); } - public function provideTestCreate() { + public function provideTestParse() { $tests = array( array('A', '\'A\''), array('A', 'b\'A\''),