Skip to content

Commit

Permalink
Merge pull request #125 from samsonasik/bump-php80
Browse files Browse the repository at this point in the history
[Step 2 Upgrade] Bump requirement to php 8.0
  • Loading branch information
lisachenko committed Jan 12, 2024
2 parents 1966e14 + 7c6cc69 commit d080526
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 124 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Expand Up @@ -16,7 +16,7 @@ jobs:
- "lowest"
- "highest"
php-version:
- "7.4"
- "8.0"
operating-system:
- "ubuntu-latest"

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Expand Up @@ -22,15 +22,16 @@
}
},
"require": {
"php": ">=7.4",
"php": ">=8.0",
"nikic/php-parser": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
"phpunit/phpunit": "^9.0",
"tracy/tracy": "^2.10"
},
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
"dev-master": "4.x-dev"
}
}
}
28 changes: 11 additions & 17 deletions phpunit.xml.dist
@@ -1,19 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
bootstrap="./vendor/autoload.php"
>
<testsuites>
<testsuite name="Parser Reflection Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" colors="true" bootstrap="./vendor/autoload.php">
<coverage>
<include>
<directory>./src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Parser Reflection Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 1 addition & 1 deletion src/ReflectionClassConstant.php
Expand Up @@ -200,7 +200,7 @@ public function __toString()
];
$value = $this->getValue();
$type = gettype($value);
if (PHP_VERSION_ID >= 70300 && isset($typeMap[$type])) {
if (isset($typeMap[$type])) {
$type = $typeMap[$type];
}
$valueType = new ReflectionType($type, null, true);
Expand Down
4 changes: 2 additions & 2 deletions src/ReflectionFunction.php
Expand Up @@ -77,11 +77,11 @@ public function getClosure()
/**
* {@inheritDoc}
*/
public function invoke($args = null)
public function invoke(mixed ...$args)
{
$this->initializeInternalReflection();

return parent::invoke(...func_get_args());
return parent::invoke(...$args);
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/ReflectionMethod.php
Expand Up @@ -112,11 +112,10 @@ public function __toString()
}

return sprintf(
"%sMethod [ <user%s%s%s>%s%s%s %s method %s ] {\n @@ %s %d - %d{$paramFormat}{$returnFormat}\n}\n",
"%sMethod [ <user%s%s>%s%s%s %s method %s ] {\n @@ %s %d - %d{$paramFormat}{$returnFormat}\n}\n",
$this->getDocComment() ? $this->getDocComment() . "\n" : '',
$prototype ? ", overwrites {$prototypeClass}, prototype {$prototypeClass}" : '',
$this->isConstructor() ? ', ctor' : '',
$this->isDestructor() ? ', dtor' : '',
$this->isFinal() ? ' final' : '',
$this->isStatic() ? ' static' : '',
$this->isAbstract() ? ' abstract' : '',
Expand Down Expand Up @@ -203,17 +202,17 @@ public function getPrototype()
/**
* {@inheritDoc}
*/
public function invoke($object, $args = null)
public function invoke(?object $object, mixed ...$args): mixed
{
$this->initializeInternalReflection();

return parent::invoke(...func_get_args());
return parent::invoke($object, ...$args);
}

/**
* {@inheritDoc}
*/
public function invokeArgs($object, array $args)
public function invokeArgs(?object $object, array $args): mixed
{
$this->initializeInternalReflection();

Expand Down
30 changes: 27 additions & 3 deletions src/ReflectionProperty.php
Expand Up @@ -113,11 +113,35 @@ public function __debugInfo(): array
*/
public function __toString()
{
$defaultValueDisplay = '';

if ($this->isDefault()) {
$this->__initialize();
$defaultValue = $this->getDefaultValue();
if (is_string($defaultValue)) {
if (strlen($defaultValue) > 18) {
$defaultValue = substr($defaultValue, 0, 15) . '...';
}

$defaultValue = "'" . $defaultValue . "'";
}

if ($defaultValue === null) {
$defaultValue = "NULL";
}

if (is_array($defaultValue)) {
$defaultValueDisplay = '= Array';
} else {
$defaultValueDisplay = '= ' . $defaultValue;
}
}

return sprintf(
"Property [%s %s $%s ]\n",
$this->isStatic() ? '' : ($this->isDefault() ? ' <default>' : ' <dynamic>'),
"Property [ %s $%s %s ]\n",
implode(' ', Reflection::getModifierNames($this->getModifiers())),
$this->getName()
$this->getName(),
$defaultValueDisplay
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/ReflectionType.php
Expand Up @@ -14,6 +14,7 @@

use ReflectionNamedType;
use ReflectionType as BaseReflectionType;
use ReflectionUnionType;

/**
* ReflectionType implementation
Expand Down Expand Up @@ -92,8 +93,8 @@ public static function convertToDisplayType(BaseReflectionType $type)

$displayType = ltrim($displayType, '\\');

if ($type->allowsNull()) {
$displayType .= ' or NULL';
if ($type->allowsNull() && ! $type instanceof ReflectionUnionType) {
$displayType = '?' . $displayType;
}

return $displayType;
Expand Down
12 changes: 4 additions & 8 deletions src/Traits/ReflectionClassLikeTrait.php
Expand Up @@ -221,7 +221,7 @@ public function getConstant($name)
/**
* {@inheritDoc}
*/
public function getConstants()
public function getConstants(?int $filter = null): array
{
if (!isset($this->constants)) {
$this->constants = $this->recursiveCollect(
Expand Down Expand Up @@ -417,10 +417,6 @@ public function getModifiers()
$modifiers += \ReflectionClass::IS_FINAL;
}

if (PHP_VERSION_ID < 70000 && $this->isTrait()) {
$modifiers += \ReflectionClass::IS_EXPLICIT_ABSTRACT;
}

if ($this->classLikeNode instanceof Class_ && $this->classLikeNode->isAbstract()) {
$modifiers += \ReflectionClass::IS_EXPLICIT_ABSTRACT;
}
Expand Down Expand Up @@ -553,7 +549,7 @@ public function getReflectionConstant($name)
/**
* @inheritDoc
*/
public function getReflectionConstants()
public function getReflectionConstants(?int $filter = null): array
{
if (!isset($this->classConstants)) {
$directClassConstants = ReflectionClassConstant::collectFromClassNode(
Expand Down Expand Up @@ -965,12 +961,12 @@ public function newInstanceArgs(array $args = [])
*
* @return object
*/
public function newInstanceWithoutConstructor($args = null)
public function newInstanceWithoutConstructor()
{
$function = __FUNCTION__;
$this->initializeInternalReflection();

return parent::$function($args);
return parent::$function();
}

/**
Expand Down
13 changes: 3 additions & 10 deletions tests/AbstractTestCase.php
Expand Up @@ -71,16 +71,9 @@ public function testCoverAllMethods()
public function getFilesToAnalyze()
{
$files = ['PHP5.5' => [__DIR__ . '/Stub/FileWithClasses55.php']];

if (PHP_VERSION_ID >= 50600) {
$files['PHP5.6'] = [__DIR__ . '/Stub/FileWithClasses56.php'];
}
if (PHP_VERSION_ID >= 70000) {
$files['PHP7.0'] = [__DIR__ . '/Stub/FileWithClasses70.php'];
}
if (PHP_VERSION_ID >= 70100) {
$files['PHP7.1'] = [__DIR__ . '/Stub/FileWithClasses71.php'];
}
$files['PHP5.6'] = [__DIR__ . '/Stub/FileWithClasses56.php'];
$files['PHP7.0'] = [__DIR__ . '/Stub/FileWithClasses70.php'];
$files['PHP7.1'] = [__DIR__ . '/Stub/FileWithClasses71.php'];

return $files;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ReflectionClassTest.php
Expand Up @@ -30,7 +30,7 @@ class ReflectionClassTest extends AbstractTestCase
*/
public function testGetModifiers($fileName)
{
if (PHP_VERSION_ID >= 70400) {
if (PHP_VERSION_ID >= 80000) {
$this->markTestSkipped('TODO: Fix mapping and logic of modifiers');
}
$mask =
Expand Down
17 changes: 3 additions & 14 deletions tests/ReflectionFunctionTest.php
Expand Up @@ -34,9 +34,7 @@ public function testGeneralInfoGetters()
'returnsReference', 'getClosureScopeClass', 'getClosureThis'
];

if (PHP_VERSION_ID >= 70000) {
$allNameGetters[] = 'hasReturnType';
}
$allNameGetters[] = 'hasReturnType';

foreach ($this->parsedRefFile->getFileNamespaces() as $fileNamespace) {
foreach ($fileNamespace->getFunctions() as $refFunction) {
Expand Down Expand Up @@ -72,7 +70,7 @@ public function testCoverAllMethods()
}

if ($allMissedMethods) {
$this->markTestIncomplete('Methods ' . join($allMissedMethods, ', ') . ' are not implemented');
$this->markTestIncomplete('Methods ' . implode(', ', $allMissedMethods) . ' are not implemented');
}
}

Expand Down Expand Up @@ -105,10 +103,6 @@ public function testInvokeArgsMethod()

public function testGetReturnTypeMethod()
{
if (PHP_VERSION_ID < 70000) {
$this->markTestSkipped('Test available only for PHP7.0 and newer');
}

$fileName = stream_resolve_include_path(__DIR__ . self::STUB_FILE70);

$reflectionFile = new ReflectionFile($fileName);
Expand All @@ -129,12 +123,7 @@ public function testGetReturnTypeMethod()
$originalReturnType = $originalRefFunction->getReturnType();
$this->assertSame($originalReturnType->allowsNull(), $parsedReturnType->allowsNull());
$this->assertSame($originalReturnType->isBuiltin(), $parsedReturnType->isBuiltin());
// TODO: To prevent deprecation error in tests
if (PHP_VERSION_ID < 70400) {
$this->assertSame($originalReturnType->__toString(), $parsedReturnType->__toString());
} else {
$this->assertSame($originalReturnType->getName(), $parsedReturnType->__toString());
}
$this->assertSame($originalReturnType->getName(), $parsedReturnType->__toString());
} else {
$this->assertSame(
$originalRefFunction->getReturnType(),
Expand Down
11 changes: 3 additions & 8 deletions tests/ReflectionMethodTest.php
Expand Up @@ -146,14 +146,9 @@ protected function getGettersToCheck()
'getNumberOfRequiredParameters', 'returnsReference', 'getClosureScopeClass', 'getClosureThis'
];

if (PHP_VERSION_ID >= 50600) {
$allNameGetters[] = 'isVariadic';
$allNameGetters[] = 'isGenerator';
}

if (PHP_VERSION_ID >= 70000) {
$allNameGetters[] = 'hasReturnType';
}
$allNameGetters[] = 'isVariadic';
$allNameGetters[] = 'isGenerator';
$allNameGetters[] = 'hasReturnType';

return $allNameGetters;
}
Expand Down
29 changes: 6 additions & 23 deletions tests/ReflectionParameterTest.php
Expand Up @@ -34,12 +34,8 @@ public function testGeneralInfoGetters($fileName)
$onlyWithDefaultValues = array_flip([
'getDefaultValue', 'getDefaultValueConstantName', 'isDefaultValueConstant'
]);
if (PHP_VERSION_ID >= 50600) {
$allNameGetters[] = 'isVariadic';
}
if (PHP_VERSION_ID >= 70000) {
$allNameGetters[] = 'hasType';
}
$allNameGetters[] = 'isVariadic';
$allNameGetters[] = 'hasType';

foreach ($this->parsedRefFile->getFileNamespaces() as $fileNamespace) {
foreach ($fileNamespace->getFunctions() as $refFunction) {
Expand Down Expand Up @@ -75,13 +71,8 @@ public function testGeneralInfoGetters($fileName)
public function fileProvider()
{
$files = ['PHP5.5' => [__DIR__ . '/Stub/FileWithParameters55.php']];

if (PHP_VERSION_ID >= 50600) {
$files['PHP5.6'] = [__DIR__ . '/Stub/FileWithParameters56.php'];
}
if (PHP_VERSION_ID >= 70000) {
$files['PHP7.0'] = [__DIR__ . '/Stub/FileWithParameters70.php'];
}
$files['PHP5.6'] = [__DIR__ . '/Stub/FileWithParameters56.php'];
$files['PHP7.0'] = [__DIR__ . '/Stub/FileWithParameters70.php'];

return $files;
}
Expand Down Expand Up @@ -253,15 +244,12 @@ public function testCoverAllMethods()
}

if ($allMissedMethods) {
$this->markTestIncomplete('Methods ' . join($allMissedMethods, ', ') . ' are not implemented');
$this->markTestIncomplete('Methods ' . implode(', ', $allMissedMethods) . ' are not implemented');
}
}

public function testGetTypeMethod()
{
if (PHP_VERSION_ID < 70000) {
$this->markTestSkipped('Test available only for PHP7.0 and newer');
}
$this->setUpFile(__DIR__ . '/Stub/FileWithParameters70.php');

foreach ($this->parsedRefFile->getFileNamespaces() as $fileNamespace) {
Expand All @@ -282,12 +270,7 @@ public function testGetTypeMethod()
$originalReturnType = $originalRefParameter->getType();
$this->assertSame($originalReturnType->allowsNull(), $parsedReturnType->allowsNull(), $message);
$this->assertSame($originalReturnType->isBuiltin(), $parsedReturnType->isBuiltin(), $message);
// TODO: To prevent deprecation error in tests
if (PHP_VERSION_ID < 70400) {
$this->assertSame($originalReturnType->__toString(), $parsedReturnType->__toString(), $message);
} else {
$this->assertSame($originalReturnType->getName(), $parsedReturnType->__toString(), $message);
}
$this->assertSame($originalReturnType->getName(), $parsedReturnType->__toString(), $message);
} else {
$this->assertSame(
$originalRefParameter->getType(),
Expand Down

0 comments on commit d080526

Please sign in to comment.