Skip to content
This repository was archived by the owner on Jan 12, 2021. It is now read-only.

Commit cbea42f

Browse files
authored
Fixed unset field default values (#94)
1 parent 3ab9e6a commit cbea42f

File tree

5 files changed

+69
-26
lines changed

5 files changed

+69
-26
lines changed

src/Allegro/Protobuf/Compiler/PhpGenerator.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,11 @@ private function _describeSingleField(FieldDescriptor $field, CodeStringBuffer $
661661
if ($phpType != 'object') {
662662
$typeName = $phpType;
663663
$argumentClass = '';
664+
$returnCast = '(' . $phpType . ')';
664665
} else {
665666
$typeName = $this->_createFullyQualifiedClassName($field->getTypeDescriptor());
666667
$argumentClass = $typeName . ' ';
668+
$returnCast = '';
667669
}
668670

669671
$comment = new CommentStringBuffer(self::TAB, self::EOL);
@@ -703,12 +705,7 @@ private function _describeSingleField(FieldDescriptor $field, CodeStringBuffer $
703705
->append($comment)
704706
->append('public function get' . $field->getCamelCaseName() . '()')
705707
->append('{')
706-
->append(
707-
'return ' .
708-
'$this->get(self::' . $field->getConstName() . ');',
709-
false,
710-
1
711-
)
708+
->append('return ' . $returnCast . '$this->get(self::' . $field->getConstName() . ');', false, 1)
712709
->append('}');
713710
}
714711

tests/Bar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Auto generated from test.proto at 2016-08-24 20:32:21
3+
* Auto generated from test.proto at 2016-08-29 12:36:15
44
*/
55

66
namespace {
@@ -68,7 +68,7 @@ public function setDoubleField($value)
6868
*/
6969
public function getDoubleField()
7070
{
71-
return $this->get(self::DOUBLE_FIELD);
71+
return (double)$this->get(self::DOUBLE_FIELD);
7272
}
7373
}
7474
}

tests/Baz.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Auto generated from test.proto at 2016-08-24 20:32:21
3+
* Auto generated from test.proto at 2016-08-29 12:36:15
44
*/
55

66
namespace {
@@ -68,7 +68,7 @@ public function setId($value)
6868
*/
6969
public function getId()
7070
{
71-
return $this->get(self::ID);
71+
return (integer)$this->get(self::ID);
7272
}
7373
}
7474
}

tests/Foo.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Auto generated from test.proto at 2016-08-24 20:32:21
3+
* Auto generated from test.proto at 2016-08-29 12:36:15
44
*/
55

66
namespace {
@@ -257,7 +257,7 @@ public function setDoubleField($value)
257257
*/
258258
public function getDoubleField()
259259
{
260-
return $this->get(self::DOUBLE_FIELD);
260+
return (double)$this->get(self::DOUBLE_FIELD);
261261
}
262262

263263
/**
@@ -279,7 +279,7 @@ public function setFloatField($value)
279279
*/
280280
public function getFloatField()
281281
{
282-
return $this->get(self::FLOAT_FIELD);
282+
return (double)$this->get(self::FLOAT_FIELD);
283283
}
284284

285285
/**
@@ -301,7 +301,7 @@ public function setInt32Field($value)
301301
*/
302302
public function getInt32Field()
303303
{
304-
return $this->get(self::INT32_FIELD);
304+
return (integer)$this->get(self::INT32_FIELD);
305305
}
306306

307307
/**
@@ -323,7 +323,7 @@ public function setInt64Field($value)
323323
*/
324324
public function getInt64Field()
325325
{
326-
return $this->get(self::INT64_FIELD);
326+
return (integer)$this->get(self::INT64_FIELD);
327327
}
328328

329329
/**
@@ -345,7 +345,7 @@ public function setUint32Field($value)
345345
*/
346346
public function getUint32Field()
347347
{
348-
return $this->get(self::UINT32_FIELD);
348+
return (integer)$this->get(self::UINT32_FIELD);
349349
}
350350

351351
/**
@@ -367,7 +367,7 @@ public function setUint64Field($value)
367367
*/
368368
public function getUint64Field()
369369
{
370-
return $this->get(self::UINT64_FIELD);
370+
return (integer)$this->get(self::UINT64_FIELD);
371371
}
372372

373373
/**
@@ -389,7 +389,7 @@ public function setSint32Field($value)
389389
*/
390390
public function getSint32Field()
391391
{
392-
return $this->get(self::SINT32_FIELD);
392+
return (integer)$this->get(self::SINT32_FIELD);
393393
}
394394

395395
/**
@@ -411,7 +411,7 @@ public function setSint64Field($value)
411411
*/
412412
public function getSint64Field()
413413
{
414-
return $this->get(self::SINT64_FIELD);
414+
return (integer)$this->get(self::SINT64_FIELD);
415415
}
416416

417417
/**
@@ -433,7 +433,7 @@ public function setFixed32Field($value)
433433
*/
434434
public function getFixed32Field()
435435
{
436-
return $this->get(self::FIXED32_FIELD);
436+
return (integer)$this->get(self::FIXED32_FIELD);
437437
}
438438

439439
/**
@@ -455,7 +455,7 @@ public function setFixed64Field($value)
455455
*/
456456
public function getFixed64Field()
457457
{
458-
return $this->get(self::FIXED64_FIELD);
458+
return (integer)$this->get(self::FIXED64_FIELD);
459459
}
460460

461461
/**
@@ -477,7 +477,7 @@ public function setSfixed32Field($value)
477477
*/
478478
public function getSfixed32Field()
479479
{
480-
return $this->get(self::SFIXED32_FIELD);
480+
return (integer)$this->get(self::SFIXED32_FIELD);
481481
}
482482

483483
/**
@@ -499,7 +499,7 @@ public function setSfixed64Field($value)
499499
*/
500500
public function getSfixed64Field()
501501
{
502-
return $this->get(self::SFIXED64_FIELD);
502+
return (integer)$this->get(self::SFIXED64_FIELD);
503503
}
504504

505505
/**
@@ -521,7 +521,7 @@ public function setBoolField($value)
521521
*/
522522
public function getBoolField()
523523
{
524-
return $this->get(self::BOOL_FIELD);
524+
return (boolean)$this->get(self::BOOL_FIELD);
525525
}
526526

527527
/**
@@ -543,7 +543,7 @@ public function setStringField($value)
543543
*/
544544
public function getStringField()
545545
{
546-
return $this->get(self::STRING_FIELD);
546+
return (string)$this->get(self::STRING_FIELD);
547547
}
548548

549549
/**
@@ -565,7 +565,7 @@ public function setBytesField($value)
565565
*/
566566
public function getBytesField()
567567
{
568-
return $this->get(self::BYTES_FIELD);
568+
return (string)$this->get(self::BYTES_FIELD);
569569
}
570570

571571
/**

tests/unset_field_default_values.phpt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
Protocol Buffers unset field default values
3+
--SKIPIF--
4+
<?php require 'skipif.inc' ?>
5+
--FILE--
6+
<?php
7+
require 'Foo.php';
8+
9+
$foo = new Foo();
10+
var_dump($foo->getDoubleField());
11+
var_dump($foo->getFloatField());
12+
var_dump($foo->getInt32Field());
13+
var_dump($foo->getInt64Field());
14+
var_dump($foo->getUint32Field());
15+
var_dump($foo->getUint64Field());
16+
var_dump($foo->getSint32Field());
17+
var_dump($foo->getSint64Field());
18+
var_dump($foo->getFixed32Field());
19+
var_dump($foo->getFixed64Field());
20+
var_dump($foo->getSfixed32Field());
21+
var_dump($foo->getSfixed64Field());
22+
var_dump($foo->getBoolField());
23+
var_dump($foo->getStringField());
24+
var_dump($foo->getBytesField());
25+
var_dump($foo->getRepeatedField());
26+
var_dump($foo->getEmbeddedField());
27+
?>
28+
--EXPECT--
29+
float(0)
30+
float(0)
31+
int(0)
32+
int(0)
33+
int(0)
34+
int(0)
35+
int(0)
36+
int(0)
37+
int(0)
38+
int(0)
39+
int(0)
40+
int(0)
41+
bool(false)
42+
string(0) ""
43+
string(0) ""
44+
array(0) {
45+
}
46+
NULL

0 commit comments

Comments
 (0)