apie-lib /
fake-annotations-plugin
| 1 | <?php |
||
| 2 | namespace Apie\FakeAnnotationsPlugin\Readers; |
||
| 3 | |||
| 4 | use Apie\Core\Annotations\ApiResource; |
||
| 5 | use Doctrine\Common\Annotations\Reader; |
||
| 6 | use ReflectionClass; |
||
| 7 | use ReflectionMethod; |
||
| 8 | use ReflectionProperty; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Extend the annotation reader with a config array to override the actual annotations if required. Should only |
||
| 12 | * work on the ApiResource annotation. |
||
| 13 | */ |
||
| 14 | class ExtendReaderWithConfigReader implements Reader |
||
| 15 | { |
||
| 16 | private $reader; |
||
| 17 | |||
| 18 | private $config; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param Reader $reader |
||
| 22 | * @param ApiResource[] $config |
||
| 23 | */ |
||
| 24 | public function __construct(Reader $reader, array $config) |
||
| 25 | { |
||
| 26 | $this->reader = $reader; |
||
| 27 | $this->config = $config; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * {@inheritDoc} |
||
| 32 | */ |
||
| 33 | public function getClassAnnotations(ReflectionClass $class) |
||
| 34 | { |
||
| 35 | $className = $class->getName(); |
||
| 36 | $annotations = $this->reader->getClassAnnotations($class); |
||
| 37 | if (isset($this->config[$className])) { |
||
| 38 | $annotations = array_filter($annotations, function ($annotation) { |
||
| 39 | return !($annotation instanceof ApiResource); |
||
| 40 | }); |
||
| 41 | $annotations[] = $this->config[$className]; |
||
| 42 | } |
||
| 43 | |||
| 44 | return $annotations; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritDoc} |
||
| 49 | */ |
||
| 50 | public function getClassAnnotation(ReflectionClass $class, $annotationName) |
||
| 51 | { |
||
| 52 | $className = $class->getName(); |
||
| 53 | if ($annotationName === ApiResource::class && isset($this->config[$className])) { |
||
| 54 | return $this->config[$className]; |
||
|
0 ignored issues
–
show
|
|||
| 55 | } |
||
| 56 | return $this->reader->getClassAnnotation($class, $annotationName); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritDoc} |
||
| 61 | */ |
||
| 62 | public function getMethodAnnotations(ReflectionMethod $method) |
||
| 63 | { |
||
| 64 | return $this->reader->getMethodAnnotations($method); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritDoc} |
||
| 69 | */ |
||
| 70 | public function getMethodAnnotation(ReflectionMethod $method, $annotationName) |
||
| 71 | { |
||
| 72 | return $this->reader->getMethodAnnotation($method, $annotationName); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritDoc} |
||
| 77 | */ |
||
| 78 | public function getPropertyAnnotations(ReflectionProperty $property) |
||
| 79 | { |
||
| 80 | return $this->reader->getPropertyAnnotations($property); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritDoc} |
||
| 85 | */ |
||
| 86 | public function getPropertyAnnotation(ReflectionProperty $property, $annotationName) |
||
| 87 | { |
||
| 88 | return $this->reader->getPropertyAnnotation($property, $annotationName); |
||
| 89 | } |
||
| 90 | } |
||
| 91 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: