Skip to content

Commit

Permalink
feat: Enable the usage of "psr/log": "^2.0|^3.0"
Browse files Browse the repository at this point in the history
`phpdocumentor/reflection` started supporting `"psr/log": "^2.0|^3.0"`
with v5.3.

See phpDocumentor/Reflection#247
  • Loading branch information
jeromegamez committed Nov 29, 2022
1 parent 366917d commit 017537f
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Core/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"yoast/phpunit-polyfills": "^1.0",
"phpspec/prophecy": "^1.10.3",
"squizlabs/php_codesniffer": "2.*",
"phpdocumentor/reflection": "^3.0||^4.0",
"phpdocumentor/reflection": "^3.0||^4.0||^5.3",
"erusev/parsedown": "^1.6",
"google/gax": "^1.9",
"opis/closure": "^3",
Expand Down
17 changes: 12 additions & 5 deletions Core/src/Testing/Reflection/ReflectionHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@
namespace Google\Cloud\Core\Testing\Reflection;

use phpDocumentor\Reflection\File\LocalFile;
use phpDocumentor\Reflection\Php\EnumCase;

/**
* Class for determining if phpdocumentor/reflection v3 or v4 is being used.
* Class for determining if phpdocumentor/reflection v3, v4 or v5 is being used.
*/
class ReflectionHandlerFactory
{
/**
* @return ReflectionHandlerV3|ReflectionHandlerV4
* @return ReflectionHandlerV3|ReflectionHandlerV4|ReflectionHandlerV5
*/
public static function create()
{
return class_exists(LocalFile::class)
? new ReflectionHandlerV4()
: new ReflectionHandlerV3();
if (class_exists(EnumCase::class)) {
return new ReflectionHandlerV5();
}

if (class_exists(LocalFile::class)) {
return new ReflectionHandlerV4();
}

return new ReflectionHandlerV3();
}
}
170 changes: 170 additions & 0 deletions Core/src/Testing/Reflection/ReflectionHandlerV5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Core\Testing\Reflection;

use Google\Cloud\Core\Testing\Reflection\DescriptionFactory as CoreDescriptionFactory;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\DocBlock\StandardTagFactory;
use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\File\LocalFile;
use phpDocumentor\Reflection\FqsenResolver;
use phpDocumentor\Reflection\NodeVisitor\ElementNameResolver;
use phpDocumentor\Reflection\Php\Factory;
use phpDocumentor\Reflection\Php\Factory\Noop;
use phpDocumentor\Reflection\Php\Factory\TraitUse;
use phpDocumentor\Reflection\Php\NodesFactory;
use phpDocumentor\Reflection\Php\ProjectFactory;
use phpDocumentor\Reflection\Php\ProjectFactoryStrategies;
use phpDocumentor\Reflection\TypeResolver;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;

/**
* Class for running snippets using phpdocumentor/reflection:v5.
*/
class ReflectionHandlerV5
{
private $descriptionFactory;
private $docBlockFactory;

public function __construct()
{
$this->descriptionFactory = $this->createDescriptionFactory();
$this->docBlockFactory = $this->createDocBlockFactory($this->descriptionFactory);
}

/**
* @param string $class
* @return DocBlock
*/
public function createDocBlock($classOrMethod)
{
return $this->docBlockFactory->create($classOrMethod);
}

/**
* @param DocBlock $docBlock
* @return string
*/
public function getDocBlockText(DocBlock $docBlock)
{
$description = $this->descriptionFactory->create(
$docBlock->getSummary() . "\n\n" . $docBlock->getDescription(),
$docBlock->getContext()
);
return $description->render();
}

/**
* @param array $files
* @return string[]
*/
public function classes(array $files)
{
$projectFactory = $this->createProjectFactory();
$localFiles = [];
foreach ($files as $file) {
$localFiles[] = new LocalFile($file);
}
$project = $projectFactory->create('My Project', $localFiles);
$classes = [];
foreach ($files as $file) {
$classesInFile = $project->getFiles()[$file]->getClasses();
foreach ($classesInFile as $class) {
$classes[] = (string) $class->getFqsen();
}
}
return $classes;
}

/**
* @return ProjectFactory
*/
public function createProjectFactory()
{
$parser = (new ParserFactory())->create(
ParserFactory::ONLY_PHP7,
new Lexer\Emulative(['phpVersion' => Lexer\Emulative::PHP_8_0])
);
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new NameResolver());
$nodeTraverser->addVisitor(new ElementNameResolver());
$nodesFactory = new NodesFactory($parser, $nodeTraverser);

$docblockFactory = $this->createDocBlockFactory($this->createDescriptionFactory());

$methodStrategy = new Factory\Method($docblockFactory);

$strategies = new ProjectFactoryStrategies(
[
new Factory\Namespace_(),
new Factory\Argument(new PrettyPrinter()),
new Factory\Class_($docblockFactory),
new Factory\Enum_($docblockFactory),
new Factory\EnumCase($docblockFactory, new PrettyPrinter()),
new Factory\Define($docblockFactory, new PrettyPrinter()),
new Factory\GlobalConstant($docblockFactory, new PrettyPrinter()),
new Factory\ClassConstant($docblockFactory, new PrettyPrinter()),
new Factory\File($docblockFactory, $nodesFactory),
new Factory\Function_($docblockFactory),
new Factory\Interface_($docblockFactory),
$methodStrategy,
new Factory\Property($docblockFactory, new PrettyPrinter()),
new Factory\Trait_($docblockFactory),
new Factory\IfStatement(),
new TraitUse(),
]
);

$strategies->addStrategy(
new Factory\ConstructorPromotion($methodStrategy, $docblockFactory, new PrettyPrinter()),
1100
);
$strategies->addStrategy(new Noop(), -PHP_INT_MAX);

return new ProjectFactory($strategies);
}

/**
* @return DescriptionFactory
*/
public function createDescriptionFactory()
{
$fqsenResolver = new FqsenResolver();
$tagFactory = new StandardTagFactory($fqsenResolver);
$descriptionFactory = new CoreDescriptionFactory($tagFactory);

$tagFactory->addService($descriptionFactory, DescriptionFactory::class);
$tagFactory->addService(new TypeResolver($fqsenResolver));

return $descriptionFactory;
}

/**
* @return DocBlockFactory
*/
private function createDocBlockFactory($descriptionFactory)
{
$tagFactory = $descriptionFactory->getTagFactory();
return new DocBlockFactory($descriptionFactory, $tagFactory);
}
}
5 changes: 1 addition & 4 deletions Logging/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"yoast/phpunit-polyfills": "^1.0",
"phpspec/prophecy": "^1.10.3",
"squizlabs/php_codesniffer": "2.*",
"phpdocumentor/reflection": "^3.0||^4.0",
"phpdocumentor/reflection": "^3.0||^4.0||^5.3",
"erusev/parsedown": "^1.6",
"google/cloud-storage": "^1.3",
"google/cloud-bigquery": "^1.0",
Expand Down Expand Up @@ -48,8 +48,5 @@
"psr-4": {
"Google\\Cloud\\Logging\\Tests\\": "tests"
}
},
"conflict": {
"psr/log": ">=3"
}
}

0 comments on commit 017537f

Please sign in to comment.