|
| 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 | +} |
0 commit comments