|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpDocReader; |
| 4 | + |
| 5 | +use Doctrine\Common\Annotations\PhpParser; |
| 6 | +use ReflectionClass; |
| 7 | +use ReflectionMethod; |
| 8 | +use ReflectionParameter; |
| 9 | +use ReflectionProperty; |
| 10 | + |
| 11 | +/** |
| 12 | + * PhpDoc reader |
| 13 | + * |
| 14 | + * @author Matthieu Napoli <matthieu@mnapoli.fr> |
| 15 | + */ |
| 16 | +class PhpDocReader |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var PhpParser |
| 20 | + */ |
| 21 | + private $phpParser; |
| 22 | + |
| 23 | + |
| 24 | + /** |
| 25 | + * Constructor |
| 26 | + */ |
| 27 | + public function __construct() |
| 28 | + { |
| 29 | + $this->phpParser = new PhpParser(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Parse the docblock of the property to get the var annotation |
| 34 | + * @param ReflectionClass $class |
| 35 | + * @param ReflectionProperty $property |
| 36 | + * @throws AnnotationException |
| 37 | + * @return string|null Type of the property (content of var annotation) |
| 38 | + */ |
| 39 | + public function getPropertyType(ReflectionClass $class, ReflectionProperty $property) |
| 40 | + { |
| 41 | + // Get the content of the @var annotation |
| 42 | + if (preg_match('/@var\s+([^\s]+)/', $property->getDocComment(), $matches)) { |
| 43 | + list(, $type) = $matches; |
| 44 | + } else { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + // If the class name is not fully qualified (i.e. doesn't start with a \) |
| 49 | + if ($type[0] !== '\\') { |
| 50 | + $alias = (false === $pos = strpos($type, '\\')) ? $type : substr($type, 0, $pos); |
| 51 | + $loweredAlias = strtolower($alias); |
| 52 | + |
| 53 | + // Retrieve "use" statements |
| 54 | + $uses = $this->phpParser->parseClass($property->getDeclaringClass()); |
| 55 | + |
| 56 | + $found = false; |
| 57 | + |
| 58 | + if (isset($uses[$loweredAlias])) { |
| 59 | + // Imported classes |
| 60 | + if (false !== $pos) { |
| 61 | + $type = $uses[$loweredAlias] . substr($type, $pos); |
| 62 | + } else { |
| 63 | + $type = $uses[$loweredAlias]; |
| 64 | + } |
| 65 | + $found = true; |
| 66 | + } elseif ($this->classExists($class->getNamespaceName() . '\\' . $type)) { |
| 67 | + $type = $class->getNamespaceName() . '\\' . $type; |
| 68 | + $found = true; |
| 69 | + } elseif (isset($uses['__NAMESPACE__']) && $this->classExists($uses['__NAMESPACE__'] . '\\' . $type)) { |
| 70 | + // Class namespace |
| 71 | + $type = $uses['__NAMESPACE__'] . '\\' . $type; |
| 72 | + $found = true; |
| 73 | + } elseif ($this->classExists($type)) { |
| 74 | + // No namespace |
| 75 | + $found = true; |
| 76 | + } |
| 77 | + |
| 78 | + if (!$found) { |
| 79 | + throw new AnnotationException("The @var annotation on {$class->name}::" . $property->getName() |
| 80 | + . " contains a non existent class. Did you maybe forget to add a 'use' statement for this annotation?"); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (!$this->classExists($type)) { |
| 85 | + throw new AnnotationException("The @var annotation on {$class->name}::" . $property->getName() |
| 86 | + . " contains a non existent class"); |
| 87 | + } |
| 88 | + |
| 89 | + // Remove the leading \ (FQN shouldn't contain it) |
| 90 | + $type = ltrim($type, '\\'); |
| 91 | + |
| 92 | + return $type; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Parse the docblock of the property to get the param annotation |
| 97 | + * @param ReflectionClass $class |
| 98 | + * @param ReflectionMethod $method |
| 99 | + * @throws AnnotationException |
| 100 | + * @return string|null Type of the property (content of var annotation) |
| 101 | + */ |
| 102 | + public function getParameterType(ReflectionClass $class, ReflectionMethod $method, ReflectionParameter $parameter) |
| 103 | + { |
| 104 | + // Use reflection |
| 105 | + $parameterClass = $parameter->getClass(); |
| 106 | + if ($parameterClass !== null) { |
| 107 | + return $parameterClass->name; |
| 108 | + } |
| 109 | + |
| 110 | + $parameterName = $parameter->name; |
| 111 | + // Get the content of the @param annotation |
| 112 | + if (preg_match('/@param\s+([^\s]+)\s+\$' . $parameterName . '/', $method->getDocComment(), $matches)) { |
| 113 | + list(, $type) = $matches; |
| 114 | + } else { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + // If the class name is not fully qualified (i.e. doesn't start with a \) |
| 119 | + if ($type[0] !== '\\') { |
| 120 | + $alias = (false === $pos = strpos($type, '\\')) ? $type : substr($type, 0, $pos); |
| 121 | + $loweredAlias = strtolower($alias); |
| 122 | + |
| 123 | + // Retrieve "use" statements |
| 124 | + $uses = $this->phpParser->parseClass($method->getDeclaringClass()); |
| 125 | + |
| 126 | + $found = false; |
| 127 | + |
| 128 | + if (isset($uses[$loweredAlias])) { |
| 129 | + // Imported classes |
| 130 | + if (false !== $pos) { |
| 131 | + $type = $uses[$loweredAlias] . substr($type, $pos); |
| 132 | + } else { |
| 133 | + $type = $uses[$loweredAlias]; |
| 134 | + } |
| 135 | + $found = true; |
| 136 | + } elseif ($this->classExists($class->getNamespaceName() . '\\' . $type)) { |
| 137 | + $type = $class->getNamespaceName() . '\\' . $type; |
| 138 | + $found = true; |
| 139 | + } elseif (isset($uses['__NAMESPACE__']) && $this->classExists($uses['__NAMESPACE__'] . '\\' . $type)) { |
| 140 | + // Class namespace |
| 141 | + $type = $uses['__NAMESPACE__'] . '\\' . $type; |
| 142 | + $found = true; |
| 143 | + } elseif ($this->classExists($type)) { |
| 144 | + // No namespace |
| 145 | + $found = true; |
| 146 | + } |
| 147 | + |
| 148 | + if (!$found) { |
| 149 | + throw new AnnotationException("The @param annotation for parameter $parameterName of {$class->name}::" . $method->name |
| 150 | + . " contains a non existent class. Did you maybe forget to add a 'use' statement for this annotation?"); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + if (!$this->classExists($type)) { |
| 155 | + throw new AnnotationException("The @param annotation for parameter $parameterName of {$class->name}::" . $method->name |
| 156 | + . " contains a non existent class"); |
| 157 | + } |
| 158 | + |
| 159 | + // Remove the leading \ (FQN shouldn't contain it) |
| 160 | + $type = ltrim($type, '\\'); |
| 161 | + |
| 162 | + return $type; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * @param string $class |
| 167 | + * @return bool |
| 168 | + */ |
| 169 | + private function classExists($class) |
| 170 | + { |
| 171 | + return class_exists($class) || interface_exists($class); |
| 172 | + } |
| 173 | +} |
0 commit comments