Skip to content

Commit

Permalink
prevent fields named with reserved words from causing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Mancini committed May 11, 2016
1 parent b918940 commit 8885e60
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion ProtobufCompiler/FieldDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,55 @@ class FieldDescriptor
'bytes' => 'string'
);

private static $_reservedWords = array(
'ABSTRACT',
'AND',
'AS',
'BREAK',
'CALLABLE',
'CASE',
'CATCH',
'CLASS',
'CLONE',
'CONST',
'CONTINUE',
'DECLARE',
'DEFAULT',
'DO',
'ECHO',
'EXTENDS',
'FINAL',
'FOR',
'FUNCTION',
'GLOBAL',
'GLOBAL',
'IF',
'IMPLEMENTS',
'INCLUDE',
'INSTANCEOF',
'INSTEADOF',
'INTERFACE',
'NAMESPACE',
'NEW',
'OR',
'PRINT',
'PRIVATE',
'PROTECTED',
'PUBLIC',
'REQUIRE',
'RETURN',
'STATIC',
'SWITCH',
'THROW',
'TRAIT',
'TRY',
'USE',
'VAR',
'WHILE',
'XOR',
'YIELD',
);

private $_default;
private $_label;
private $_name;
Expand Down Expand Up @@ -96,7 +145,14 @@ public function getCamelCaseName()
public function getConstName()
{
$chunks = preg_split('/[^a-z0-9]/is', $this->getName());
return implode('_', array_map('strtoupper', $chunks));
$constant = implode('_', array_map('strtoupper', $chunks));

// intercept reserved word field names and append '_FIELD' to avoid unexpected errors
if (in_array($constant, static::$_reservedWords)) {
$constant = $constant . '_FIELD';
}

return $constant;
}

/**
Expand Down

0 comments on commit 8885e60

Please sign in to comment.