-
Notifications
You must be signed in to change notification settings - Fork 89
Initial PHP 8 Support #67
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
Changes from all commits
251fce0
b890211
a1312e9
a95a6a8
fd66ae0
1864f84
710bbfb
878c73a
920c191
7f26f1d
24e80e7
13590e2
c484451
6047099
36f3d22
0f69f14
f453f32
378c39e
c9eac8a
47ad58b
5f856e1
b116437
aba532d
7a0d6ee
f9e510d
baa435f
3a8eb4a
a0676d9
bb133ae
94745f7
7a7ff99
2274a2c
522c9b6
ed079ca
3b0ebd7
f61aa2a
ab833c4
caaf68c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,10 @@ | |
|
||
namespace Opis\Closure; | ||
|
||
defined('T_NAME_QUALIFIED') || define('T_NAME_QUALIFIED', -4); | ||
defined('T_NAME_FULLY_QUALIFIED') || define('T_NAME_FULLY_QUALIFIED', -5); | ||
defined('T_FN') || define('T_FN', -6); | ||
|
||
use Closure; | ||
use ReflectionFunction; | ||
|
||
|
@@ -84,29 +88,12 @@ public function getCode() | |
} | ||
|
||
$className = null; | ||
$fn = false; | ||
|
||
|
||
if (null !== $className = $this->getClosureScopeClass()) { | ||
$className = '\\' . trim($className->getName(), '\\'); | ||
} | ||
|
||
|
||
if($php7 = PHP_MAJOR_VERSION === 7){ | ||
switch (PHP_MINOR_VERSION){ | ||
case 0: | ||
$php7_types = array('string', 'int', 'bool', 'float'); | ||
break; | ||
case 1: | ||
$php7_types = array('string', 'int', 'bool', 'float', 'void'); | ||
break; | ||
case 2: | ||
default: | ||
$php7_types = array('string', 'int', 'bool', 'float', 'void', 'object'); | ||
} | ||
$fn = PHP_MINOR_VERSION === 4; | ||
} | ||
|
||
$builtin_types = self::getBuiltinTypes(); | ||
$class_keywords = ['self', 'static', 'parent']; | ||
|
||
$ns = $this->getNamespaceName(); | ||
|
@@ -143,7 +130,7 @@ public function getCode() | |
if ($token[0] === T_FUNCTION || $token[0] === T_STATIC) { | ||
$code .= $token[1]; | ||
$state = $token[0] === T_FUNCTION ? 'function' : 'static'; | ||
} elseif ($fn && $token[0] === T_FN) { | ||
} elseif ($token[0] === T_FN) { | ||
$isShortClosure = true; | ||
$code .= $token[1]; | ||
$state = 'closure_args'; | ||
|
@@ -155,7 +142,7 @@ public function getCode() | |
if ($token[0] === T_FUNCTION) { | ||
$state = 'function'; | ||
} | ||
} elseif ($fn && $token[0] === T_FN) { | ||
} elseif ($token[0] === T_FN) { | ||
$isShortClosure = true; | ||
$code .= $token[1]; | ||
$state = 'closure_args'; | ||
|
@@ -182,14 +169,20 @@ public function getCode() | |
if($token[0] === T_FUNCTION || $token[0] === T_STATIC){ | ||
$code = $token[1]; | ||
$state = $token[0] === T_FUNCTION ? 'function' : 'static'; | ||
} elseif ($fn && $token[0] === T_FN) { | ||
} elseif ($token[0] === T_FN) { | ||
$isShortClosure = true; | ||
$code .= $token[1]; | ||
$state = 'closure_args'; | ||
} | ||
break; | ||
case 'closure_args': | ||
switch ($token[0]){ | ||
case T_NAME_QUALIFIED: | ||
list($id_start, $id_start_ci, $id_name) = $this->parseNameQualified($token[1]); | ||
$context = 'args'; | ||
$state = 'id_name'; | ||
$lastState = 'closure_args'; | ||
break; | ||
case T_NS_SEPARATOR: | ||
case T_STRING: | ||
$id_start = $token[1]; | ||
|
@@ -258,6 +251,12 @@ public function getCode() | |
$state = 'id_name'; | ||
$lastState = 'return'; | ||
break 2; | ||
case T_NAME_QUALIFIED: | ||
list($id_start, $id_start_ci, $id_name) = $this->parseNameQualified($token[1]); | ||
$context = 'return_type'; | ||
$state = 'id_name'; | ||
$lastState = 'return'; | ||
break 2; | ||
case T_DOUBLE_ARROW: | ||
$code .= $token[1]; | ||
if ($isShortClosure) { | ||
|
@@ -364,6 +363,12 @@ public function getCode() | |
$state = 'id_name'; | ||
$lastState = 'closure'; | ||
break 2; | ||
case T_NAME_QUALIFIED: | ||
list($id_start, $id_start_ci, $id_name) = $this->parseNameQualified($token[1]); | ||
$context = 'root'; | ||
$state = 'id_name'; | ||
$lastState = 'closure'; | ||
break 2; | ||
case T_NEW: | ||
$code .= $token[1]; | ||
$context = 'new'; | ||
|
@@ -463,13 +468,18 @@ public function getCode() | |
$code .= $token[1]; | ||
break; | ||
case T_NS_SEPARATOR: | ||
case T_NAME_FULLY_QUALIFIED: | ||
case T_STRING: | ||
case T_STATIC: | ||
$id_start = $token[1]; | ||
$id_start_ci = strtolower($id_start); | ||
$id_name = ''; | ||
$state = 'id_name'; | ||
break 2; | ||
case T_NAME_QUALIFIED: | ||
list($id_start, $id_start_ci, $id_name) = $this->parseNameQualified($token[1]); | ||
$state = 'id_name'; | ||
break 2; | ||
case T_VARIABLE: | ||
$code .= $token[1]; | ||
$state = $lastState; | ||
|
@@ -485,10 +495,9 @@ public function getCode() | |
break; | ||
case 'id_name': | ||
switch ($token[0]){ | ||
case T_NAME_QUALIFIED: | ||
case T_NS_SEPARATOR: | ||
case T_STRING: | ||
$id_name .= $token[1]; | ||
break; | ||
case T_WHITESPACE: | ||
case T_COMMENT: | ||
case T_DOC_COMMENT: | ||
|
@@ -538,7 +547,7 @@ public function getCode() | |
if (!$inside_structure) { | ||
$isUsingScope = $token[0] === T_DOUBLE_COLON; | ||
} | ||
} elseif (!($php7 && in_array($id_start_ci, $php7_types))){ | ||
} elseif (!(\PHP_MAJOR_VERSION >= 7 && in_array($id_start_ci, $builtin_types))){ | ||
if ($classes === null) { | ||
$classes = $this->getClasses(); | ||
} | ||
|
@@ -588,7 +597,7 @@ public function getCode() | |
if (!$inside_structure && !$id_start_ci === 'static') { | ||
$isUsingScope = true; | ||
} | ||
} elseif (!($php7 && in_array($id_start_ci, $php7_types))){ | ||
} elseif (!(\PHP_MAJOR_VERSION >= 7 && in_array($id_start_ci, $builtin_types))){ | ||
if($classes === null){ | ||
$classes = $this->getClasses(); | ||
} | ||
|
@@ -646,6 +655,32 @@ public function getCode() | |
return $this->code; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
private static function getBuiltinTypes() | ||
{ | ||
// PHP 5 | ||
if (\PHP_MAJOR_VERSION === 5) { | ||
return ['array', 'callable']; | ||
} | ||
|
||
// PHP 8 | ||
if (\PHP_MAJOR_VERSION === 8) { | ||
return ['array', 'callable', 'string', 'int', 'bool', 'float', 'iterable', 'void', 'object', 'mixed', 'false', 'null']; | ||
} | ||
|
||
// PHP 7 | ||
switch (\PHP_MINOR_VERSION) { | ||
case 0: | ||
return ['array', 'callable', 'string', 'int', 'bool', 'float']; | ||
case 1: | ||
return ['array', 'callable', 'string', 'int', 'bool', 'float', 'iterable', 'void']; | ||
default: | ||
return ['array', 'callable', 'string', 'int', 'bool', 'float', 'iterable', 'void', 'object']; | ||
} | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
|
@@ -901,6 +936,11 @@ protected function fetchItems() | |
$name .= $token[1]; | ||
$alias = $token[1]; | ||
break; | ||
case T_NAME_QUALIFIED: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are more tokens than this to consider. ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one fixes one specific test that I wrote that was passing on 7.4. It's 23:00 here so I'm heading to bed for today and will continue tomorrow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still unresolved. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you be more specific? |
||
$name .= $token[1]; | ||
$pieces = explode('\\', $token[1]); | ||
$alias = end($pieces); | ||
break; | ||
case T_AS: | ||
$lastState = 'use'; | ||
$state = 'alias'; | ||
|
@@ -935,6 +975,11 @@ protected function fetchItems() | |
case T_NS_SEPARATOR: | ||
$name .= $token[1]; | ||
break; | ||
case T_NAME_QUALIFIED: | ||
$name .= $token[1]; | ||
$pieces = explode('\\', $token[1]); | ||
$alias = end($pieces); | ||
break; | ||
case T_STRING: | ||
$name .= $token[1]; | ||
$alias = $token[1]; | ||
|
@@ -1035,4 +1080,17 @@ protected function fetchItems() | |
static::$constants[$key] = $constants; | ||
static::$structures[$key] = $structures; | ||
} | ||
|
||
private function parseNameQualified($token) | ||
{ | ||
$pieces = explode('\\', $token); | ||
|
||
$id_start = array_shift($pieces); | ||
|
||
$id_start_ci = strtolower($id_start); | ||
|
||
$id_name = '\\' . implode('\\', $pieces); | ||
|
||
return [$id_start, $id_start_ci, $id_name]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Opis\Closure\Test; | ||
|
||
use Closure; | ||
use Opis\Closure\ReflectionClosure; | ||
|
||
final class NamespaceGroupTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test_namespace_fully_qualified() | ||
sorinsarca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$f = function () { new \A; }; | ||
$e = 'function () { new \A; }'; | ||
$this->assertEquals($e, $this->c($f)); | ||
} | ||
|
||
protected function c(Closure $closure) | ||
{ | ||
$r = new ReflectionClosure($closure); | ||
|
||
return $r->getCode(); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.