Skip to content

Commit d154369

Browse files
feature #541 [8.5] Add polyfill for DelayedTargetValidation (DanielEScherzer)
This PR was merged into the 1.x branch. Discussion ---------- [8.5] Add polyfill for DelayedTargetValidation Commits ------- 17524a5 [8.5] Add polyfill for DelayedTargetValidation
2 parents 22d170f + 17524a5 commit d154369

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Polyfills are provided for:
7878
- the `get_error_handler` and `get_exception_handler` functions introduced in PHP 8.5;
7979
- the `NoDiscard` attribute introduced in PHP 8.5;
8080
- the `array_first` and `array_last` functions introduced in PHP 8.5;
81+
- the `DelayedTargetValidation` attribute introduced in PHP 8.5;
8182

8283
It is strongly recommended to upgrade your PHP version and/or install the missing
8384
extensions whenever possible. This polyfill should be used only when there is no

src/Php85/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This component provides features added to PHP 8.5 core:
66
- [`get_error_handler` and `get_exception_handler`](https://wiki.php.net/rfc/get-error-exception-handler)
77
- [`NoDiscard`](https://wiki.php.net/rfc/marking_return_value_as_important)
88
- [`array_first` and `array_last`](https://wiki.php.net/rfc/array_first_last)
9+
- [`DelayedTargetValidation`](https://wiki.php.net/rfc/delayedtargetvalidation_attribute)
910

1011
More information can be found in the
1112
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
if (\PHP_VERSION_ID < 80500) {
13+
// @author Daniel Scherzer <[email protected]>
14+
#[Attribute(Attribute::TARGET_ALL)]
15+
final class DelayedTargetValidation
16+
{
17+
}
18+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Polyfill\Tests\Php85;
13+
14+
use Attribute;
15+
use DelayedTargetValidation;
16+
use PHPUnit\Framework\TestCase;
17+
use ReflectionClass;
18+
use ReflectionClassConstant;
19+
use ReflectionFunction;
20+
use ReflectionMethod;
21+
use ReflectionParameter;
22+
use ReflectionProperty;
23+
24+
#[DelayedTargetValidation]
25+
class HasAttribute {
26+
#[DelayedTargetValidation]
27+
private $prop;
28+
29+
#[DelayedTargetValidation]
30+
public const FOO = 'BAR';
31+
32+
#[DelayedTargetValidation]
33+
public function __construct(
34+
#[DelayedTargetValidation] $param
35+
) {}
36+
}
37+
38+
#[DelayedTargetValidation]
39+
function globalFunc() {}
40+
41+
/**
42+
* @author Daniel Scherzer <[email protected]>
43+
* @requires PHP >= 8.0
44+
*/
45+
class DelayedTargetValidationTest extends TestCase
46+
{
47+
public function testAttributeAttribute()
48+
{
49+
$ref = new ReflectionClass(\DelayedTargetValidation::class);
50+
$attributes = $ref->getAttributes();
51+
$this->assertCount(1, $attributes);
52+
$attribute = $attributes[0];
53+
$this->assertSame('Attribute', $attribute->getName());
54+
$args = $attribute->getArguments();
55+
$this->assertCount(1, $args);
56+
$this->assertSame(Attribute::TARGET_ALL, $args[0]);
57+
$this->expectException(\ReflectionException::class);
58+
$this->expectExceptionMessage('Constant "missing" does not exist');
59+
new \ReflectionConstant('missing');
60+
}
61+
62+
/**
63+
* @dataProvider provideReflectionInstances
64+
*/
65+
public function testTargetValidation($reflectionSource)
66+
{
67+
$attributes = $reflectionSource->getAttributes();
68+
$this->assertCount(1, $attributes);
69+
$attrib = $attributes[0];
70+
$this->assertSame('DelayedTargetValidation', $attrib->getName());
71+
$this->assertSame([], $attrib->getArguments());
72+
$this->assertInstanceOf(
73+
DelayedTargetValidation::class,
74+
$attrib->newInstance()
75+
);
76+
}
77+
78+
public static function provideReflectionInstances() {
79+
yield 'Class' => [ new ReflectionClass(HasAttribute::class) ];
80+
yield 'Property' => [ new ReflectionProperty(HasAttribute::class, 'prop')];
81+
yield 'Class constant' => [ new ReflectionClassConstant(HasAttribute::class, 'FOO')];
82+
yield 'Method' => [ new ReflectionMethod(HasAttribute::class, '__construct')];
83+
yield 'Parameter' => [
84+
new ReflectionParameter([HasAttribute::class, '__construct'], 'param')
85+
];
86+
yield 'Function' => [ new ReflectionFunction(__NAMESPACE__ . '\\globalFunc') ];
87+
}
88+
}

0 commit comments

Comments
 (0)