-
Notifications
You must be signed in to change notification settings - Fork 0
Support arbitrary expressions in property initializations and parameter defaults #104
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
Merged
Conversation
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
Additional changes to diff --git a/src/main/php/lang/reflect/Parameter.class.php b/src/main/php/lang/reflect/Parameter.class.php
index decf71dc6..789e51192 100755
--- a/src/main/php/lang/reflect/Parameter.class.php
+++ b/src/main/php/lang/reflect/Parameter.class.php
@@ -1,6 +1,6 @@
<?php namespace lang\reflect;
-use lang\{ElementNotFoundException, ClassLoadingException, ClassNotFoundException, XPClass, Type, TypeUnion};
+use lang\{ElementNotFoundException, IllegalStateException, ClassLoadingException, ClassNotFoundException, XPClass, Type, TypeUnion};
use util\Objects;
/**
@@ -160,17 +160,24 @@ class Parameter {
}
/**
- * Get default value.
+ * Get default value. Additionally checks `default` annotation for NULL defaults.
*
* @throws lang.IllegalStateException in case this argument is not optional
* @return var
*/
public function getDefaultValue() {
if ($this->_reflect->isOptional()) {
- return $this->_reflect->isDefaultValueAvailable() ? $this->_reflect->getDefaultValue() : null;
+ if (!$this->_reflect->isDefaultValueAvailable()) return null;
+
+ $value= $this->_reflect->getDefaultValue();
+ if (null === $value) {
+ $details= XPClass::detailsForMethod($this->_reflect->getDeclaringClass(), $this->_details[1]);
+ return $details[DETAIL_TARGET_ANNO]['$'.$this->_reflect->getName()]['default'] ?? null;
+ }
+ return $value;
}
- throw new \lang\IllegalStateException('Parameter "'.$this->_reflect->getName().'" has no default value');
+ throw new IllegalStateException('Parameter "'.$this->_reflect->getName().'" has no default value');
}
/**
|
thekid
added a commit
to thekid/core
that referenced
this pull request
Mar 6, 2021
|
Real-world example Beforeuse net\daringfireball\markdown\Markdown;
use web\frontend\helpers\Extension;
class RenderMarkdown extends Extension {
private $engine;
/** Creates new markdown renderer */
public function __construct() {
$this->engine= new Markdown();
}
// ...
} Afteruse net\daringfireball\markdown\Markdown;
use web\frontend\helpers\Extension;
class RenderMarkdown extends Extension {
private $engine= new Markdown();
// ...
} |
3 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Initialization is now supported for properties, static variables, parameter defaults and in combination with property promotion. On top of https://wiki.php.net/rfc/new_in_initializers, this pull request also supports arbitrary expressions in any of these places (as hinted in the RFC PHP might do in the future).
The parser already supported expressions in these place, but emitting the code generated compile errors of the kind Constant expression contains invalid operations.
const
, as there is no way to emit valid PHP 7.X / PHP 8.0 code nor any behind-the-scenes trickery (not even with runkit) to achieve this!Code added to
main
is less than 100 lines:/cc @mikey179