|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of phpDocumentor. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @link http://phpdoc.org |
| 12 | + */ |
| 13 | + |
| 14 | +namespace phpDocumentor\Reflection\Php\Factory; |
| 15 | + |
| 16 | +use phpDocumentor\Reflection\Fqsen; |
| 17 | +use phpDocumentor\Reflection\Location; |
| 18 | +use phpDocumentor\Reflection\Php\Constant as ConstantElement; |
| 19 | +use phpDocumentor\Reflection\Php\StrategyContainer; |
| 20 | +use phpDocumentor\Reflection\PrettyPrinter; |
| 21 | +use phpDocumentor\Reflection\Types\Context; |
| 22 | +use PhpParser\Node\Arg; |
| 23 | +use PhpParser\Node\Expr\FuncCall; |
| 24 | +use PhpParser\Node\Name; |
| 25 | +use PhpParser\Node\Scalar\String_; |
| 26 | +use PhpParser\Node\Stmt\Expression; |
| 27 | +use RuntimeException; |
| 28 | +use function sprintf; |
| 29 | + |
| 30 | +/** |
| 31 | + * Strategy to convert `define` expressions to ConstantElement |
| 32 | + * |
| 33 | + * @see ConstantElement |
| 34 | + * @see GlobalConstantIterator |
| 35 | + */ |
| 36 | +final class Define extends AbstractFactory |
| 37 | +{ |
| 38 | + /** @var PrettyPrinter */ |
| 39 | + private $valueConverter; |
| 40 | + |
| 41 | + /** |
| 42 | + * Initializes the object. |
| 43 | + */ |
| 44 | + public function __construct(PrettyPrinter $prettyPrinter) |
| 45 | + { |
| 46 | + $this->valueConverter = $prettyPrinter; |
| 47 | + } |
| 48 | + |
| 49 | + public function matches($object) : bool |
| 50 | + { |
| 51 | + if (!$object instanceof Expression) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + $expression = $object->expr; |
| 56 | + if (!$expression instanceof FuncCall) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + if (!$expression->name instanceof Name) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + if ((string) $expression->name !== 'define') { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Creates an Constant out of the given object. |
| 73 | + * |
| 74 | + * Since an object might contain other objects that need to be converted the $factory is passed so it can be |
| 75 | + * used to create nested Elements. |
| 76 | + * |
| 77 | + * @param Expression $object object to convert to an Element |
| 78 | + * @param StrategyContainer $strategies used to convert nested objects. |
| 79 | + * @param Context $context of the created object |
| 80 | + * |
| 81 | + * @return ConstantElement |
| 82 | + */ |
| 83 | + protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null) |
| 84 | + { |
| 85 | + $expression = $object->expr; |
| 86 | + if (!$expression instanceof FuncCall) { |
| 87 | + throw new RuntimeException( |
| 88 | + 'Provided expression is not a function call; this should not happen because the `create` method' |
| 89 | + . ' checks the given object again using `matches`' |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + [$name, $value] = $expression->args; |
| 94 | + |
| 95 | + return new ConstantElement( |
| 96 | + $this->determineFqsen($context, $name), |
| 97 | + $this->createDocBlock($strategies, $object->getDocComment(), $context), |
| 98 | + $this->determineValue($value), |
| 99 | + new Location($object->getLine()) |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + private function determineValue(?Arg $value) : ?string |
| 104 | + { |
| 105 | + if ($value === null) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + return $this->valueConverter->prettyPrintExpr($value->value); |
| 110 | + } |
| 111 | + |
| 112 | + private function determineFqsen(?Context $context, Arg $name) : Fqsen |
| 113 | + { |
| 114 | + /** @var String_ $nameString */ |
| 115 | + $nameString = $name->value; |
| 116 | + $namespace = $context ? $context->getNamespace() : ''; |
| 117 | + |
| 118 | + return new Fqsen(sprintf('\\%s\\%s', $namespace, $nameString->value)); |
| 119 | + } |
| 120 | +} |
0 commit comments