Skip to content
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

Start PHP 8.0 support #47

Merged
merged 8 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,24 @@ env:
matrix:
fast_finish: true
include:
- php: 7.1
- php: 7.3
env:
- DEPS=lowest
- php: 7.1
- php: 7.3
env:
- DEPS=latest
- CS_CHECK=true
- TEST_COVERAGE=true
- php: 7.2
env:
- DEPS=lowest
- php: 7.2
env:
- DEPS=latest
- php: 7.3
- php: 7.4
env:
- DEPS=lowest
- php: 7.3
- php: 7.4
env:
- DEPS=latest
- php: 7.4
- php: nightly
env:
- DEPS=lowest
- php: 7.4
- php: nightly
env:
- DEPS=latest

Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- Nothing.
- [#47](https://github.com/laminas/laminas-code/pull/47) adds support for PHP 8. NOTE: this simply means the code runs on PHP 8, not that it can generate code specific to PHP 8.

### Changed

Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
"extra": {
},
"require": {
"php": "^7.1",
"laminas/laminas-eventmanager": "^2.6 || ^3.0",
"laminas/laminas-zendframework-bridge": "^1.0"
"php": "^7.3 || ~8.0.0",
"laminas/laminas-eventmanager": "^3.3",
"laminas/laminas-zendframework-bridge": "^1.1"
},
"require-dev": {
"ext-phar": "*",
"doctrine/annotations": "^1.7",
"laminas/laminas-coding-standard": "^1.0",
"laminas/laminas-stdlib": "^2.7 || ^3.0",
"phpunit/phpunit": "^7.5.16 || ^8.4"
"doctrine/annotations": "^1.10.4",
"laminas/laminas-coding-standard": "^1.0.0",
"laminas/laminas-stdlib": "^3.3.0",
"phpunit/phpunit": "^9.4.2"
},
"conflict": {
"phpspec/prophecy": "<1.9.0"
Expand Down
30 changes: 14 additions & 16 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="laminas-code Test Suite">
<directory>./test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>

<php>
<!-- Enable this if you have installed Doctrine\Common on the
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="laminas-code Test Suite">
<directory>./test</directory>
</testsuite>
</testsuites>
<php>
<!-- Enable this if you have installed Doctrine\Common on the
include_path or via Composer. -->
<env name="TESTS_LAMINAS_CODE_ANNOTATION_DOCTRINE_SUPPORT" value="false" />
</php>
<env name="TESTS_LAMINAS_CODE_ANNOTATION_DOCTRINE_SUPPORT" value="false"/>
</php>
</phpunit>
20 changes: 7 additions & 13 deletions src/Reflection/ParameterReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public function getDeclaringClass()
*/
public function getClass()
{
$phpReflection = parent::getClass();
if ($phpReflection === null) {
$phpReflectionType = parent::getType();
if ($phpReflectionType === null) {
return null;
}

$laminasReflection = new ClassReflection($phpReflection->getName());
unset($phpReflection);
$laminasReflection = new ClassReflection($phpReflectionType->getName());
unset($phpReflectionType);

return $laminasReflection;
}
Expand Down Expand Up @@ -77,20 +77,14 @@ public function getDeclaringFunction()
public function detectType()
{
if (method_exists($this, 'getType')
&& ($type = $this->getType())
&& null !== ($type = $this->getType())
&& $type->isBuiltin()
) {
return $type->getName();
}

// can be dropped when dropping PHP7 support:
if ($this->isArray()) {
return 'array';
}

// can be dropped when dropping PHP7 support:
if ($this->isCallable()) {
return 'callable';
if (null !== $type && $type->getName() === 'self') {
return $this->getDeclaringClass()->getName();
}

if (($class = $this->getClass()) instanceof \ReflectionClass) {
Expand Down
8 changes: 7 additions & 1 deletion src/Scanner/ClassScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,8 @@ protected function scan()

case T_NS_SEPARATOR:
case T_STRING:
case T_NAME_FULLY_QUALIFIED:
case T_NAME_QUALIFIED:
switch ($classContext) {
case T_EXTENDS:
if ($this->isInterface) {
Expand Down Expand Up @@ -1160,7 +1162,11 @@ protected function scan()
if ($tokenType !== null) {
// use context
if (false === $useAliasContext) {
if ($tokenType == T_NS_SEPARATOR || $tokenType == T_STRING) {
if ($tokenType == T_NS_SEPARATOR
|| $tokenType == T_STRING
|| $tokenType == T_NAME_QUALIFIED
|| $tokenType == T_NAME_FULLY_QUALIFIED
) {
$infos[$infoIndex]['use_statements'][$useStatementIndex] .= $tokenContent;
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/Scanner/MethodScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,8 @@ protected function scan()

case T_VARIABLE:
case T_STRING:
case T_NAME_QUALIFIED:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where should I put the backword compatibility definitions for the new constants?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anywhere you want, as long as it's @internal, I suppose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Analyzing the source, \Laminas\Code\Scanner\TokenArrayScanner::scan seems the right place: https://github.com/laminas/laminas-code/pull/47/files#diff-c08ce1832ae9f8788d45e1cf8fa62d743ede600c44a4e622ec20b4b684383320R383
Had a run with --process-isolation, and everything works fine.

case T_NAME_FULLY_QUALIFIED:
if ($tokenType === T_STRING && $parentCount === 0) {
$this->name = $tokenContent;
}
Expand Down
9 changes: 8 additions & 1 deletion src/Scanner/ParameterScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,14 @@ protected function scan()
goto SCANNER_CONTINUE;
}
} else {
if ($this->name === null && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR)) {
if ($this->name === null
&& (
$token[0] === T_STRING
|| $token[0] === T_NS_SEPARATOR
|| $token[0] === T_NAME_QUALIFIED
|| $token[0] === T_NAME_FULLY_QUALIFIED
)
) {
$this->class .= $token[1];
goto SCANNER_CONTINUE;
}
Expand Down
20 changes: 18 additions & 2 deletions src/Scanner/TokenArrayScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,14 @@ protected function scan()
return $infoIndex;
};

// ensure php backwards compatibility
if (! defined('T_NAME_QUALIFIED')) {
define('T_NAME_QUALIFIED', 24001);
}
if (! defined('T_NAME_FULLY_QUALIFIED')) {
define('T_NAME_FULLY_QUALIFIED', 24002);
}

/**
* START FINITE STATE MACHINE FOR SCANNING TOKENS
*/
Expand Down Expand Up @@ -426,7 +434,11 @@ protected function scan()
goto SCANNER_NAMESPACE_CONTINUE;
}

if ($tokenType === T_NS_SEPARATOR || $tokenType === T_STRING) {
if ($tokenType === T_NS_SEPARATOR
|| $tokenType === T_STRING
|| $tokenType === T_NAME_QUALIFIED
|| $tokenType === T_NAME_FULLY_QUALIFIED
) {
$infos[$infoIndex]['namespace'] .= $tokenContent;
}

Expand Down Expand Up @@ -491,7 +503,11 @@ protected function scan()
goto SCANNER_USE_CONTINUE;
}

if ($tokenType == T_NS_SEPARATOR || $tokenType == T_STRING) {
if ($tokenType == T_NS_SEPARATOR
|| $tokenType == T_STRING
|| $tokenType == T_NAME_QUALIFIED
|| $tokenType == T_NAME_FULLY_QUALIFIED
) {
if ($useAsContext == false) {
$infos[$infoIndex]['statements'][$useStatementIndex]['use'] .= $tokenContent;
} else {
Expand Down
4 changes: 2 additions & 2 deletions test/Generator/ParameterGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public function testCreateFromArray()
$parameterGenerator = ParameterGenerator::fromArray([
'name' => 'SampleParameter',
'type' => 'int',
'defaultvalue' => 'foo',
'defaultvalue' => 'default-foo',
'passedbyreference' => false,
'position' => 1,
'sourcedirty' => false,
Expand All @@ -227,7 +227,7 @@ public function testCreateFromArray()
self::assertFalse($parameterGenerator->isSourceDirty());
self::assertEquals('foo', $parameterGenerator->getSourceContent());
self::assertEquals('-', $parameterGenerator->getIndentation());
self::assertAttributeEquals(true, 'omitDefaultValue', $parameterGenerator);
self::assertStringNotContainsString('default-foo', $parameterGenerator->generate());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions test/Generator/PropertyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ public function testCreateFromArray() : void
{
$propertyGenerator = PropertyGenerator::fromArray([
'name' => 'SampleProperty',
'const' => true,
'defaultvalue' => 'foo',
'const' => false,
'defaultvalue' => 'default-foo',
'docblock' => [
'shortdescription' => 'foo',
],
Expand All @@ -253,14 +253,14 @@ public function testCreateFromArray() : void
]);

self::assertEquals('SampleProperty', $propertyGenerator->getName());
self::assertTrue($propertyGenerator->isConst());
self::assertFalse($propertyGenerator->isConst());
self::assertInstanceOf(ValueGenerator::class, $propertyGenerator->getDefaultValue());
self::assertInstanceOf(DocBlockGenerator::class, $propertyGenerator->getDocBlock());
self::assertTrue($propertyGenerator->isAbstract());
self::assertTrue($propertyGenerator->isFinal());
self::assertTrue($propertyGenerator->isStatic());
self::assertEquals(PropertyGenerator::VISIBILITY_PROTECTED, $propertyGenerator->getVisibility());
self::assertAttributeEquals(true, 'omitDefaultValue', $propertyGenerator);
self::assertStringNotContainsString('default-foo', $propertyGenerator->generate());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/Generator/TypeGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function validType()
];

return array_combine(
array_map('reset', $valid),
array_map('current', $valid),
$valid
);
}
Expand Down Expand Up @@ -253,7 +253,7 @@ public function invalidType()
];

return array_combine(
array_map('reset', $invalid),
array_map('current', $invalid),
$invalid
);
}
Expand Down