Skip to content

Commit 0c0676e

Browse files
author
Caitlin Bales (MSFT)
committed
Update checks for naming collisions in PHP
1 parent 07c7ea7 commit 0c0676e

File tree

3 files changed

+76
-70
lines changed

3 files changed

+76
-70
lines changed

Templates/PHP/Model/ComplexType.php.tt

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,45 @@
22
<#@ template debug="true" hostspecific="true" language="C#" #>
33
<#@ output extension="\\" #>
44
<#
5-
CustomT4Host host = (CustomT4Host) Host;
6-
OdcmModel model = host.CurrentModel;
7-
CodeWriterPHP writer = (CodeWriterPHP) host.CodeWriter;
8-
OdcmClass complex = (OdcmClass)host.CurrentType;
9-
#>
10-
<#=writer.WriteHeader(writer.GetDocBlock(complex.Name.ToCheckedCase()))#>
5+
CustomT4Host host = (CustomT4Host) Host;
6+
OdcmModel model = host.CurrentModel;
7+
CodeWriterPHP writer = (CodeWriterPHP) host.CodeWriter;
8+
OdcmClass complex = (OdcmClass)host.CurrentType;
9+
String complexName = complex.Name.SanitizeEntityName();
10+
String complexBaseName = "";
11+
if (complex.Base != null)
12+
complexBaseName = complex.Base.Name.SanitizeEntityName();
13+
#>
14+
<#=writer.WriteHeader(writer.GetDocBlock(complexName.ToCheckedCase()))#>
1115
namespace Microsoft\Graph\Model;
12-
<#=writer.GetClassBlock(complex.Name.ToCheckedCase().ToString(), "Model")#>
16+
<#=writer.GetClassBlock(complexName.ToCheckedCase().ToString(), "Model")#>
1317
<#
1418
if (complex.Base != null) {
1519
#>
16-
class <#=complex.Name.ToCheckedCase()#> extends <#=complex.Base.Name.ToCheckedCase()#>
20+
class <#=complexName.ToCheckedCase()#> extends <#=complexBaseName.ToCheckedCase()#>
1721
<#
1822
} else {
1923
#>
20-
class <#=complex.Name.ToCheckedCase()#> extends Entity
24+
class <#=complexName.ToCheckedCase()#> extends Entity
2125
<#
2226
}
2327
#>
2428
{
2529
<#
2630
foreach(var property in complex.Properties.Where(prop => prop.Type.GetTypeString() != "bytes")){
27-
var propertyName = property.Name.ToUnderscore();
31+
var propertyName = property.Name.SanitizePropertyName(complexName);
2832
if (!property.Type.IsComplex()) {
2933
#>
3034
/**
31-
* Gets the <#=property.Name#>
35+
* Gets the <#=propertyName#>
3236
<# if (property.LongDescription != null) {
3337
#>
3438
* <#=property.LongDescription#>
3539
<# } #>
3640
*
37-
* @return <#=property.Type.GetTypeString()#> The <#=property.Name#>
41+
* @return <#=property.Type.GetTypeString()#> The <#=propertyName#>
3842
*/
39-
public function get<#=property.Name.ToCheckedCase()#>()
43+
public function get<#=propertyName.ToCheckedCase()#>()
4044
{
4145
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
4246
<#
@@ -56,17 +60,17 @@ foreach(var property in complex.Properties.Where(prop => prop.Type.GetTypeString
5660
}
5761

5862
/**
59-
* Sets the <#=property.Name#>
63+
* Sets the <#=propertyName#>
6064
<# if (property.LongDescription != null) {
6165
#>
6266
* <#=property.LongDescription#>
6367
<# } #>
6468
*
65-
* @param <#=property.Type.GetTypeString()#> $val The value of the <#=property.Name#>
69+
* @param <#=property.Type.GetTypeString()#> $val The value of the <#=propertyName#>
6670
*
67-
* @return <#=complex.Name.ToCheckedCase()#>
71+
* @return <#=complexName.ToCheckedCase()#>
6872
*/
69-
public function set<#=property.Name.ToCheckedCase()#>($val)
73+
public function set<#=propertyName.ToCheckedCase()#>($val)
7074
{
7175
<#
7276
if (property.Type.GetTypeString() == "\\DateTime") {
@@ -87,15 +91,15 @@ foreach(var property in complex.Properties.Where(prop => prop.Type.GetTypeString
8791
#>
8892

8993
/**
90-
* Gets the <#=property.Name#>
94+
* Gets the <#=propertyName#>
9195
<# if (property.LongDescription != null) {
9296
#>
9397
* <#=property.LongDescription#>
9498
<# } #>
9599
*
96-
* @return <#=property.Type.GetTypeString()#> The <#=property.Name#>
100+
* @return <#=property.Type.GetTypeString().SanitizeEntityName().ToCheckedCase()#> The <#=propertyName#>
97101
*/
98-
public function get<#=property.Name.ToCheckedCase()#>()
102+
public function get<#=propertyName.ToCheckedCase()#>()
99103
{
100104
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
101105
if (is_a($this->_propDict["<#=property.Name.ToCamelize()#>"], "Microsoft\Graph\Model\<#=property.Type.GetTypeString()#>")) {
@@ -105,7 +109,7 @@ foreach(var property in complex.Properties.Where(prop => prop.Type.GetTypeString
105109
$this->_propDict["<#=property.Name.ToCamelize()#>"] = \GuzzleHttp\Psr7\stream_for($this->_propDict["<#=property.Name.ToCamelize()#>"]);
106110
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
107111
<# } else { #>
108-
$this->_propDict["<#=property.Name.ToCamelize()#>"] = new <#=property.Type.GetTypeString()#>($this->_propDict["<#=property.Name.ToCamelize()#>"]);
112+
$this->_propDict["<#=property.Name.ToCamelize()#>"] = new <#=property.Type.GetTypeString().SanitizeEntityName().ToCheckedCase()#>($this->_propDict["<#=property.Name.ToCamelize()#>"]);
109113
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
110114
<# } #>
111115
}
@@ -114,17 +118,17 @@ foreach(var property in complex.Properties.Where(prop => prop.Type.GetTypeString
114118
}
115119

116120
/**
117-
* Sets the <#=property.Name#>
121+
* Sets the <#=propertyName#>
118122
<# if (property.LongDescription != null) {
119123
#>
120124
* <#=property.LongDescription#>
121125
<# } #>
122126
*
123127
* @param <#=property.Type.GetTypeString()#> $val The value to assign to the <#=property.Name#>
124128
*
125-
* @return <#=complex.Name.ToCheckedCase()#> The <#=complex.Name.ToUpperFirstChar()#>
129+
* @return <#=complexName.ToCheckedCase()#> The <#=complexName.ToCheckedCase()#>
126130
*/
127-
public function set<#=property.Name.ToCheckedCase()#>($val)
131+
public function set<#=propertyName.ToCheckedCase()#>($val)
128132
{
129133
<# if (property.Type.GetTypeString() == "bool") { #>
130134
$this->_propDict["<#=property.Name#>"] = boolval($val);

Templates/PHP/Model/EntityType.php.tt

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@
22
<#@ template debug="true" hostspecific="true" language="C#" #>
33
<#@ output extension="\\" #>
44
<#
5-
CustomT4Host host = (CustomT4Host) Host;
6-
OdcmModel model = host.CurrentModel;
7-
CodeWriterPHP writer = (CodeWriterPHP) host.CodeWriter;
8-
OdcmClass entity = host.CurrentType.AsOdcmClass();
5+
CustomT4Host host = (CustomT4Host) Host;
6+
OdcmModel model = host.CurrentModel;
7+
CodeWriterPHP writer = (CodeWriterPHP) host.CodeWriter;
8+
OdcmClass entity = host.CurrentType.AsOdcmClass();
99
TemplateWriterSettings settings = ConfigurationService.Settings;
10-
10+
String entityName = entity.Name.SanitizeEntityName();
11+
String entityBaseName = "";
12+
if (entity.Base != null)
13+
entityBaseName = entity.Base.Name.SanitizeEntityName();
1114
#>
12-
<#=writer.WriteHeader(writer.GetDocBlock(entity.Name.ToCheckedCase()))#>
15+
<#=writer.WriteHeader(writer.GetDocBlock(entityName.ToCheckedCase()))#>
1316
namespace Microsoft\Graph\Model;
1417

15-
<#=writer.GetClassBlock(entity.Name.ToCheckedCase().ToString(), "Model")#>
18+
<#=writer.GetClassBlock(entityName.ToCheckedCase().ToString(), "Model")#>
1619
<#
1720
if (entity.Name.ToCheckedCase() == "Entity") {
1821
#>
1922
class <#=entity.Name.ToCheckedCase()#> implements \JsonSerializable
2023
<#
2124
} else {
2225
#>
23-
class <#=entity.Name.ToCheckedCase()#> extends <#=entity.Base.Name.ToCheckedCase() #>
26+
class <#=entityName.ToCheckedCase()#> extends <#=entityBaseName.ToCheckedCase() #>
2427
<#
2528
}
2629
#>
@@ -59,22 +62,21 @@ if (entity.Name.ToCheckedCase() == "Entity") {
5962
<#
6063
}
6164
foreach(var property in entity.Properties.Where(prop => prop.Type.GetTypeString() != "bytes")){
62-
var propertyName = property.Name.ToUnderscore();
63-
var propertyNameCap = property.Name.ToUpperFirstChar();
65+
String propertyName = property.Name.SanitizePropertyName(entityName).ToCamelize();
6466
if (property.Type.IsComplex()) {
6567
if (property.IsCollection()) {
6668
#>
6769

6870
/**
69-
* Gets the <#=property.Name#>
71+
* Gets the <#=propertyName#>
7072
<# if (property.LongDescription != null) {
7173
#>
7274
* <#=property.LongDescription#>
7375
<# } #>
7476
*
75-
* @return array The <#=property.Name#>
77+
* @return array The <#=propertyName#>
7678
*/
77-
public function get<#=property.Name.ToCheckedCase()#>()
79+
public function get<#=propertyName.ToCheckedCase()#>()
7880
{
7981
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
8082
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
@@ -84,17 +86,17 @@ foreach(var property in entity.Properties.Where(prop => prop.Type.GetTypeString(
8486
}
8587

8688
/**
87-
* Sets the <#=property.Name#>
89+
* Sets the <#=propertyName#>
8890
<# if (property.LongDescription != null) {
8991
#>
9092
* <#=property.LongDescription#>
9193
<# } #>
9294
*
93-
* @param string $val The <#=property.Name#>
95+
* @param string $val The <#=propertyName#>
9496
*
9597
* @return <#=entity.Name.ToCheckedCase()#>
9698
*/
97-
public function set<#=property.Name.ToCheckedCase()#>($val)
99+
public function set<#=propertyName.ToCheckedCase()#>($val)
98100
{
99101
$this->_propDict["<#=property.Name.ToCamelize()#>"] = $val;
100102
return $this;
@@ -104,15 +106,15 @@ foreach(var property in entity.Properties.Where(prop => prop.Type.GetTypeString(
104106
} else {
105107
#>
106108
/**
107-
* Gets the <#=property.Name#>
109+
* Gets the <#=propertyName#>
108110
<# if (property.LongDescription != null) {
109111
#>
110112
* <#=property.LongDescription#>
111113
<# } #>
112114
*
113-
* @return <#=property.Type.GetTypeString()#> The <#=property.Name#>
115+
* @return <#=property.Type.GetTypeString().SanitizeEntityName().ToCheckedCase()#> The <#=propertyName#>
114116
*/
115-
public function get<#=property.Name.ToCheckedCase()#>()
117+
public function get<#=propertyName.ToCheckedCase()#>()
116118
{
117119
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
118120
if (is_a($this->_propDict["<#=property.Name.ToCamelize()#>"], "Microsoft\Graph\Model\<#=property.Type.GetTypeString()#>")) {
@@ -122,7 +124,7 @@ foreach(var property in entity.Properties.Where(prop => prop.Type.GetTypeString(
122124
$this->_propDict["<#=property.Name.ToCamelize()#>"] = \GuzzleHttp\Psr7\stream_for($this->_propDict["<#=property.Name.ToCamelize()#>"]);
123125
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
124126
<# } else { #>
125-
$this->_propDict["<#=property.Name.ToCamelize()#>"] = new <#=property.Type.GetTypeString()#>($this->_propDict["<#=property.Name.ToCamelize()#>"]);
127+
$this->_propDict["<#=property.Name.ToCamelize()#>"] = new <#=property.Type.GetTypeString().SanitizeEntityName().ToCheckedCase()#>($this->_propDict["<#=property.Name.ToCamelize()#>"]);
126128
return $this->_propDict["<#=property.Name.ToCamelize()#>"];
127129
<# } #>
128130
}
@@ -131,17 +133,17 @@ foreach(var property in entity.Properties.Where(prop => prop.Type.GetTypeString(
131133
}
132134

133135
/**
134-
* Sets the <#=property.Name#>
136+
* Sets the <#=propertyName#>
135137
<# if (property.LongDescription != null) {
136138
#>
137139
* <#=property.LongDescription#>
138140
<# } #>
139141
*
140-
* @param string $val The <#=property.Name#>
142+
* @param string $val The <#=propertyName#>
141143
*
142144
* @return <#=entity.Name.ToCheckedCase()#>
143145
*/
144-
public function set<#=property.Name.ToCheckedCase()#>($val)
146+
public function set<#=propertyName.ToCheckedCase()#>($val)
145147
{
146148
<# if (property.Type.GetTypeString() == "bool") { #>
147149
$this->_propDict["<#=property.Name.ToCamelize()#>"] = boolval($val);
@@ -158,15 +160,15 @@ foreach(var property in entity.Properties.Where(prop => prop.Type.GetTypeString(
158160
} else {
159161
#>
160162
/**
161-
* Gets the <#=property.Name#>
163+
* Gets the <#=propertyName#>
162164
<# if (property.LongDescription != null) {
163165
#>
164166
* <#=property.LongDescription#>
165167
<# } #>
166168
*
167-
* @return <#=property.Type.GetTypeString()#> The <#=property.Name#>
169+
* @return <#=property.Type.GetTypeString()#> The <#=propertyName#>
168170
*/
169-
public function get<#=property.Name.ToCheckedCase()#>()
171+
public function get<#=propertyName.ToCheckedCase()#>()
170172
{
171173
if (array_key_exists("<#=property.Name.ToCamelize()#>", $this->_propDict)) {
172174
<#
@@ -186,17 +188,17 @@ foreach(var property in entity.Properties.Where(prop => prop.Type.GetTypeString(
186188
}
187189

188190
/**
189-
* Sets the <#=property.Name#>
191+
* Sets the <#=propertyName#>
190192
<# if (property.LongDescription != null) {
191193
#>
192194
* <#=property.LongDescription#>
193195
<# } #>
194196
*
195-
* @param <#=property.Type.GetTypeString()#> $val The <#=property.Name#>
197+
* @param <#=property.Type.GetTypeString()#> $val The <#=propertyName#>
196198
*
197199
* @return <#=entity.Name.ToCheckedCase()#>
198200
*/
199-
public function set<#=property.Name.ToCheckedCase()#>($val)
201+
public function set<#=propertyName.ToCheckedCase()#>($val)
200202
{
201203
<#
202204
if (property.Type.GetTypeString() == "\\DateTime") {

src/GraphODataTemplateWriter/CodeHelpers/PHP/TypeHelperPHP.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ namespace Microsoft.Graph.ODataTemplateWriter.CodeHelpers.PHP
99

1010
public static class TypeHelperPHP
1111
{
12-
public const string ReservedPrefix = "msgraph_";
12+
public const string ReservedPrefix = "Graph";
1313
public static HashSet<string> ReservedNames
1414
{
1515
get
1616
{
1717
return new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
18-
"abstract", "and", "array()", "as", "break", "callable", "case", "catch", "class", "clone", "const", "continue", "declare", "default", "die()",
19-
"do", "echo", "else", "elseif", "empty()", "enddeclare", "endfor", "endforeach", "endif", "endswitch", "endwhile", "eval()", "exit()", "extends",
18+
"abstract", "and", "array", "as", "break", "callable", "case", "catch", "class", "clone", "const", "continue", "declare", "default", "die",
19+
"do", "echo", "else", "elseif", "empty", "enddeclare", "endfor", "endforeach", "endif", "endswitch", "endwhile", "eval", "exit", "extends",
2020
"final", "finally", "for", "foreach", "function", "global", "goto", "if", "implements", "include", "include_once", "instanceof", "insteadof", "interface",
21-
"isset()", "list()", "namespace", "new", "or", "print", "private", "protected", "public", "require", "require_once", "return", "static", "switch",
22-
"throw", "trait", "try", "unset()", "use", "var", "while", "xor", "yield", "int", "float", "bool", "string", "true", "false", "null"
21+
"isset", "list", "namespace", "new", "or", "print", "private", "protected", "public", "require", "require_once", "return", "static", "switch",
22+
"throw", "trait", "try", "unset", "use", "var", "while", "xor", "yield", "int", "float", "bool", "string", "true", "false", "null"
2323
};
2424
}
2525
}
@@ -116,25 +116,25 @@ public static string GetToLowerFirstCharName(this OdcmProperty property)
116116
return property.Name.ToLowerFirstChar();
117117
}
118118

119-
public static string SanitizePropertyName(this OdcmObject property)
119+
public static string SanitizeEntityName(this String name)
120120
{
121-
if (ReservedNames.Contains(property.Name))
121+
if (ReservedNames.Contains(name))
122122
{
123-
return ReservedPrefix + property.Name;
123+
return ReservedPrefix + name.ToCheckedCase();
124124
}
125125

126-
return property.Name.Replace("@", string.Empty).Replace(".", "_");
126+
return name.Replace("@", string.Empty).Replace(".", "_");
127127
}
128128

129-
public static string GetToLowerImport(this OdcmProperty property)
129+
public static string SanitizePropertyName(this String name, String entityName)
130130
{
131-
var type = property.Projection.Type;
132-
var index = type.Name.LastIndexOf('.');
133-
return type.Name.Substring(0, index).ToLower() + type.Name.Substring(index);
134-
}
135-
public static string GetNamespaceName(this OdcmNamespace namespaceObject)
136-
{
137-
return Inflector.Inflector.Titleize(namespaceObject.Name);
131+
if (name == "properties")
132+
{
133+
return entityName + name.ToCheckedCase();
134+
}
135+
136+
return name.Replace("@", string.Empty).Replace(".", "_");
138137
}
138+
139139
}
140140
}

0 commit comments

Comments
 (0)