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

This commit is contained in:
Simon Welsh 2013-05-05 12:19:31 +12:00
parent 7fbb9e3a96
commit 835aefbe83
8 changed files with 64 additions and 8 deletions

View File

@ -211,7 +211,12 @@ abstract class Object {
}
} 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);

View File

@ -282,10 +282,9 @@ class SS_ConfigStaticManifest_Parser {
$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;
}

View File

@ -252,7 +252,7 @@ class i18nTextCollector extends Object {
$inConcat = true;
} elseif($inTransFn && $token == ',') {
$inConcat = false;
} elseif($inTransFn && ($token == ')' || $finalTokenDueToArray)) {
} elseif($inTransFn && ($token == ')' || $finalTokenDueToArray || $token == '[')) {
// finalize definition
$inTransFn = false;
$inConcat = false;

View File

@ -395,6 +395,18 @@ class ObjectTest extends SapphireTest {
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()),

View File

@ -163,4 +163,23 @@ DOC;
$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']);
}
}

View File

@ -0,0 +1,8 @@
<?php
class ConfigStaticManifestTestMyObject implements TestOnly {
static private $db = [
'Name' => 'Varchar',
'Description' => 'Text',
];
}

View File

@ -327,6 +327,14 @@ _t("i18nTestModule.INJECTIONS3", "Hello {name} {greeting}. But it is late, {good
"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 @@ PHP;
'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);