Permalink
Browse files

FIX Handle PHP 5.4's short array notation everywhere arrays are parsed.

  • Loading branch information...
1 parent 7fbb9e3 commit 835aefbe83401ac16d1c368d64a688960e7cfbbb @simonwelsh simonwelsh committed May 5, 2013
View
@@ -211,7 +211,12 @@ public static function parse_class_spec($classSpec) {
}
} else {
- if($tName == ')') {
+ if($tName == '[') {
+ // Add an empty array to the bucket
+ $bucket[] = array();
+ $bucketStack[] = &$bucket;
+ $bucket = &$bucket[sizeof($bucket)-1];
+ } elseif($tName == ')' || $tName == ']') {
// Pop-by-reference
$bucket = &$bucketStack[sizeof($bucketStack)-1];
array_pop($bucketStack);
@@ -282,10 +282,9 @@ function parseStatic($access, $class) {
$type = is_array($token) ? $token[0] : $token;
// Track array nesting depth
- if($type == T_ARRAY) {
+ if($type == T_ARRAY || $type == '[') {
$depth += 1;
- }
- else if($type == ')') {
+ } elseif($type == ')' || $type == ']') {
$depth -= 1;
}
@@ -332,4 +331,4 @@ function parseStatic($access, $class) {
if($token == ',') $this->parseStatic($access, $class);
}
-}
+}
@@ -252,7 +252,7 @@ public function collectFromCode($content, $module) {
$inConcat = true;
} elseif($inTransFn && $token == ',') {
$inConcat = false;
- } elseif($inTransFn && ($token == ')' || $finalTokenDueToArray)) {
+ } elseif($inTransFn && ($token == ')' || $finalTokenDueToArray || $token == '[')) {
// finalize definition
$inTransFn = false;
$inConcat = false;
@@ -630,4 +630,4 @@ public static function GetTranslatables($template) {
return self::$entities;
}
-}
+}
View
@@ -395,6 +395,18 @@ public function testParseClassSpec() {
Object::parse_class_spec(
"Enum(array('Accepted', 'Pending', 'Declined', array('UnsubmittedA','UnsubmittedB')), 'Unsubmitted')")
);
+ // 5.4 Shorthand Array
+ $this->assertEquals(
+ array('Enum',array(array('Accepted', 'Pending', 'Declined', 'Unsubmitted'), 'Unsubmitted')),
+ Object::parse_class_spec("Enum(['Accepted', 'Pending', 'Declined', 'Unsubmitted'), 'Unsubmitted']")
+ );
+ // 5.4 Nested shorthand array
+ $this->assertEquals(
+ array('Enum',array(array('Accepted', 'Pending', 'Declined', array('UnsubmittedA','UnsubmittedB')),
+ 'Unsubmitted')),
+ Object::parse_class_spec(
+ "Enum(['Accepted', 'Pending', 'Declined', ['UnsubmittedA','UnsubmittedB']], 'Unsubmitted')")
+ );
// Namespaced class
$this->assertEquals(
array('Test\MyClass', array()),
@@ -163,4 +163,23 @@ public function testIgnoresStaticMethods() {
$statics = $this->parseSelf()->getStatics();
$this->assertNull(@$statics[__CLASS__]['static_method']);
}
-}
+
+ public function testParsingShortArray() {
+ if(version_compare(PHP_VERSION, '5.4', '<')) {
+ $this->markTestSkipped('This test requires PHP 5.4 or higher');
+ return;
+ }
+
+ $parser = new SS_ConfigStaticManifest_Parser(__DIR__ . '/ConfigStaticManifestTest/ConfigStaticManifestTestMyObject.php');
+ $parser->parse();
+
+ $statics = $parser->getStatics();
+
+ $expectedValue = array(
+ 'Name' => 'Varchar',
+ 'Description' => 'Text',
+ );
+
+ $this->assertEquals($expectedValue, $statics['ConfigStaticManifestTestMyObject']['db']['value']);
+ }
+}
@@ -0,0 +1,8 @@
+<?php
+
+class ConfigStaticManifestTestMyObject implements TestOnly {
+ static private $db = [
+ 'Name' => 'Varchar',
+ 'Description' => 'Text',
+ ];
+}
@@ -327,6 +327,14 @@ public function testCollectFromCodeNewSignature() {
"New context (this should be ignored)",
array("name"=>"Steffen", "greeting"=>"willkommen", "goodbye"=>"wiedersehen"));
_t('i18nTestModule.INJECTIONS4', array("name"=>"Cat", "greeting"=>"meow", "goodbye"=>"meow"));
+_t('i18nTestModule.INJECTIONS5','_DOES_NOT_EXIST', "Hello {name} {greeting}. But it is late, {goodbye}",
+ ["name"=>"Mark", "greeting"=>"welcome", "goodbye"=>"bye"]);
+_t('i18nTestModule.INJECTIONS6', "Hello {name} {greeting}. But it is late, {goodbye}",
+ ["name"=>"Paul", "greeting"=>"good you are here", "goodbye"=>"see you"]);
+_t("i18nTestModule.INJECTIONS7", "Hello {name} {greeting}. But it is late, {goodbye}",
+ "New context (this should be ignored)",
+ ["name"=>"Steffen", "greeting"=>"willkommen", "goodbye"=>"wiedersehen"]);
+_t('i18nTestModule.INJECTIONS8', ["name"=>"Cat", "greeting"=>"meow", "goodbye"=>"meow"]);
PHP;
$collectedTranslatables = $c->collectFromCode($php, 'mymodule');
@@ -338,6 +346,11 @@ public function testCollectFromCodeNewSignature() {
'i18nTestModule.INJECTIONS2' => array("Hello {name} {greeting}. But it is late, {goodbye}"),
'i18nTestModule.INJECTIONS3' => array("Hello {name} {greeting}. But it is late, {goodbye}",
"New context (this should be ignored)"),
+ 'i18nTestModule.INJECTIONS5' => array("_DOES_NOT_EXIST",
+ "Hello {name} {greeting}. But it is late, {goodbye}"),
+ 'i18nTestModule.INJECTIONS6' => array("Hello {name} {greeting}. But it is late, {goodbye}"),
+ 'i18nTestModule.INJECTIONS7' => array("Hello {name} {greeting}. But it is late, {goodbye}",
+ "New context (this should be ignored)"),
));
ksort($expectedArray);

0 comments on commit 835aefb

Please sign in to comment.