Commit graph

37 commits

Author SHA1 Message Date
Brent Shaffer
7d396a9cf3 fix(php): add missing reserved keywords (object, mixed, and never) (#27475)
See https://github.com/googleapis/google-cloud-php/issues/9193

protoc is generating the invalid class name `Object` when it should be prefixed with `PB` to prevent a conflict with PHP reserved words. I've updated this library to generate files correctly, as well as updated the `GPBUtil` to look for the prefixed class. I've also added the `never` and `mixed` keywords, which are also reserved and cannot be used as classnames.

To cleanup this code I've also introduced https://github.com/protocolbuffers/protobuf/pull/27456, which introduces a script to keep all our code and tests in sync when updating/modifying keywords

Closes #27475

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/27475 from bshaffer:php-add-missing-reserved-words 8938259b85
PiperOrigin-RevId: 927083089
2026-06-04 22:46:38 -07:00
Fabio Silva
6b5128c4db [php][ext] - Handle NULL optional constructor arg (#21224)
Should fix : https://github.com/protocolbuffers/protobuf/issues/21207

Closes #21224

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/21224 from ChessCom:php-constructor-null-optional b3996235b1
PiperOrigin-RevId: 872954782
2026-02-20 10:04:05 -08:00
Rachel Goldfinger
6556a4ea26 Update php and php_ext tests to include 8.5.0
PiperOrigin-RevId: 862834652
2026-01-29 11:53:41 -08:00
Brent Shaffer
aee03b7892 feat(php): Add PHP typehints for setters and remove redundant GPBUtil checks (#25296)
This PR modifies `php_generator.cc` to:
- Add PHP typehints to setter method signatures for all types (for 64-bit integers, the type hint is `int|string` to maintain compatibility with 32-bit systems)
- Remove redundant `GPBUtil::check...` for primitive types and messages (the PHP type hint provides sufficient validation)
- Retain `GPBUtil::check...` for 32 and 64-bit integers, strings (for UTF-8 validation), enums, maps, and repeated fields

Additionally makes the following changes:
- Updates descriptor protos with generator changes
- Suppresses float to int "loss of precision" warnings in tests which expect it
- Removes compatibility in tests with PHPUnit 6, which is no longer needed
- Proper casing in gencode for `true` and `false` (instead of `True` and `False`)

Closes #25296

PiperOrigin-RevId: 856990912
2026-01-15 23:44:59 -08:00
Joshua Haberman
94fe944f49 Automated rollback of commit 8f569de0ba.
PiperOrigin-RevId: 843812355
2025-12-12 13:31:24 -08:00
Tom Arbesser
938d3bdf86 fix(php): Fix double to bool casting (use Z_DVAL_P: -0.0 -> false) (#24072)
Updates the `IS_DOUBLE` branch to use `Z_DVAL_P(from)` instead of `Z_LVAL_P(from)`. The old code was reading from the integer slot, which isn’t defined for doubles and could return garbage.

The new logic follows PHP's own casting rules: 0.0 and -0.0 are `false`, everything else (1.5, INF, -INF, NAN) is `true`.

Added a PHPT test that fails with the old code but passes with this change.

No API changes, just correctness and consistency with PHP itself.

Closes #24072

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/24072 from TomA-R:php-double-bool-cast 7e99380ad9
PiperOrigin-RevId: 822146903
2025-10-21 09:20:46 -07:00
Protobuf Team Bot
9fbce62597 fix(php): use count instead of ->count() to avoid bug in c-extension
PiperOrigin-RevId: 761147122
2025-05-20 10:56:31 -07:00
Sandy Zhang
8f569de0ba Automated rollback of commit f9863dfd3c.
PiperOrigin-RevId: 749061493
2025-04-18 09:51:22 -07:00
Fabio Silva
f9863dfd3c [php][ext] Prevent Seg fault if constructor is not called (#21240)
Should fix : https://github.com/protocolbuffers/protobuf/issues/19978

Closes #21240

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/21240 from ChessCom:php-ext-fault-constructor 8772e96c32
PiperOrigin-RevId: 747660794
2025-04-14 19:26:40 -07:00
Brent Shaffer
e04fe69a9f feat(php): improve return typehint when repeatedfield (#11734)
Adds the type of the `RepeatedField` to the PHPDoc, e.g.

```php
/**
 * @return \Google\Protobuf\Internal\RepeatedField<int>
 */
```

Whereas before, the `<int>` part was not included.

This is the "getter" counterpart for https://github.com/protocolbuffers/protobuf/pull/4533

Closes #11734

PiperOrigin-RevId: 726881276
2025-02-14 05:06:51 -08:00
Nathan Baulch
e4cbc79ab4 Fix minor typos (#17682)
Just thought I'd contribute some typo fixes that keep tripping up CI/CD checks in my projects. Nothing controversial (hopefully), just 174 simple fixes.

Use the following command to get a quick and dirty summary of the specific corrections made:
```shell
git diff HEAD^! --word-diff-regex='\w+' -U0 \
  | grep -E '\[\-.*\-\]\{\+.*\+\}' \
  | sed -r 's/.*\[\-(.*)\-\]\{\+(.*)\+\}.*/\1 \2/' \
  | sort | uniq -c | sort -n
```

FWIW, the top typos are:
* trimed → trimmed (37)
* substract → subtract (7)
* qualifed → qualified (7)
* extesion → extension (6)
* mising → missing (5)
* btye → byte (4)
* likey → likely (4)
* candicate → candidate (3)
* decriptor → descriptor (3)
* inherting → inheriting (3)
* colletion → collection (3)
* caluclated → calculated (3)
* unititialized → uninitialized (3)
* implemting → implementing (3)
* binrary → binary (3)
* descripor → descriptor (3)
* negitive → negative (3)

Closes #17682

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/17682 from NathanBaulch:typos d41762d137
PiperOrigin-RevId: 677074418
2024-09-20 20:50:06 -07:00
Brent Shaffer
6d84da5e2d fix: do not throw deprecated warning on field getters for default values (#17788)
fixes https://github.com/protocolbuffers/protobuf/issues/13428

Wraps deprecated field getters in a conditional to see if the value has been set before throwing the warning. This is because this method is used internally, so users can get deprecated warnings even when they are not using the field.

Closes #17788

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/17788 from bshaffer:wrap-deprecation-in-conditional 084c87dc12
PiperOrigin-RevId: 665346195
2024-08-20 07:07:22 -07:00
Joshua Haberman
60b80c6976 Breaking Change: Remove deprecated legacy class aliases.
Fixes: https://github.com/protocolbuffers/protobuf/issues/9593
Fixes: https://github.com/protocolbuffers/protobuf/issues/15696
PiperOrigin-RevId: 603777295
2024-02-02 13:47:40 -08:00
Brent Shaffer
f929439ccd feat: more readable phpdoc escaping (#11208)
The PHPDoc escaping in PHP is aggressive in that it escapes some character sequences that don't need to be escaped (`/*`), and it uses HTML entities to escape others (`*/` and `@`) instead of the recommended PHPDoc escape sequences.

For Example, in [`Google\Api\RoutingParameter`](https://github.com/googleapis/common-protos-php/blob/main/src/Api/RoutingParameter.php#L42):

```
 * path_template: "projects/&#42;&#47;{table_location=instances/&#42;}/tables/&#42;"
```

Should be escaped as:

```
 * path_template: "projects/{@*}{table_location=instances/*}/tables/*"
```

according to [the PHPDoc guide](https://manual.phpdoc.org/HTMLframesConverter/default/phpDocumentor/tutorial_phpDocumentor.howto.pkg.html#basics.desc):

 - For `@`: "if you need an actual "@" in your DocBlock's description parts, you should be careful to either ensure it is not the first character on a line, or else escape it ("\\@") to avoid it being interpreted as a PhpDocumentor tag marker."

 - For `*/`: " If you need to use the closing comment "\*/" in a DocBlock, use the special escape sequence "{@*}."

Closes #11208

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/11208 from bshaffer:more-readable-phpdoc-escaping a75f9745ea
PiperOrigin-RevId: 603091642
2024-01-31 10:31:42 -08:00
Joshua Haberman
d14dbbc838 Breaking Change: Validate UTF-8 in string setters, [as previously announced](https://protobuf.dev/news/2023-12-27/#php-breaking-changes).
Pure-PHP was already validating UTF-8, but this makes the C extension validate also.

PiperOrigin-RevId: 597695655
2024-01-11 17:30:47 -08:00
Brent Shaffer
c752a43e1e fix: fix PHP readonly legacy files for nested messages 2022-07-26 18:20:23 -07:00
Brent Shaffer
53944b5983 Add back "fix: reserve "ReadOnly" keyword for PHP 8.1 and add compatibility (#9633)" 2022-05-25 10:49:37 -07:00
Joshua Haberman
8984184e54 Revert "fix: reserve "ReadOnly" keyword for PHP 8.1 and add compatibility (#9633)"
This reverts commit eb27c201f1.
2022-05-24 21:06:00 +00:00
Noah Dietz
4d56f2d83c
Change enum string name for reserved words (#9780)
* Change enum string name for reserved words

Update PHP descriptor protos

* conditionally generate value compat code
2022-04-26 09:44:39 -07:00
Brent Shaffer
eb27c201f1
fix: reserve "ReadOnly" keyword for PHP 8.1 and add compatibility (#9633)
* fix: reserve "ReadOnly" keyword for PHP 8.1 and add compatibility

* typo fix - readme > readonly
2022-04-12 15:48:14 -07:00
Brent Shaffer
ce57b5bab6
fix: [PHP] add missing reserved classnames (#9458)
* fix: add missing reserved classnames

* Try to remove PARENT and SELF from reserved constants

* add back for tests

* add to validConstantNames

* update kReservedNamesSize
2022-02-10 09:16:20 -08:00
Joshua Haberman
67995fb4e9 Fixed data loss bug with PHP C extension. 2022-01-27 17:06:41 -08:00
Joshua Haberman
44df0abc02 Added support for PHP 8.1 (currently in RC1). 2021-09-09 21:32:11 -07:00
Brent Shaffer
bade043986 update reserved names: add finally, fn, match, yield 2021-06-29 07:56:08 -07:00
deannagarcia
f5a0e3988b
Merge branch 'master' into 3.17.x 2021-06-04 17:22:24 +00:00
Joshua Haberman
75de6aa21a Fixed sub-message getters for well-known types when message is unset.
The well-known types generate C code into wkt.inc, and this C code was
not testing isset($msg->submsg_field) like the generated code does:

```php
// PHP generated getter: checks isset().
public function getOptions()
{
    return isset($this->options) ? $this->options : null;
}
```

```c
// C generated getter, does not check upb_msg_has()
static PHP_METHOD(google_protobuf_Value, getListValue) {
  Message* intern = (Message*)Z_OBJ_P(getThis());
  const upb_fielddef *f = upb_msgdef_ntofz(intern->desc->msgdef,
                                           "list_value");
  zval ret;
  Message_get(intern, f, &ret);
  RETURN_COPY_VALUE(&ret);
}
```

This led to an error where we wnuld try to get a sub-message field from upb
when it `upb_msg_has(msg, field) == false`, which is an error according to upb.

There are two possible fixes for this bug. A guiding principle is that we want
the generated C code in wkt.inc to have the same behavior as PHP generated
code. Following this principle, the two possible fixes are:

1. Change the code generator for wkt.inc to check upb_msg_has(f) before
   calling Message_get(). This would match the isset() check that the
   The PHP generated code does, and we would leave the PHP code unchanged.

2. Change Message_get() to itself perform the upb_msg_has(f) check for
   sub-message fields. This means that generated code would no longer need
   to perform an isset() check, so we would want to remove this check from
   the PHP generated code also to avoid a redundant check.

Both of these are reasonable fixes, and it is not immediately obvious which is
better. (1) has the benefit of resolving this case when we are in more
specialized code (a getter function that already knows this is a sub-message
field), and therefore avoids performing the check later in more generic code
that would have to test the type again. On the other hand, the isset() check is
not needed for the pure PHP implementation, as an unset PHP variable will
return `null` anyway. And for the C extension, we'd rather check upb_msg_has()
at the C level instead of PHP.

So this change implements (2). The generated code in wkt.inc remains unchanged,
and the PHP generated code for sub-message fields is changed to remove the
isset() check.
2021-05-27 10:45:18 -07:00
Joshua Haberman
0b87475592
Php & Ruby Cherry Picks for 3.17.1 (#8632)
* Some more updates to PHP testing infrastructure (#8576)

* WIP.

* Added build config for all of the tests.

* Use ../src/protoc if it is available, for cases where Bazel isn't available.

* Added test_php.sh.

* Fix for the broken macOS tests.

* Move all jobs to use php80 instead of lots of separate jobs.

* Only pass -t flag if we are running in a terminal.

* Updated php_all job to use new Docker stuff.

* Fixed PHP memory leaks and arginfo errors (#8614)

* Fixed a bunch of incorrect arginfo and a few incorrect error messages.

* Passes mem check test with no leaks!

* WIP.

* Fix build warning that was causing Bazel build to fail.

* Added compatibility code for PHP <8.0.

* Added test_valgrind target and made tests Valgrind-clean.

* Updated Valgrind test to fail if memory leaks are detected.

* Removed intermediate shell script so commands are easier to cut, paste, and modify.

* Passing all Valgrind tests!

* Hoist addref into ObjCache_Get().

* Removed special case of map descriptors by keying object map on upb_msgdef.

* Removed all remaining RETURN_ZVAL() macros.

* Removed all explicit reference add/del operations.

* Added REFCOUNTING.md to Makefile.am.

* Updated upb version and fixed PHP to not get unset message field. (#8621)

* Updated upb version and fixed PHP to not get unset message field.

* Updated changelog.

* Fixed preproc test to handle old versions of Clang withot __has_attribute().

* A second try at fixing __has_attribute().

* Copy __has_attribute() fix to cc file also.

* Updated failure list for PHP for fixed test.

* Updated version of upb for Ruby (#8624)

* Updated upb.

* Preserve legacy behavior for unset messages.

* Updated failure list.

* Updated CHANGES.txt.

* Added erroneously-deleted test file.

* Fixed condition on compatibility code.

* Re-introduced deleted file again, and fixed Rakefile to not delete it.

* Fix generation of test protos.
2021-05-19 15:53:47 -07:00
Joshua Haberman
7e95c64dfb
[PHP] Fixed $msg->setMessage(null) to properly clear the message. (#8472)
* [PHP] Fixed $msg->setMessage(null) to properly clear the message.

Fixes: https://github.com/protocolbuffers/protobuf/issues/8457

* Changed pure PHP to throw TypeError, and added a test for null.

* Added more tests and fixed null in setter for oneof.
2021-05-14 16:48:45 -07:00
Joshua Haberman
b0d90e3abe
Fixed PHP memory leaks and arginfo errors (#8614)
* Fixed a bunch of incorrect arginfo and a few incorrect error messages.

* Passes mem check test with no leaks!

* WIP.

* Fix build warning that was causing Bazel build to fail.

* Added compatibility code for PHP <8.0.

* Added test_valgrind target and made tests Valgrind-clean.

* Updated Valgrind test to fail if memory leaks are detected.

* Removed intermediate shell script so commands are easier to cut, paste, and modify.

* Passing all Valgrind tests!

* Hoist addref into ObjCache_Get().

* Removed special case of map descriptors by keying object map on upb_msgdef.

* Removed all remaining RETURN_ZVAL() macros.

* Removed all explicit reference add/del operations.

* Added REFCOUNTING.md to Makefile.am.
2021-05-13 23:16:49 -07:00
Joshua Haberman
8b87075924
Updated and simplified PHP testing structure (#8558)
* Simplified PHP testing setup.

- Consolidated on a single autoloader, created by composer.
- Consolidated on a single phpunit invocation strategy: we run
  phpunit on a directory, which will run all tests matching *Test.php
  in that directory.
- We now rely on autoloading to import all test protos. require_once()
  calls for test protos are removed.
- For now the valgrind tests are removed. A follow-up PR will re-enable
  them in a more robust way.

* More improvements to PHP testing.

1. Replace custom PHPUnit-selection logic in test.sh with generic
   composer version selection.
2. Optimized both test proto generation and the custom extension
   build to avoid unnecessary work when the files are already up
   to date.

* Added assertions to verify that the C test doesn't use PHP sources.

* Updated tests.sh for the new PHP testing commands.

* Removed obsolete rules from tests.sh.

* Fixed generate_test_protos.sh for when tmp does not exist.

Also removed undefined_test.php and fixed Makefile.am.

* Added php8.0_all again which is still used.

* Added missing file to Makefile.am.

* Re-added php_all_32 rule which is also still used.

* Updated testing commands for macOS and download composer.

* Use /usr/local/bin on mac instead of /usr/bin, since the latter is not writable.
2021-05-04 10:19:22 -07:00
Joshua Haberman
f3e53a05d6
Fixed clone for Message, RepeatedField, and MapField. (#8245)
Also updated the code to use a TypeInfo struct for convenient
passing of type and desc together. This simplified a lot of code
and made this change easier to write.
2021-02-02 09:15:10 -08:00
Joshua Haberman
d18df4f764
Protect against stack overflow if the user derives from Message. (#8248)
* Protect against stack overflow if the user derives from Message.

* For pure-PHP, change error into an exception.
2021-02-01 20:38:15 -08:00
Christian Alexander Wolf
f4d0f7c85e
feat: add support for deprecated fields to PHP compiler (#8223)
* feat: add support for deprecated fields to PHP compiler

* PR feedback 1
2021-01-29 11:33:27 -08:00
Paul Yang
823f351448
Port for php8 (#8105)
* Port for php8

* Port php c extension for php8

* Update composer.json

* Drop php7.0 support

* Update phpunit for php7.1 in c extension test

* Add back support for php7.0

* Add badge for php8 continuous build
2020-12-04 13:46:34 -08:00
Joshua Haberman
a8a1493f35 Moved HasOneof test into GeneratedClassTest.php, to avoid the extra file. 2020-11-04 11:32:43 -08:00
Joshua Haberman
f0942fb0a2
Equals passes tests in both pure-PHP and the C extension. (#7883) 2020-09-14 13:40:07 -07:00
Paul Yang
d4ca92962c
Port php c extension to php8 (#7793)
* Only ported c extension to php8.
* Didn't fixed the issue of throwing warnings for missing arginfo in bundled files.
* Tests not fixed, because syntax of phpunit (<7 vs >9.3) are not compatible.
* In next release, needs to drop php5 and php7.0 support (in order to use phpunit > 7)
2020-08-11 19:30:46 -07:00
Renamed from php/tests/generated_class_test.php (Browse further)