10000 Support arbitrary expressions in property initializations and parameter defaults by thekid · Pull Request #104 · xp-framework/compiler · GitHub
[go: up one dir, main page]

Skip to content

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
merged 15 commits into from
Mar 6, 2021

Conversation

thekid
Copy link
Member
@thekid thekid commented Mar 6, 2021

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.

class Parser {
  private static $DEFAULT_SYNTAX= new Syntax('php');

  public function __construct(private $options= new Options()) { }

  public function parse($tokens= new Tokens(), $syntax= self::$DEFAULT_SYNTAX) {
    // TBI
  }
}

⚠️ This pull request does not support non-constant expressions for 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:

$ git diff master src/main/php/ | grep '^-' | wc -l
16
$ git diff master src/main/php/ | grep '^+' | wc -l
98

/cc @mikey179

@thekid
Copy link
Member Author
thekid commented Mar 6, 2021

Additional changes to xp-framework/reflection and xp-framework/core reflection need to be made to support reflective access to parameter defaults:

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
Copy link
Member Author
thekid commented Mar 6, 2021

Additional changes to xp-framework/reflection and xp-framework/core reflection need to be made

@thekid thekid merged commit 6d7039c into 8000 master Mar 6, 2021
@thekid thekid deleted the feature/new_in_initializers branch March 6, 2021 17:22
@thekid
Copy link
Member Author
thekid commented Mar 6, 2021

@thekid
Copy link
Member Author
thekid commented Mar 6, 2021

Real-world example

Before

use 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();
  }

  // ...
}

After

use net\daringfireball\markdown\Markdown;
use web\frontend\helpers\Extension;

class RenderMarkdown extends Extension {
  private $engine= new Markdown();

  // ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0