Commit graph

1111 commits

Author SHA1 Message Date
DanielEScherzer
54003aecc4
Fix description of ReflectionParameter::getPosition() (GH-16738)
Appears to have been copied from `ReflectionParameter::isOptional()`.

[skip ci]
2024-11-09 15:32:53 +01:00
DanielEScherzer
10f1f924cf
Add ReflectionConstant::getExtension() and ::getExtensionName() (#16603) 2024-11-09 11:08:02 +01:00
Niels Dossche
64f2d11e38
Merge branch 'PHP-8.4'
* PHP-8.4:
  Fix GH-16628: FPM logs are getting corrupted with this log statement
  Fix GH-16601: Memory leak in Reflection constructors
2024-11-02 19:39:00 +01:00
Niels Dossche
bfd9e0cca3
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3:
  Fix GH-16628: FPM logs are getting corrupted with this log statement
  Fix GH-16601: Memory leak in Reflection constructors
2024-11-02 19:38:54 +01:00
Niels Dossche
16cda10650
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-16628: FPM logs are getting corrupted with this log statement
  Fix GH-16601: Memory leak in Reflection constructors
2024-11-02 19:37:28 +01:00
Niels Dossche
f0f666ba3f
Fix GH-16601: Memory leak in Reflection constructors
Additionally fixes wrong behaviour in ReflectionParameter when you first
have a construction that uses an object and the subsequent doesn't.

Closes GH-16672.
2024-11-02 19:35:20 +01:00
Daniel Scherzer
f5e743a520
Add ReflectionConstant::getFileName()
Allow determining the name of the file that defined a constant, when the
constant was defined in userland code via const or define(). For constants
defined by PHP core or extensions, false is returned, matching the existing
getFileName() methods on other reflection classes.

Fixes GH-15723
Closes GH-15847
2024-10-31 16:47:45 +01:00
Arnaud Le Blanc
50a3f019dc
Merge branch 'PHP-8.4'
* PHP-8.4:
  Add ReflectionProperty::isLazy()
2024-10-31 14:15:49 +01:00
Arnaud Le Blanc
54a40f3bde
Add ReflectionProperty::isLazy()
Closes GH-16342
2024-10-31 14:14:20 +01:00
DanielEScherzer
f4e203103d
ext/reflection: use ZEND_PARSE_PARAMETERS_NONE(); macro (#16605) 2024-10-26 10:57:43 +02:00
Tim Düsterhus
3da6818c9e
reflection: Use fast ZPP for ReflectionProperty::(get|set)Value() (#16329)
During the Doctrine Core Team Meetup 2024 the Doctrine team investigated the
performance overhead of using `setRawValueWithoutLazyInitialization()` instead
of `setValue()` and came to the surprising conclusion that
`setRawValueWithoutLazyInitialization()` outperformed `setValue()`, despite
doing more work.

These two scripts are used as the benchmark:

    <?php

    class Foo
    {
        public $id;
        public $foo1;
        public $foo2;
        public $foo3;
        public $foo4;
    }

    $reflection = new ReflectionClass(Foo::class);
    $properties = $reflection->getProperties();

    for ($i = 0; $i < 1000000; $i++) {
        $foo = new Foo();
        foreach ($properties as $property) {
            $property->setValue($foo, 1);
        }
    }

and

    <?php

    class Foo
    {
        public $id;
        public $foo1;
        public $foo2;
        public $foo3;
        public $foo4;
    }

    $reflection = new ReflectionClass(Foo::class);
    $properties = $reflection->getProperties();

    for ($i = 0; $i < 1000000; $i++) {
        $foo = new Foo();
        foreach ($properties as $property) {
            $property->setRawValueWithoutLazyInitialization($foo, 1);
        }
    }

Benchmarking these with a current git master shows that `setValue()` is 50%
slower:

    $ hyperfine -L script setValue,setRawValueWithoutLazyInitialization '/tmp/php-before /tmp/test/{script}.php'
    Benchmark 1: /tmp/php-before /tmp/test/setValue.php
      Time (mean ± σ):     216.0 ms ±   5.8 ms    [User: 212.0 ms, System: 3.7 ms]
      Range (min … max):   208.2 ms … 225.3 ms    13 runs

    Benchmark 2: /tmp/php-before /tmp/test/setRawValueWithoutLazyInitialization.php
      Time (mean ± σ):     145.6 ms ±   3.6 ms    [User: 141.6 ms, System: 3.8 ms]
      Range (min … max):   140.4 ms … 152.8 ms    20 runs

    Summary
      /tmp/php-before /tmp/test/setRawValueWithoutLazyInitialization.php ran
        1.48 ± 0.05 times faster than /tmp/php-before /tmp/test/setValue.php

Looking into the “why” revealed that the `setValue()` script spent quite some
time in `zend_parse_parameters()`.

A 50% overhead can be significant, given that `setValue()` is commonly called
several thousand times in a single request when using Doctrine.

This commit changes the non-static property case of `setValue()` to make use of
the fast parameter parsing API and adjusts `getValue()` for consistency.

The resulting comparison shows that both `setValue()` and
`setRawValueWithoutLazyInitialization()` are now (almost) equal:

    $ hyperfine -L script setValue,setRawValueWithoutLazyInitialization 'sapi/cli/php /tmp/test/{script}.php'
    Benchmark 1: sapi/cli/php /tmp/test/setValue.php
      Time (mean ± σ):     143.0 ms ±   6.4 ms    [User: 139.4 ms, System: 3.4 ms]
      Range (min … max):   134.8 ms … 157.7 ms    18 runs

    Benchmark 2: sapi/cli/php /tmp/test/setRawValueWithoutLazyInitialization.php
      Time (mean ± σ):     147.0 ms ±   5.5 ms    [User: 143.0 ms, System: 3.6 ms]
      Range (min … max):   139.9 ms … 159.8 ms    19 runs

    Summary
      sapi/cli/php /tmp/test/setValue.php ran
        1.03 ± 0.06 times faster than sapi/cli/php /tmp/test/setRawValueWithoutLazyInitialization.php
2024-10-10 09:19:53 +02:00
Ilija Tovilo
b34f22d801
Merge branch 'PHP-8.4'
* PHP-8.4:
  Fix GH-16162: No ReflectionProperty::IS_VIRTUAL
2024-10-07 14:18:36 +02:00
Daniel Scherzer
76e5d82eb2
Fix GH-16162: No ReflectionProperty::IS_VIRTUAL
Closes GH-16166
2024-10-07 14:17:40 +02:00
Niels Dossche
5ffe6f1644
Merge branch 'PHP-8.4'
* PHP-8.4:
  Fix GH-16187: ReflectionClass::__toString() with packed properties hash table
2024-10-05 10:21:30 +02:00
Niels Dossche
6631aa9cc3
Merge branch 'PHP-8.3' into PHP-8.4 2024-10-05 10:20:49 +02:00
Niels Dossche
158ba541c2
Merge branch 'PHP-8.2' into PHP-8.3 2024-10-05 10:18:56 +02:00
Daniel Scherzer
331da7e869
Fix GH-16187: ReflectionClass::__toString() with packed properties hash table
Closes GH-16192.
2024-10-05 10:18:11 +02:00
DanielEScherzer
8b8a6733d1
ext/reflection: fix some typos [skip ci] (#16183) 2024-10-03 07:55:25 +02:00
Tim Düsterhus
624d0a89ae
Merge branch 'PHP-8.4'
* PHP-8.4:
  reflection: Fix the return value of ReflectionFunction::{getNamespaceName,inNamespace}() for closures (#16129)
2024-09-30 16:34:55 +02:00
Tim Düsterhus
a1cc091808
reflection: Fix the return value of ReflectionFunction::{getNamespaceName,inNamespace}() for closures (#16129)
* reflection: Fix the return value of ReflectionFunction::{getNamespaceName,inNamespace}() for closures

Fixes GH-16122

* reflection: Clean up implementation of `ReflectionFunctionAbstract::inNamespace()`

* reflection: Clean up implementation of `ReflectionFunctionAbstract::getNamespaceName()`
2024-09-30 16:33:46 +02:00
Máté Kocsis
5070fbf161
Some tidying-up related to property existence checks 2024-09-26 22:36:22 +02:00
DanielEScherzer
71edc05139
php_reflection.c: make a bunch of pointers const (#15927)
* php_reflection.c: make a bunch of pointers `const`

* _function_closure_string: use %u for unsigned

Co-authored-by: Niels Dossche <7771979+nielsdos@users.noreply.github.com>

* _extension_class_string: make indent pointer `const`

Co-authored-by: Ilija Tovilo <ilija.tovilo@me.com>

---------

Co-authored-by: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
Co-authored-by: Ilija Tovilo <ilija.tovilo@me.com>
2024-09-17 03:17:46 +02:00
DanielEScherzer
65b4f22686
Fix some misleading comments about __clone() never being executed (#15926)
For the `Exception`, `ReflectionClass`, and `ReflectionAttribute` classes, the
`__clone()` method is declared to be private, and the implementation has a
comment that it should never be executed. However, the implementation can be
executed by using a `ReflectionMethod`. Fix the comments to instead explain why
the implementation is needed.

[skip ci]
2024-09-17 01:53:30 +02:00
Ilija Tovilo
2fce0bb877
Implement ReflectionProperty::isFinal()
Closes GH-15919
2024-09-16 23:22:52 +02:00
Ilija Tovilo
d75a289f6f
Implement ReflectionProperty::hasHook[s]
Closes GH-15844
2024-09-16 14:28:41 +02:00
DanielEScherzer
2ced1c926b
Add ReflectionProperty::isDynamic() as an alternative to isDefault() (#15758)
Dynamic properties are generally referred to as "dynamic" properties, while
non-dynamic properties are not commonly referred to as "default" properties.
Thus, the existing method `ReflectionProperty::isDefault()` has a non obvious
name; while an alias could be added for `isNotDynamic()`, a new `isDynamic()`
method seems cleaner. The new method returns the opposite of `isDefault()`;
dynamic properties are not present on the class by default, and properties
present by default are not added dynamically.

Closes GH-15754
2024-09-11 10:51:38 +02:00
Daniel Scherzer
18df69ee34
ReflectionProperty::get{Hook,Hooks}(): handle dynamic properties
For dynamic properties, instead of crashing with a segmentation fault, just say
that there are no hooks. Also includes a test to prevent regression.

Fixes GH-15718
Closes GH-15721
2024-09-03 11:31:15 +02:00
Arnaud Le Blanc
58aa6fc830
Lazy objects
RFC: https://wiki.php.net/rfc/lazy-objects

Closes GH-15019
2024-08-30 17:30:03 +02:00
Ilija Tovilo
8df557ac42
[RFC] Asymmetric visibility v2 (GH-15063)
Co-authored-by: Larry Garfield <larry@garfieldtech.com>
2024-08-27 02:04:48 +02:00
Niels Dossche
f78d5cfcd2 Allow ZEND_ACC_VIRTUAL to be used to not have property backing storage without resorting to hooks
This is useful to reduce the memory usage of objects that don't actually
use the backing storage. Examples are XMLReader and DOM. When the
properties were added to the stubs, these objects became much much
bigger, which is a waste of memory.

Closes GH-11644.

Work towards GH-13988.
2024-08-26 21:17:49 +02:00
Ilija Tovilo
780a8280d2
[RFC] Property hooks (#13455)
RFC: https://wiki.php.net/rfc/property-hooks

Co-authored-by: Nikita Popov <nikita.ppv@gmail.com>
2024-07-14 11:55:03 +02:00
Arnaud Le Blanc
11accb5cdf
Preferably include from build dir (#13516)
* Include from build dir first

This fixes out of tree builds by ensuring that configure artifacts are included
from the build dir.

Before, out of tree builds would preferably include files from the src dir, as
the include path was defined as follows (ignoring includes from ext/ and sapi/) :

    -I$(top_builddir)/main
    -I$(top_srcdir)
    -I$(top_builddir)/TSRM
    -I$(top_builddir)/Zend
    -I$(top_srcdir)/main
    -I$(top_srcdir)/Zend
    -I$(top_srcdir)/TSRM
    -I$(top_builddir)/

As a result, an out of tree build would include configure artifacts such as
`main/php_config.h` from the src dir.

After this change, the include path is defined as follows:

    -I$(top_builddir)/main
    -I$(top_builddir)
    -I$(top_srcdir)/main
    -I$(top_srcdir)
    -I$(top_builddir)/TSRM
    -I$(top_builddir)/Zend
    -I$(top_srcdir)/Zend
    -I$(top_srcdir)/TSRM

* Fix extension include path for out of tree builds

* Include config.h with the brackets form

`#include "config.h"` searches in the directory containing the including-file
before any other include path. This can include the wrong config.h when building
out of tree and a config.h exists in the source tree.

Using `#include <config.h>` uses exclusively the include path, and gives
priority to the build dir.
2024-06-26 00:26:43 +02:00
Ilija Tovilo
998bce117c
Show enum cases in errors
Closes GH-14496
2024-06-10 22:58:25 +02:00
Tim Düsterhus
8a87206211
reflection: Add ReflectionGenerator::isClosed() (#14358)
* reflection: Add `ReflectionGenerator::isClosed()`

see https://github.com/php/php-src/pull/14167#issuecomment-2133641998

* Fix test expectation

* Drop `{{{` / `}}}` comments around `ReflectionGenerator::isClosed()`
2024-05-29 19:07:09 +02:00
Tim Düsterhus
8094bd1b58
Make ReflectionGenerator::getFunction() legal after generator termination (#14167)
* Make `ReflectionGenerator::getFunction()` legal after generator termination

* Expose the generator function name via `Generator::__debugInfo()`

* Allow creating `ReflectionGenerator` after termination

* Reorder `struct _zend_generator` to avoid a hole

* Adjust `ext/reflection/tests/028.phpt`

This is legal now.

* Fix Generator Closure collection

* Add test to verify the Closure dies with the generator

* NEWS / UPGRADING
2024-05-21 08:54:51 +02:00
Tim Düsterhus
c90c4fe553
Add zend_get_attribute_object() (#14161)
* Add `zend_get_attribute_object()`

This makes the implementation for `ReflectionAttribute::newInstance()`
reusable.

* Add test for the stack trace behavior of ReflectionAttribute::newInstance()

This test ensures that the `filename` parameter for the fake stack frame is
functional. Without it, the stack trace would show `[internal function]` for
frame `#0`.

* Fix return type of `call_attribute_constructor`
2024-05-14 08:39:43 +02:00
Ilija Tovilo
2ba30f0299
Merge branch 'PHP-8.3'
* PHP-8.3:
  Delay #[Attribute] arg validation until runtime
2024-05-06 12:48:45 +02:00
Ilija Tovilo
480d08a70a
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Delay #[Attribute] arg validation until runtime
2024-05-06 12:48:32 +02:00
Ilija Tovilo
f8d1864bbb
Delay #[Attribute] arg validation until runtime
Fixes GH-13970
Closes GH-14105

We cannot validate at compile-time for multiple reasons:

* Evaluating the argument naively with zend_get_attribute_value can lead to code
  execution at compile time through the new expression, leading to possible
  reentrance of the compiler.
* Even if the evaluation was possible, it would need to be restricted to the
  current file, because constant values coming from other files can change
  without affecting the current compilation unit. For this reason, validation
  would need to be repeated at runtime anyway.
* Enums cannot be instantiated at compile-time (the actual bug report). This
  could be allowed here, because the value is immediately destroyed. But given
  the other issues, this won't be needed.

Instead, we just move it to runtime entirely. It's only needed for
ReflectionAttribute::newInstance(), which is not particularly a hot path. The
checks are also simple.
2024-05-06 12:38:56 +02:00
Tim Düsterhus
6e8b134023
reflection: Fix ReflectionFunction::getShortName() for first class callables (#14087)
Fix fixes an incorrect fix in php/php-src#14001.
2024-04-30 17:27:11 +02:00
Tim Düsterhus
b5ffac7f6a
Add ReflectionClassConstant::isDeprecated() (#14086)
This is in preparation for php/php-src#11293 and for consistency with
ReflectionConstant::isDeprecated() that was added in php/php-src#13669.
2024-04-30 17:26:27 +02:00
Tim Düsterhus
d03d436528
reflection: Fix ReflectionFunction::getShortName() for Closures (#14001)
see php/php-src#13550
2024-04-19 12:14:16 +02:00
Ilija Tovilo
e23440e5a6
Implement reflection constant
Fixes GH-13570
Closes GH-13669
2024-04-17 22:53:09 +02:00
Niels Dossche
700fbca58d
Change getThis() into ZEND_THIS where possible (#13641) 2024-03-08 22:19:06 +01:00
Máté Kocsis
f2e199e878
Implement "support doc comments for internal classes and functions" (#13266)
Fixes #13130
2024-02-25 08:41:31 +01:00
Máté Kocsis
4b405d8520
Display class constant and property doc comments via reflection (#13499) 2024-02-25 08:39:41 +01:00
Niels Dossche
3b5986db69 Implement GH-12908: Show attribute name/class in ReflectionAttribute dump
This is consistent with how many other Reflection classes have a name
field, and it makes debugging easier.

Closes GH-12908.
Closes GH-12917.
2024-02-04 23:02:02 +01:00
Tim Düsterhus
97b3b4552d
random: Move CSPRNG API into php_random_csprng.h (#13290)
This allows consumers of just the CSPRNG to include a much smaller header. It
also allows to verify at a glance whether a source file might use non-secure
randomness.

This commit includes the new header wherever the CSPRNG is used, possibly
replacing the inclusion of php_random.h if nothing else is used, but also
includes it in the main php_random.h header for compatibility.

Somewhat related to 45f8cfaf10,
2b30f18708, and
b14dd85dca.
2024-02-01 19:09:35 +01:00
Máté Kocsis
688c6f373c Deprecate calling ReflectionMethod::__construct() with 1 argument 2023-12-04 22:27:59 +01:00
Ilija Tovilo
3433dab5f7
Revert 479e659331
There were 4 different reports of this breaking behavior. This is higher than I
expected. This bug fix may still be desirable, but should be discussed on the
list beforehand.

Closes GH-12127
2023-09-05 16:14:28 +02:00