Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
feat: php 8.2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-thebaud committed Dec 13, 2022
1 parent 0d7c410 commit 7a6bb93
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 69 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
php: [ 8.0, 8.1 ]
php: [ 8.0, 8.1, 8.2 ]
os: [ ubuntu-latest, windows-latest ]

name: PHP${{ matrix.php }} - ${{ matrix.os }}
Expand All @@ -40,11 +40,11 @@ jobs:

- name: Prepare SonarCloud Scan
run: sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' .coverage/clover.xml
if: ${{ matrix.php == '8.1' && matrix.os == 'ubuntu-latest' }}
if: ${{ matrix.php == '8.2' && matrix.os == 'ubuntu-latest' }}

- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@v1.6
if: ${{ matrix.php == '8.1' && matrix.os == 'ubuntu-latest' }}
if: ${{ matrix.php == '8.2' && matrix.os == 'ubuntu-latest' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 3.1.0

- Add compatibility with `php@8.2` and `roave/better-reflection@^6.0`.

## 3.0.0

**Added**
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
}
],
"require": {
"php": "~8.0.12 || ~8.1.0",
"php": "~8.0.12 || ~8.1.0 || ~8.2.0",
"league/container": "^3.2",
"phpdocumentor/reflection-docblock": "^5.2",
"roave/better-reflection": "^5.0",
"phpdocumentor/type-resolver": "^1.6",
"roave/better-reflection": "^5.0 || ^6.0",
"tightenco/collect": "^8.0 || ^9.0"
},
"require-dev": {
Expand Down
60 changes: 10 additions & 50 deletions src/Helpers/Reflect.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\DocBlockFactoryInterface;
use phpDocumentor\Reflection\Types\Context;
use PhpParser\Node;
use phpDocumentor\Reflection\Types\ContextFactory;
use PhpUnitGen\Core\Reflection\ReflectionType;
use Roave\BetterReflection\Reflection\ReflectionClass;
use Roave\BetterReflection\Reflection\ReflectionMethod;
Expand Down Expand Up @@ -166,7 +166,7 @@ public static function returnType(ReflectionMethod $reflectionMethod): ?Reflecti
*/
public static function docBlock(ReflectionMethod|ReflectionClass $reflectionObject): ?DocBlock
{
$docComment = $reflectionObject->getDocComment();
$docComment = $reflectionObject->getDocComment() ?? '';

return $docComment !== ''
? self::getDocBlockFactory()->create($docComment, self::docBlockContext($reflectionObject))
Expand Down Expand Up @@ -208,55 +208,15 @@ private static function convertDocBlockTagToTypes(?DocBlock\Tags\TagWithType $ta
*/
private static function docBlockContext(ReflectionMethod|ReflectionClass $reflectionObject): Context
{
$namespaceAst = $reflectionObject instanceof ReflectionMethod
? $reflectionObject->getDeclaringClass()->getDeclaringNamespaceAst()
: $reflectionObject->getDeclaringNamespaceAst();
$reflectionClass = $reflectionObject instanceof ReflectionMethod
? $reflectionObject->getDeclaringClass()
: $reflectionObject;

return new Context(
$namespaceAst->name ? $namespaceAst->name->toString() : '',
self::getAliasesFullyQualifiedNamesMap($namespaceAst)->all(),
);
}

/**
* Get aliases mapped with fully qualified names.
*
* @param Node\Stmt\Namespace_ $namespace
*
* @return Collection
*/
private static function getAliasesFullyQualifiedNamesMap(Node\Stmt\Namespace_ $namespace): Collection
{
return self::getClassAlikeUses($namespace)
->map(static function (Node\Stmt\Use_|Node\Stmt\GroupUse $use): Collection {
return (new Collection($use->uses))
->mapWithKeys(static function (Node\Stmt\UseUse $useUse) use ($use): array {
/** @psalm-var class-string $useUseClassName */
$useUseClassName = $use instanceof Node\Stmt\GroupUse
? $use->prefix->toString().'\\'.$useUse->name->toString()
: $useUse->name->toString();

return [$useUse->getAlias()->toString() => $useUseClassName];
});
})
->mapWithKeys(static function (Collection $uses) {
return $uses->all();
});
}
$contextFactory = new ContextFactory();

/**
* Get class uses statements.
*
* @param Node\Stmt\Namespace_ $namespace
*
* @return Collection
*/
private static function getClassAlikeUses(Node\Stmt\Namespace_ $namespace): Collection
{
return (new Collection($namespace->stmts))
->filter(static function (Node $node) {
return ($node instanceof Node\Stmt\Use_ || $node instanceof Node\Stmt\GroupUse)
&& in_array($node->type, [Node\Stmt\Use_::TYPE_UNKNOWN, Node\Stmt\Use_::TYPE_NORMAL], true);
});
return $contextFactory->createForNamespace(
$reflectionClass->getNamespaceName() ?? '',
$reflectionClass->getLocatedSource()->getSource(),
);
}
}
15 changes: 9 additions & 6 deletions tests/Unit/Generators/Factories/DocumentationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

use Mockery;
use Mockery\Mock;
use PhpParser\Node\Stmt\Namespace_;
use PhpUnitGen\Core\Contracts\Config\Config;
use PhpUnitGen\Core\Contracts\Generators\Factories\TypeFactory;
use PhpUnitGen\Core\Generators\Factories\DocumentationFactory;
use PhpUnitGen\Core\Models\TestClass;
use PhpUnitGen\Core\Models\TestMethod;
use PhpUnitGen\Core\Models\TestProperty;
use Roave\BetterReflection\Reflection\ReflectionClass;
use Roave\BetterReflection\SourceLocator\Located\LocatedSource;
use Tests\PhpUnitGen\Core\TestCase;
use Tightenco\Collect\Support\Collection;

Expand Down Expand Up @@ -71,9 +71,6 @@ public function testMakeForClassWithoutCustomTags(): void
$reflectionClass->shouldReceive('getDocComment')
->withNoArgs()
->andReturn('');
$reflectionClass->shouldReceive('getDeclaringNamespaceAst')
->withNoArgs()
->andReturn(new Namespace_());

$class->shouldReceive('getShortName')
->withNoArgs()
Expand All @@ -95,6 +92,7 @@ public function testMakeForClassWithCustomTags(): void
{
$class = Mockery::mock(TestClass::class);
$reflectionClass = Mockery::mock(ReflectionClass::class);
$reflectionSource = Mockery::mock(LocatedSource::class);

$this->config->shouldReceive('phpDoc')
->withNoArgs()
Expand All @@ -103,12 +101,17 @@ public function testMakeForClassWithCustomTags(): void
->withNoArgs()
->andReturn(['author', 'since', 'copyright']);

$reflectionSource->shouldReceive(['getSource' => '']);

$reflectionClass->shouldReceive('getName')
->withNoArgs()
->andReturn('App\\Foo');
$reflectionClass->shouldReceive('getDeclaringNamespaceAst')
$reflectionClass->shouldReceive('getNamespaceName')
->withNoArgs()
->andReturn('');
$reflectionClass->shouldReceive('getLocatedSource')
->withNoArgs()
->andReturn(new Namespace_());
->andReturn($reflectionSource);
$reflectionClass->shouldReceive('getDocComment')
->withNoArgs()
->andReturn("/**\n * @author John\n * @since 1.0.0\n * @internal\n*/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,6 @@ public function testAddProperties(): void
$class->shouldReceive('addProperty')
->with($userProperty);

$this->callProtectedMethod($testGenerator, 'addProperties', $class);
$this->assertNull($this->callProtectedMethod($testGenerator, 'addProperties', $class));
}
}
38 changes: 31 additions & 7 deletions tests/Unit/Helpers/ReflectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlockFactoryInterface;
use phpDocumentor\Reflection\Types\Context;
use PhpParser\Node\Stmt\Namespace_;
use PhpUnitGen\Core\Helpers\Reflect;
use Roave\BetterReflection\Reflection\ReflectionClass;
use Roave\BetterReflection\Reflection\ReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionParameter;
use Roave\BetterReflection\Reflection\ReflectionProperty;
use Roave\BetterReflection\SourceLocator\Located\LocatedSource;
use Tests\PhpUnitGen\Core\Helpers\PhpVersionDependents;
use Tests\PhpUnitGen\Core\TestCase;

Expand Down Expand Up @@ -128,9 +128,13 @@ public function testParameterTypeWithDocBlockType(): void
$reflectionClass = Mockery::mock(ReflectionClass::class);
$reflectionMethod = Mockery::mock(ReflectionMethod::class);
$reflectionParameter = Mockery::mock(ReflectionParameter::class);
$reflectionSource = Mockery::mock(LocatedSource::class);

$reflectionSource->shouldReceive(['getSource' => '']);

$reflectionClass->shouldReceive([
'getDeclaringNamespaceAst' => new Namespace_(),
'getNamespaceName' => '',
'getLocatedSource' => $reflectionSource,
]);
$reflectionMethod->shouldReceive([
'getDocComment' => "/*\n * @param string|null \$foo\n */",
Expand All @@ -154,9 +158,13 @@ public function testParameterTypeWithNone(): void
$reflectionClass = Mockery::mock(ReflectionClass::class);
$reflectionMethod = Mockery::mock(ReflectionMethod::class);
$reflectionParameter = Mockery::mock(ReflectionParameter::class);
$reflectionSource = Mockery::mock(LocatedSource::class);

$reflectionSource->shouldReceive(['getSource' => '']);

$reflectionClass->shouldReceive([
'getDeclaringNamespaceAst' => new Namespace_(),
'getNamespaceName' => '',
'getLocatedSource' => $reflectionSource,
]);
$reflectionMethod->shouldReceive([
'getDocComment' => '',
Expand Down Expand Up @@ -197,9 +205,13 @@ public function testReturnTypeWithDocBlockType(): void
{
$reflectionClass = Mockery::mock(ReflectionClass::class);
$reflectionMethod = Mockery::mock(ReflectionMethod::class);
$reflectionSource = Mockery::mock(LocatedSource::class);

$reflectionSource->shouldReceive(['getSource' => '']);

$reflectionClass->shouldReceive([
'getDeclaringNamespaceAst' => new Namespace_(),
'getNamespaceName' => '',
'getLocatedSource' => $reflectionSource,
]);
$reflectionMethod->shouldReceive([
'getReturnType' => null,
Expand Down Expand Up @@ -242,9 +254,13 @@ public function testDocBlockWhenDefaultFactoryAndNotEmptyDocComment(): void
{
$reflectionClass = Mockery::mock(ReflectionClass::class);
$reflectionMethod = Mockery::mock(ReflectionMethod::class);
$reflectionSource = Mockery::mock(LocatedSource::class);

$reflectionSource->shouldReceive(['getSource' => '']);

$reflectionClass->shouldReceive([
'getDeclaringNamespaceAst' => new Namespace_(),
'getNamespaceName' => '',
'getLocatedSource' => $reflectionSource,
]);
$reflectionMethod->shouldReceive('getDocComment')
->withNoArgs()
Expand All @@ -260,9 +276,13 @@ public function testDocBlockWhenCustomFactory(): void
{
$reflectionClass = Mockery::mock(ReflectionClass::class);
$reflectionMethod = Mockery::mock(ReflectionMethod::class);
$reflectionSource = Mockery::mock(LocatedSource::class);

$reflectionSource->shouldReceive(['getSource' => '']);

$reflectionClass->shouldReceive([
'getDeclaringNamespaceAst' => new Namespace_(),
'getNamespaceName' => '',
'getLocatedSource' => $reflectionSource,
]);
$reflectionMethod->shouldReceive('getDocComment')
->withNoArgs()
Expand Down Expand Up @@ -303,9 +323,13 @@ public function testDocBlockTagsWhenNotEmptyDocComment(): void
{
$reflectionClass = Mockery::mock(ReflectionClass::class);
$reflectionMethod = Mockery::mock(ReflectionMethod::class);
$reflectionSource = Mockery::mock(LocatedSource::class);

$reflectionSource->shouldReceive(['getSource' => '']);

$reflectionClass->shouldReceive([
'getDeclaringNamespaceAst' => new Namespace_(),
'getNamespaceName' => '',
'getLocatedSource' => $reflectionSource,
]);
$reflectionMethod->shouldReceive('getDocComment')
->withNoArgs()
Expand Down

0 comments on commit 7a6bb93

Please sign in to comment.