Complex classes like HalRenderer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HalRenderer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class HalRenderer implements RenderInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var Reader |
||
| 30 | */ |
||
| 31 | private $reader; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var RouterInterface |
||
| 35 | */ |
||
| 36 | private $router; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var ResourceInterface |
||
| 40 | */ |
||
| 41 | private $resource; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Curies |
||
| 45 | */ |
||
| 46 | private $curies; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param Reader $reader |
||
| 50 | * @param RouterInterface $router |
||
| 51 | */ |
||
| 52 | 4 | public function __construct(Reader $reader, RouterInterface $router, ResourceInterface $resource) |
|
| 53 | { |
||
| 54 | 4 | $this->reader = $reader; |
|
| 55 | 4 | $this->router = $router; |
|
| 56 | 4 | $this->resource = $resource; |
|
| 57 | 4 | } |
|
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | 10 | public function render(ResourceObject $ro) |
|
| 63 | { |
||
| 64 | 10 | $method = 'on' . ucfirst($ro->uri->method); |
|
| 65 | 10 | if (! method_exists($ro, $method)) { |
|
| 66 | 1 | $ro->view = ''; // no view for OPTIONS request |
|
| 67 | |||
| 68 | 1 | return ''; |
|
| 69 | } |
||
| 70 | 9 | $annotations = $this->reader->getMethodAnnotations(new \ReflectionMethod($ro, $method)); |
|
| 71 | 9 | $this->curies = $this->reader->getClassAnnotation(new \ReflectionClass($ro), Curies::class); |
|
| 72 | 9 | if ($this->isReturnCreatedResource($ro, $annotations)) { |
|
| 73 | 1 | return $this->returnCreatedResource($ro); |
|
| 74 | } |
||
| 75 | |||
| 76 | 9 | return $this->renderHal($ro, $annotations); |
|
| 77 | } |
||
| 78 | |||
| 79 | 9 | private function renderHal(ResourceObject $ro, $annotations) : string |
|
| 80 | { |
||
| 81 | 9 | list($ro, $body) = $this->valuate($ro); |
|
| 82 | /* @var $annotations Link[] */ |
||
| 83 | /* @var $ro ResourceObject */ |
||
| 84 | 9 | $hal = $this->getHal($ro->uri, $body, $annotations); |
|
| 85 | 9 | $ro->view = $hal->asJson(true) . PHP_EOL; |
|
| 86 | 9 | $this->updateHeaders($ro); |
|
| 87 | |||
| 88 | 9 | return $ro->view; |
|
| 89 | } |
||
| 90 | |||
| 91 | 9 | private function isReturnCreatedResource(ResourceObject $ro, array $annotations) : bool |
|
| 92 | { |
||
| 93 | 9 | return $ro->code === 201 && $ro->uri->method === 'post' && isset($ro->headers['Location']) && $this->hasReturnCreatedResource($annotations); |
|
| 94 | } |
||
| 95 | |||
| 96 | 1 | private function hasReturnCreatedResource(array $annotations) : bool |
|
| 97 | { |
||
| 98 | 1 | foreach ($annotations as $annotation) { |
|
| 99 | 1 | if ($annotation instanceof ReturnCreatedResource) { |
|
| 100 | 1 | return true; |
|
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | return false; |
||
| 105 | } |
||
| 106 | |||
| 107 | 1 | private function returnCreatedResource(ResourceObject $ro) : string |
|
| 108 | { |
||
| 109 | 1 | $ro->view = $this->getLocatedView($ro); |
|
| 110 | 1 | $this->updateHeaders($ro); |
|
| 111 | |||
| 112 | 1 | return $ro->view; |
|
| 113 | } |
||
| 114 | |||
| 115 | 8 | private function valuateElements(ResourceObject &$ro) |
|
| 116 | { |
||
| 117 | 8 | foreach ($ro->body as $key => &$embeded) { |
|
| 118 | 8 | if ($embeded instanceof AbstractRequest) { |
|
| 119 | 2 | $isDefferentSchema = $this->isDifferentSchema($ro, $embeded->resourceObject); |
|
| 120 | 2 | if ($isDefferentSchema === true) { |
|
| 121 | 1 | $ro->body['_embedded'][$key] = $embeded()->body; |
|
| 122 | 1 | unset($ro->body[$key]); |
|
| 123 | 1 | continue; |
|
| 124 | } |
||
| 125 | 1 | unset($ro->body[$key]); |
|
| 126 | 1 | $view = $this->render($embeded()); |
|
| 127 | 8 | $ro->body['_embedded'][$key] = json_decode($view); |
|
| 128 | } |
||
| 129 | } |
||
| 130 | 8 | } |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Return "is different schema" (page <-> app) |
||
| 134 | */ |
||
| 135 | 2 | private function isDifferentSchema(ResourceObject $parentRo, ResourceObject $childRo) : bool |
|
| 136 | { |
||
| 137 | 2 | return $parentRo->uri->scheme . $parentRo->uri->host !== $childRo->uri->scheme . $childRo->uri->host; |
|
| 138 | } |
||
| 139 | |||
| 140 | 9 | private function getHal(AbstractUri $uri, array $body, array $annotations) : Hal |
|
| 141 | { |
||
| 142 | 9 | $query = $uri->query ? '?' . http_build_query($uri->query) : ''; |
|
| 143 | 9 | $path = $uri->path . $query; |
|
| 144 | 9 | $selfLink = $this->getReverseMatchedLink($path); |
|
| 145 | |||
| 146 | 9 | $hal = new Hal($selfLink, $body); |
|
| 147 | 9 | $hal = $this->getHalLink($body, $annotations, $hal); |
|
| 148 | |||
| 149 | 9 | return $hal; |
|
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return mixed |
||
| 154 | */ |
||
| 155 | 9 | private function getReverseMatchedLink(string $uri) |
|
| 156 | { |
||
| 157 | 9 | $urlParts = parse_url($uri); |
|
| 158 | 9 | $routeName = $urlParts['path']; |
|
| 159 | 9 | isset($urlParts['query']) ? parse_str($urlParts['query'], $value) : $value = []; |
|
| 160 | 9 | if ($value === []) { |
|
| 161 | 4 | return $uri; |
|
| 162 | } |
||
| 163 | 6 | $reverseUri = $this->router->generate($routeName, (array) $value); |
|
| 164 | 6 | if (is_string($reverseUri)) { |
|
| 165 | 2 | return $reverseUri; |
|
| 166 | } |
||
| 167 | |||
| 168 | 4 | return $uri; |
|
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return array [ResourceObject, array] |
||
| 173 | */ |
||
| 174 | 9 | private function valuate(ResourceObject $ro) : array |
|
| 190 | |||
| 191 | 9 | private function getHalLink(array $body, array $methodAnnotations, Hal $hal) : Hal |
|
| 192 | { |
||
| 193 | 9 | if ($this->curies instanceof Curies) { |
|
| 194 | 1 | $hal->addCurie($this->curies->name, $this->curies->href); |
|
| 195 | } |
||
| 196 | 9 | if (! empty($methodAnnotations)) { |
|
| 197 | 5 | $hal = $this->linkAnnotation($body, $methodAnnotations, $hal); |
|
| 198 | } |
||
| 199 | 9 | if (isset($body['_links'])) { |
|
| 200 | 2 | $hal = $this->bodyLink($body, $hal); |
|
| 205 | |||
| 206 | 9 | private function updateHeaders(ResourceObject $ro) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Return `Location` URI view |
||
| 216 | */ |
||
| 217 | 1 | private function getLocatedView(ResourceObject $ro) : string |
|
| 229 | |||
| 230 | 5 | private function linkAnnotation(array $body, array $methodAnnotations, Hal $hal) : Hal |
|
| 243 | |||
| 244 | 2 | private function bodyLink(array $body, Hal $hal) : Hal |
|
| 254 | } |
||
| 255 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: