-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Add #[\DelayedTargetValidation]
attribute
#18817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DanielEScherzer
wants to merge
13
commits into
php:master
Choose a base branch
from
DanielEScherzer:delayed-target-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,761
−66
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
570cfed
Add `#[\DelayedTargetValidation]` attribute
DanielEScherzer 626932c
Avoid uninitialized memory
DanielEScherzer 60b8778
Run validators for delayed validators
DanielEScherzer d47c354
Fix for reflection
DanielEScherzer 23824e3
Test application of all internal attributes, fix 2 bugs
DanielEScherzer 34e4d06
Missing comment
DanielEScherzer 55b4656
Tests for property hooks
DanielEScherzer 120eb26
Delay validation of #[\NoDiscard] on property hooks
DanielEScherzer 7759d47
Update tests; tweak comments
DanielEScherzer f7e14d5
#[\Attribute]
DanielEScherzer b4c70c5
UPGRADING
DanielEScherzer 5017426
without
DanielEScherzer 6b11fd2
Update for #[\Override] on properties
DanielEScherzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
282 changes: 282 additions & 0 deletions
282
Zend/tests/attributes/delayed_target_validation/has_runtime_errors.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,282 @@ | ||
--TEST-- | ||
#[\DelayedTargetValidation] has errors at runtime | ||
--FILE-- | ||
<?php | ||
|
||
#[DelayedTargetValidation] | ||
#[NoDiscard] | ||
class Demo { | ||
|
||
#[DelayedTargetValidation] | ||
#[Attribute] | ||
public const FOO = 'BAR'; | ||
|
||
#[DelayedTargetValidation] | ||
#[Attribute] | ||
public string $v1; | ||
|
||
public string $v2 { | ||
#[DelayedTargetValidation] | ||
#[Attribute] | ||
get => $this->v2; | ||
#[DelayedTargetValidation] | ||
#[Attribute] | ||
set => $value; | ||
} | ||
|
||
#[DelayedTargetValidation] | ||
#[Attribute] | ||
public function __construct( | ||
#[DelayedTargetValidation] | ||
#[Attribute] | ||
public string $v3 | ||
) { | ||
$this->v1 = $v3; | ||
echo __METHOD__ . "\n"; | ||
} | ||
} | ||
|
||
#[DelayedTargetValidation] | ||
#[Attribute] | ||
function demoFn() { | ||
echo __FUNCTION__ . "\n"; | ||
} | ||
|
||
#[DelayedTargetValidation] | ||
#[Attribute] | ||
const EXAMPLE = true; | ||
|
||
$cases = [ | ||
new ReflectionClass('Demo'), | ||
new ReflectionClassConstant('Demo', 'FOO'), | ||
new ReflectionProperty('Demo', 'v1'), | ||
new ReflectionProperty('Demo', 'v2')->getHook(PropertyHookType::Get), | ||
new ReflectionProperty('Demo', 'v2')->getHook(PropertyHookType::Set), | ||
new ReflectionMethod('Demo', '__construct'), | ||
new ReflectionParameter([ 'Demo', '__construct' ], 'v3'), | ||
new ReflectionProperty('Demo', 'v3'), | ||
new ReflectionFunction('demoFn'), | ||
new ReflectionConstant('EXAMPLE'), | ||
]; | ||
foreach ($cases as $r) { | ||
echo str_repeat("*", 20) . "\n"; | ||
echo $r . "\n"; | ||
$attributes = $r->getAttributes(); | ||
var_dump($attributes); | ||
try { | ||
$attributes[1]->newInstance(); | ||
} catch (Error $e) { | ||
echo get_class($e) . ": " . $e->getMessage() . "\n"; | ||
} | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
******************** | ||
Class [ <user> <iterateable> class Demo ] { | ||
@@ %s %d-%d | ||
|
||
- Constants [1] { | ||
Constant [ public string FOO ] { BAR } | ||
} | ||
|
||
- Static properties [0] { | ||
} | ||
|
||
- Static methods [0] { | ||
} | ||
|
||
- Properties [3] { | ||
Property [ public string $v1 ] | ||
Property [ public string $v2 { get; set; } ] | ||
Property [ public string $v3 ] | ||
} | ||
|
||
- Methods [1] { | ||
Method [ <user, ctor> public method __construct ] { | ||
@@ %s %d - %d | ||
|
||
- Parameters [1] { | ||
Parameter #0 [ <required> string $v3 ] | ||
} | ||
} | ||
} | ||
} | ||
|
||
array(2) { | ||
[0]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(23) "DelayedTargetValidation" | ||
} | ||
[1]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(9) "NoDiscard" | ||
} | ||
} | ||
Error: Attribute "NoDiscard" cannot target class (allowed targets: function, method) | ||
******************** | ||
Constant [ public string FOO ] { BAR } | ||
|
||
array(2) { | ||
[0]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(23) "DelayedTargetValidation" | ||
} | ||
[1]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(9) "Attribute" | ||
} | ||
} | ||
Error: Attribute "Attribute" cannot target class constant (allowed targets: class) | ||
******************** | ||
Property [ public string $v1 ] | ||
|
||
array(2) { | ||
[0]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(23) "DelayedTargetValidation" | ||
} | ||
[1]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(9) "Attribute" | ||
} | ||
} | ||
Error: Attribute "Attribute" cannot target property (allowed targets: class) | ||
******************** | ||
Method [ <user> public method $v2::get ] { | ||
@@ %s %d - %d | ||
|
||
- Parameters [0] { | ||
} | ||
- Return [ string ] | ||
} | ||
|
||
array(2) { | ||
[0]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(23) "DelayedTargetValidation" | ||
} | ||
[1]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(9) "Attribute" | ||
} | ||
} | ||
Error: Attribute "Attribute" cannot target method (allowed targets: class) | ||
******************** | ||
Method [ <user> public method $v2::set ] { | ||
@@ %s %d - %d | ||
|
||
- Parameters [1] { | ||
Parameter #0 [ <required> string $value ] | ||
} | ||
- Return [ void ] | ||
} | ||
|
||
array(2) { | ||
[0]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(23) "DelayedTargetValidation" | ||
} | ||
[1]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(9) "Attribute" | ||
} | ||
} | ||
Error: Attribute "Attribute" cannot target method (allowed targets: class) | ||
******************** | ||
Method [ <user, ctor> public method __construct ] { | ||
@@ %s %d - %d | ||
|
||
- Parameters [1] { | ||
Parameter #0 [ <required> string $v3 ] | ||
} | ||
} | ||
|
||
array(2) { | ||
[0]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(23) "DelayedTargetValidation" | ||
} | ||
[1]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(9) "Attribute" | ||
} | ||
} | ||
Error: Attribute "Attribute" cannot target method (allowed targets: class) | ||
******************** | ||
Parameter #0 [ <required> string $v3 ] | ||
array(2) { | ||
[0]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(23) "DelayedTargetValidation" | ||
} | ||
[1]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(9) "Attribute" | ||
} | ||
} | ||
Error: Attribute "Attribute" cannot target parameter (allowed targets: class) | ||
******************** | ||
Property [ public string $v3 ] | ||
|
||
array(2) { | ||
[0]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(23) "DelayedTargetValidation" | ||
} | ||
[1]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(9) "Attribute" | ||
} | ||
} | ||
Error: Attribute "Attribute" cannot target property (allowed targets: class) | ||
******************** | ||
Function [ <user> function demoFn ] { | ||
@@ %s %d - %d | ||
} | ||
|
||
array(2) { | ||
[0]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(23) "DelayedTargetValidation" | ||
} | ||
[1]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(9) "Attribute" | ||
} | ||
} | ||
Error: Attribute "Attribute" cannot target function (allowed targets: class) | ||
******************** | ||
Constant [ bool EXAMPLE ] { 1 } | ||
|
||
array(2) { | ||
[0]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(23) "DelayedTargetValidation" | ||
} | ||
[1]=> | ||
object(ReflectionAttribute)#%d (1) { | ||
["name"]=> | ||
string(9) "Attribute" | ||
} | ||
} | ||
Error: Attribute "Attribute" cannot target constant (allowed targets: class) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we ever added attributes here. Makes sense, but I would just mention the class name + RFC and not the full explanation. Can you just commit the related change for NoDiscard to master?