Skip to content

Commit 3e4b14b

Browse files
committed
feat: Enable the usage of "psr/log": "^2.0|^3.0"
`phpdocumentor/reflection` started supporting `"psr/log": "^2.0|^3.0"` with v5.3. See phpDocumentor/Reflection#247
1 parent 353a0b4 commit 3e4b14b

File tree

4 files changed

+184
-10
lines changed

4 files changed

+184
-10
lines changed

Core/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"yoast/phpunit-polyfills": "^1.0",
1919
"phpspec/prophecy": "^1.10.3",
2020
"squizlabs/php_codesniffer": "2.*",
21-
"phpdocumentor/reflection": "^3.0||^4.0",
21+
"phpdocumentor/reflection": "^3.0||^4.0||^5.3",
2222
"erusev/parsedown": "^1.6",
2323
"google/gax": "^1.9",
2424
"opis/closure": "^3",

Core/src/Testing/Reflection/ReflectionHandlerFactory.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,26 @@
1818
namespace Google\Cloud\Core\Testing\Reflection;
1919

2020
use phpDocumentor\Reflection\File\LocalFile;
21+
use phpDocumentor\Reflection\Php\EnumCase;
2122

2223
/**
23-
* Class for determining if phpdocumentor/reflection v3 or v4 is being used.
24+
* Class for determining if phpdocumentor/reflection v3, v4 or v5 is being used.
2425
*/
2526
class ReflectionHandlerFactory
2627
{
2728
/**
28-
* @return ReflectionHandlerV3|ReflectionHandlerV4
29+
* @return ReflectionHandlerV3|ReflectionHandlerV4|ReflectionHandlerV5
2930
*/
3031
public static function create()
3132
{
32-
return class_exists(LocalFile::class)
33-
? new ReflectionHandlerV4()
34-
: new ReflectionHandlerV3();
33+
if (class_exists(EnumCase::class)) {
34+
return new ReflectionHandlerV5();
35+
}
36+
37+
if (class_exists(LocalFile::class)) {
38+
return new ReflectionHandlerV4();
39+
}
40+
41+
return new ReflectionHandlerV3();
3542
}
3643
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Core\Testing\Reflection;
19+
20+
use Google\Cloud\Core\Testing\Reflection\DescriptionFactory as CoreDescriptionFactory;
21+
use phpDocumentor\Reflection\DocBlock;
22+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
23+
use phpDocumentor\Reflection\DocBlock\StandardTagFactory;
24+
use phpDocumentor\Reflection\DocBlockFactory;
25+
use phpDocumentor\Reflection\File\LocalFile;
26+
use phpDocumentor\Reflection\FqsenResolver;
27+
use phpDocumentor\Reflection\NodeVisitor\ElementNameResolver;
28+
use phpDocumentor\Reflection\Php\Factory;
29+
use phpDocumentor\Reflection\Php\Factory\Noop;
30+
use phpDocumentor\Reflection\Php\Factory\TraitUse;
31+
use phpDocumentor\Reflection\Php\NodesFactory;
32+
use phpDocumentor\Reflection\Php\ProjectFactory;
33+
use phpDocumentor\Reflection\Php\ProjectFactoryStrategies;
34+
use phpDocumentor\Reflection\TypeResolver;
35+
use PhpParser\Lexer;
36+
use PhpParser\NodeTraverser;
37+
use PhpParser\NodeVisitor\NameResolver;
38+
use PhpParser\ParserFactory;
39+
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
40+
41+
/**
42+
* Class for running snippets using phpdocumentor/reflection:v5.
43+
*/
44+
class ReflectionHandlerV5
45+
{
46+
private $descriptionFactory;
47+
private $docBlockFactory;
48+
49+
public function __construct()
50+
{
51+
$this->descriptionFactory = $this->createDescriptionFactory();
52+
$this->docBlockFactory = $this->createDocBlockFactory($this->descriptionFactory);
53+
}
54+
55+
/**
56+
* @param string $class
57+
* @return DocBlock
58+
*/
59+
public function createDocBlock($classOrMethod)
60+
{
61+
return $this->docBlockFactory->create($classOrMethod);
62+
}
63+
64+
/**
65+
* @param DocBlock $docBlock
66+
* @return string
67+
*/
68+
public function getDocBlockText(DocBlock $docBlock)
69+
{
70+
$description = $this->descriptionFactory->create(
71+
$docBlock->getSummary() . "\n\n" . $docBlock->getDescription(),
72+
$docBlock->getContext()
73+
);
74+
return $description->render();
75+
}
76+
77+
/**
78+
* @param array $files
79+
* @return string[]
80+
*/
81+
public function classes(array $files)
82+
{
83+
$projectFactory = $this->createProjectFactory();
84+
$localFiles = [];
85+
foreach ($files as $file) {
86+
$localFiles[] = new LocalFile($file);
87+
}
88+
$project = $projectFactory->create('My Project', $localFiles);
89+
$classes = [];
90+
foreach ($files as $file) {
91+
$classesInFile = $project->getFiles()[$file]->getClasses();
92+
foreach ($classesInFile as $class) {
93+
$classes[] = (string) $class->getFqsen();
94+
}
95+
}
96+
return $classes;
97+
}
98+
99+
/**
100+
* @return ProjectFactory
101+
*/
102+
public function createProjectFactory()
103+
{
104+
$parser = (new ParserFactory())->create(
105+
ParserFactory::ONLY_PHP7,
106+
new Lexer\Emulative(['phpVersion' => Lexer\Emulative::PHP_8_0])
107+
);
108+
$nodeTraverser = new NodeTraverser();
109+
$nodeTraverser->addVisitor(new NameResolver());
110+
$nodeTraverser->addVisitor(new ElementNameResolver());
111+
$nodesFactory = new NodesFactory($parser, $nodeTraverser);
112+
113+
$docblockFactory = $this->createDocBlockFactory($this->createDescriptionFactory());
114+
115+
$methodStrategy = new Factory\Method($docblockFactory);
116+
117+
$strategies = new ProjectFactoryStrategies(
118+
[
119+
new Factory\Namespace_(),
120+
new Factory\Argument(new PrettyPrinter()),
121+
new Factory\Class_($docblockFactory),
122+
new Factory\Enum_($docblockFactory),
123+
new Factory\EnumCase($docblockFactory, new PrettyPrinter()),
124+
new Factory\Define($docblockFactory, new PrettyPrinter()),
125+
new Factory\GlobalConstant($docblockFactory, new PrettyPrinter()),
126+
new Factory\ClassConstant($docblockFactory, new PrettyPrinter()),
127+
new Factory\File($docblockFactory, $nodesFactory),
128+
new Factory\Function_($docblockFactory),
129+
new Factory\Interface_($docblockFactory),
130+
$methodStrategy,
131+
new Factory\Property($docblockFactory, new PrettyPrinter()),
132+
new Factory\Trait_($docblockFactory),
133+
new Factory\IfStatement(),
134+
new TraitUse(),
135+
]
136+
);
137+
138+
$strategies->addStrategy(
139+
new Factory\ConstructorPromotion($methodStrategy, $docblockFactory, new PrettyPrinter()),
140+
1100
141+
);
142+
$strategies->addStrategy(new Noop(), -PHP_INT_MAX);
143+
144+
return new ProjectFactory($strategies);
145+
}
146+
147+
/**
148+
* @return DescriptionFactory
149+
*/
150+
public function createDescriptionFactory()
151+
{
152+
$fqsenResolver = new FqsenResolver();
153+
$tagFactory = new StandardTagFactory($fqsenResolver);
154+
$descriptionFactory = new CoreDescriptionFactory($tagFactory);
155+
156+
$tagFactory->addService($descriptionFactory, DescriptionFactory::class);
157+
$tagFactory->addService(new TypeResolver($fqsenResolver));
158+
159+
return $descriptionFactory;
160+
}
161+
162+
/**
163+
* @return DocBlockFactory
164+
*/
165+
private function createDocBlockFactory($descriptionFactory)
166+
{
167+
$tagFactory = $descriptionFactory->getTagFactory();
168+
return new DocBlockFactory($descriptionFactory, $tagFactory);
169+
}
170+
}

Logging/composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"yoast/phpunit-polyfills": "^1.0",
1313
"phpspec/prophecy": "^1.10.3",
1414
"squizlabs/php_codesniffer": "2.*",
15-
"phpdocumentor/reflection": "^3.0||^4.0",
15+
"phpdocumentor/reflection": "^3.0||^4.0||^5.3",
1616
"erusev/parsedown": "^1.6",
1717
"google/cloud-storage": "^1.3",
1818
"google/cloud-bigquery": "^1.0",
@@ -48,8 +48,5 @@
4848
"psr-4": {
4949
"Google\\Cloud\\Logging\\Tests\\": "tests"
5050
}
51-
},
52-
"conflict": {
53-
"psr/log": ">=3"
5451
}
5552
}

0 commit comments

Comments
 (0)