From b3f74737c7f9882f7da9cebc21d376bd5d19a33c Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 23 Mar 2024 18:06:57 +0100 Subject: [PATCH] Make compatible with XP 12 --- .github/workflows/ci.yml | 6 +- ChangeLog.md | 6 ++ README.md | 2 +- composer.json | 9 +-- .../php/lang/reflection/Constants.class.php | 24 ++------ src/main/php/lang/reflection/Method.class.php | 8 +-- .../php/lang/reflection/Property.class.php | 5 +- .../php/lang/reflection/Routine.class.php | 2 +- src/main/php/lang/reflection/Type.class.php | 14 +---- src/main/php/module.xp | 58 ------------------- 10 files changed, 23 insertions(+), 111 deletions(-) delete mode 100755 src/main/php/module.xp diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4986533..cf71cda 100755 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ jobs: strategy: fail-fast: false matrix: - php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] + php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] os: [ubuntu-latest, windows-latest] steps: @@ -24,7 +24,7 @@ jobs: run: git config --system core.autocrlf false; git config --system core.eol lf - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up PHP ${{ matrix.php-versions }} uses: shivammathur/setup-php@v2 @@ -40,7 +40,7 @@ jobs: run: echo "::set-output name=dir::$(composer config cache-files-dir)" - name: Cache dependencies - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} diff --git a/ChangeLog.md b/ChangeLog.md index d644bdd..d25e1a3 100755 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,12 @@ XP Reflection ChangeLog ## ?.?.? / ????-??-?? +## 3.0.0 / 2024-03-23 + +* Made this library compatible with XP 12, droppping support for all but + the latest PHP 7 version. Minimum PHP version required is now **7.4**! + (@thekid) + ## 2.14.1 / 2023-09-30 * Fixed `FromAttributes::imports()` for classes created inside `eval` diff --git a/README.md b/README.md index 9f848fe..27ea0ea 100755 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ XP Reflection [![Build status on GitHub](https://github.com/xp-framework/reflection/workflows/Tests/badge.svg)](https://github.com/xp-framework/reflection/actions) [![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core) [![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md) -[![Requires PHP 7.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_0plus.svg)](http://php.net/) +[![Requires PHP 7.4+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_4plus.svg)](http://php.net/) [![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/) [![Latest Stable Version](https://poser.pugx.org/xp-framework/reflection/version.png)](https://packagist.org/packages/xp-framework/reflection) diff --git a/composer.json b/composer.json index c064b7d..990d069 100755 --- a/composer.json +++ b/composer.json @@ -6,15 +6,12 @@ "description" : "Reflection", "keywords": ["module", "xp"], "require" : { - "xp-framework/core": "^11.0 | ^10.13", - "xp-framework/ast": "^10.0 | ^9.0 | ^8.0 | ^7.6", - "php" : ">=7.0.0" - }, - "suggest" : { + "xp-framework/core": "^12.0 | ^11.0 | ^10.13", + "xp-framework/ast": "^11.0 | ^10.0 | ^9.0 | ^8.0 | ^7.6", "php" : ">=7.4.0" }, "require-dev" : { - "xp-framework/test": "^1.0" + "xp-framework/test": "^2.0 | ^1.0" }, "bin": ["bin/xp.xp-framework.reflection.reflect"], "autoload" : { diff --git a/src/main/php/lang/reflection/Constants.class.php b/src/main/php/lang/reflection/Constants.class.php index 4af7b9e..173300d 100755 --- a/src/main/php/lang/reflection/Constants.class.php +++ b/src/main/php/lang/reflection/Constants.class.php @@ -1,6 +1,6 @@ = 70100) { - foreach ($this->reflect->getReflectionConstants() as $constant) { - if (0 !== strncmp($constant->name, '__', 2)) { - yield $constant->name => new Constant($constant); - } + foreach ($this->reflect->getReflectionConstants() as $constant) { + if (0 !== strncmp($constant->name, '__', 2)) { + yield $constant->name => new Constant($constant); } - } else { - - // PHP 7.0 does not have getReflectionConstants(), enumerate constants - // using name and values instead and use ReflectionClassConstant polyfill - // - // @codeCoverageIgnoreStart - foreach ($this->reflect->getConstants() as $name => $_) { - if (0 !== strncmp($name, '__', 2)) { - yield $name => new Constant(new \ReflectionClassConstant($this->reflect->name, $name)); - } - } - // @codeCoverageIgnoreEnd } } @@ -44,7 +30,7 @@ public function getIterator(): Traversable { */ public function named($name) { return $this->reflect->hasConstant($name) - ? new Constant(new \ReflectionClassConstant($this->reflect->name, $name)) + ? new Constant(new ReflectionClassConstant($this->reflect->name, $name)) : null ; } diff --git a/src/main/php/lang/reflection/Method.class.php b/src/main/php/lang/reflection/Method.class.php index 892f5d4..dc24182 100755 --- a/src/main/php/lang/reflection/Method.class.php +++ b/src/main/php/lang/reflection/Method.class.php @@ -62,12 +62,6 @@ public function invoke($instance, $args= [], $context= null) { try { $pass= PHP_VERSION_ID < 80000 && $args ? self::pass($this->reflect, $args) : $args; - - // PHP 7.0 still had warnings for arguments - if (PHP_VERSION_ID < 70100 && sizeof($pass) < $this->reflect->getNumberOfRequiredParameters()) { - throw new ReflectionException('Too few arguments'); - } - return $this->reflect->invokeArgs($instance, $pass); } catch (ReflectionException $e) { throw new CannotInvoke($this, $e); @@ -126,7 +120,7 @@ public function toString() { } $returns= substr($name, 1); } else { - $returns= strtr(PHP_VERSION_ID >= 70100 ? $t->getName() : $t->__toString(), '\\', '.'); + $returns= strtr($t->getName(), '\\', '.'); $t->allowsNull() && $nullable= '?'; } diff --git a/src/main/php/lang/reflection/Property.class.php b/src/main/php/lang/reflection/Property.class.php index 0f1ef86..2388310 100755 --- a/src/main/php/lang/reflection/Property.class.php +++ b/src/main/php/lang/reflection/Property.class.php @@ -35,7 +35,7 @@ public function constraint() { }; $t= Type::resolve( - PHP_VERSION_ID >= 70400 || '' === $this->reflect->name ? $this->reflect->getType() : null, + $this->reflect->getType(), Member::resolve($this->reflect), $api ); @@ -103,8 +103,7 @@ public function set($instance, $value, $context= null) { public function toString() { // Compile property type - $t= PHP_VERSION_ID >= 70400 || '' === $this->reflect->name ? $this->reflect->getType() : null; - if (null === $t) { + if (null === ($t= $this->reflect->getType())) { $name= Reflection::meta()->propertyType($this->reflect) ?? 'var'; } else if ($t instanceof ReflectionUnionType) { $name= ''; diff --git a/src/main/php/lang/reflection/Routine.class.php b/src/main/php/lang/reflection/Routine.class.php index dcf0cfa..491e351 100755 --- a/src/main/php/lang/reflection/Routine.class.php +++ b/src/main/php/lang/reflection/Routine.class.php @@ -40,7 +40,7 @@ protected function signature($meta) { } $type= substr($name, 1); } else { - $type= strtr(PHP_VERSION_ID >= 70100 ? $t->getName() : $t->__toString(), '\\', '.'); + $type= strtr($t->getName(), '\\', '.'); $parameter->isVariadic() && $type.= '...'; $t->allowsNull() && $nullable= '?'; } diff --git a/src/main/php/lang/reflection/Type.class.php b/src/main/php/lang/reflection/Type.class.php index 3162dc5..d22536e 100755 --- a/src/main/php/lang/reflection/Type.class.php +++ b/src/main/php/lang/reflection/Type.class.php @@ -28,19 +28,7 @@ public function class(): XPClass { return new XPClass($this->reflect); } /** Returns this type's modifiers */ public function modifiers(): Modifiers { - if (PHP_VERSION_ID < 70400) { - - // PHP 7.4 made type and member modifiers consistent. For versions before that, - // map PHP reflection modifiers to generic form. - // - // @codeCoverageIgnoreStart - $m= $this->reflect->getModifiers(); - $r= 0; - $m & ReflectionClass::IS_EXPLICIT_ABSTRACT && $r|= Modifiers::IS_ABSTRACT; - $m & ReflectionClass::IS_IMPLICIT_ABSTRACT && $r|= Modifiers::IS_ABSTRACT; - $m & ReflectionClass::IS_FINAL && $r|= Modifiers::IS_FINAL; - // @codeCoverageIgnoreEnd - } else if (PHP_VERSION_ID >= 80200) { + if (PHP_VERSION_ID >= 80200) { // PHP 8.2 introduced readonly classes, but its modifier bit is different from // the one that properties use (65536 vs. 128), map this to generic form. diff --git a/src/main/php/module.xp b/src/main/php/module.xp deleted file mode 100755 index 351b8cc..0000000 --- a/src/main/php/module.xp +++ /dev/null @@ -1,58 +0,0 @@ -class= $class; - $this->name= $name; - $this->reflect= new \ReflectionClass($class); - } - - public function getName() { return $this->name; } - - public function getModifiers() { return MODIFIER_PUBLIC; } - - public function getValue() { return $this->reflect->getConstant($this->name); } - - public function getDocComment() { - $tokens= token_get_all(file_get_contents($this->reflect->getFileName())); - $const= $comment= false; - for ($i= 0, $s= sizeof($tokens); $i < $s; $i++) { - if (T_DOC_COMMENT === $tokens[$i][0]) { - $comment= $tokens[$i][1]; - } else if (T_CONST === $tokens[$i][0]) { - $const= true; - } else if ($const && "=" === $tokens[$i][0]) { - if ($this->name === $tokens[$i - 2][1]) return $comment; - $const= false; - $comment= false; - } - } - return false; - } - - public function getDeclaringClass() { - $reflect= $this->reflect; - do { - $parent= $reflect->getParentClass(); - } while ($parent && $parent->hasConstant($name) && $reflect= $parent); - return $reflect; - } - }'); - } - } -} \ No newline at end of file