Complex classes like Geometry 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 Geometry, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Geometry implements ComponentInterface |
||
| 41 | { |
||
| 42 | |||
| 43 | const ALLOWED_PRIMITIVES = array( |
||
| 44 | 'box', |
||
| 45 | 'circle', |
||
| 46 | 'cone', |
||
| 47 | 'cylinder', |
||
| 48 | 'plane', |
||
| 49 | 'ring', |
||
| 50 | 'sphere', |
||
| 51 | 'torus', |
||
| 52 | 'torusKnot' |
||
| 53 | ); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Properties based on primitive |
||
| 57 | */ |
||
| 58 | const P_COMMON_PROPS = array( |
||
| 59 | |||
| 60 | /* One of box, circle, cone, cylinder, plane, ring, sphere, torus, torusKnot. */ |
||
| 61 | 'primitive' => null, |
||
| 62 | |||
| 63 | /* The translate property translates the geometry. It is provided as a vec3. This is a useful short-hand for |
||
| 64 | * translating the geometry to effectively move its pivot point when running animations. */ |
||
| 65 | 'translate' => '0 1 0' |
||
| 66 | ); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The box primitive defines boxes (i.e., any quadilateral, not just cubes). |
||
| 70 | */ |
||
| 71 | const P_BOX = array( |
||
| 72 | /* Width (in meters) of the sides on the X axis. */ |
||
| 73 | 'width' => 1, |
||
| 74 | /* Height (in meters) of the sides on the Y axis. */ |
||
| 75 | 'height' => 1, |
||
| 76 | /* Depth (in meters) of the sides on the Z axis. */ |
||
| 77 | 'depth' => 1 |
||
| 78 | ); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The circle primitive defines two-dimensional circles, which can be complete circles or partial circles (like Pac-Man). |
||
| 82 | * Note that because it is flat, only a single side of the circle will be rendered if “side: double” is not specified on the material component. |
||
| 83 | */ |
||
| 84 | const P_CIRCLE = array( |
||
| 85 | /* Radius (in meters) of the circle. */ |
||
| 86 | 'radius' => 1, |
||
| 87 | /* Number of triangles to construct the circle, like pizza slices. A higher number of segments means the circle will be more round. */ |
||
| 88 | 'segments' => 32, |
||
| 89 | /* Start angle for first segment. Can be used to define a partial circle. */ |
||
| 90 | 'thetaStart' => 0, |
||
| 91 | /* The central angle (in degrees). Defaults to 360, which makes for a complete circle. */ |
||
| 92 | 'thetaLength' => 360 |
||
| 93 | ); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The cone primitive under the hood is a cylinder primitive with varying top and bottom radiuses. |
||
| 97 | */ |
||
| 98 | const P_CONE = array( |
||
| 99 | /* Height of the cone. */ |
||
| 100 | 'height' => 2, |
||
| 101 | /* Whether the ends of the cone are open (true) or capped ('false'). */ |
||
| 102 | 'openEnded' => 'false', |
||
| 103 | /* Radius of the bottom end of the cone. */ |
||
| 104 | 'radiusBottom' => 1, |
||
| 105 | /* Radius of the top end of the cone. */ |
||
| 106 | 'radiusTop' => 1, |
||
| 107 | /* Number of segmented faces around the circumference of the cone. */ |
||
| 108 | 'segmentsRadial' => 36, |
||
| 109 | /* Number of rows of faces along the height of the cone. */ |
||
| 110 | 'segmentsHeight' => 18, |
||
| 111 | /* Starting angle in degrees. */ |
||
| 112 | 'thetaStart' => 0, |
||
| 113 | /* Central angle in degrees. */ |
||
| 114 | 'thetaLength' => 360 |
||
| 115 | ); |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The cylinder primitive can define cylinders in the traditional sense like a Coca-Cola™ can, |
||
| 119 | * but it can also define shapes such as tubes and curved surfaces. |
||
| 120 | * We’ll go over some of these cylinder recipes below. |
||
| 121 | * 1. Traditional cylinders can be defined by using only a height and a radius: |
||
| 122 | * 2. Tubes can be defined by making the cylinder open-ended, which removes the top and bottom surfaces of the cylinder such that the inside is visible. |
||
| 123 | * A double-sided material will be needed to render properly: |
||
| 124 | * 3. Curved surfaces can be defined by specifying the angle via thetaLength such that the cylinder doesn’t curve all the way around, |
||
| 125 | * making the cylinder open-ended, and then making the material double-sided. |
||
| 126 | * 4. Other types of prisms can be defined by varying the number of radial segments (i.e., sides). For example, to make a hexagonal prism: |
||
| 127 | */ |
||
| 128 | const P_CYLINDER = array( |
||
| 129 | /* Radius of the cylinder. */ |
||
| 130 | 'height' => 3, |
||
| 131 | /* Height of the cylinder. */ |
||
| 132 | 'radius' => 2, |
||
| 133 | /* Number of segmented faces around the circumference of the cylinder. */ |
||
| 134 | 'segmentsRadial' => 36, |
||
| 135 | /* Number of rows of faces along the height of the cylinder. */ |
||
| 136 | 'segmentsHeight' => 18, |
||
| 137 | /* Whether the ends of the cylinder are open (true) or capped ('false'). */ |
||
| 138 | 'openEnded' => 'false', |
||
| 139 | /* Starting angle in degrees. */ |
||
| 140 | 'thetaStart' => 0, |
||
| 141 | /* Central angle in degrees. */ |
||
| 142 | 'thetaLength' => 360 |
||
| 143 | ); |
||
| 144 | |||
| 145 | /** |
||
| 146 | * The plane primitive defines a flat surface. |
||
| 147 | * Note that because it is flat, |
||
| 148 | * only a single side of the plane will be rendered if side: double is not specified on the material component. |
||
| 149 | */ |
||
| 150 | const P_PLANE = array( |
||
| 151 | /* Width along the X axis. */ |
||
| 152 | 'width' => 1, |
||
| 153 | /* Height along the Y axis. */ |
||
| 154 | 'height' => 1 |
||
| 155 | ); |
||
| 156 | |||
| 157 | /** |
||
| 158 | * The ring geometry defines a flat ring, like a CD. |
||
| 159 | * Note that because it is flat, |
||
| 160 | * only a single side of the ring will be rendered if side: double is not specified on the material component. |
||
| 161 | */ |
||
| 162 | const P_RING = array( |
||
| 163 | /* Radius of the inner hole of the ring. */ |
||
| 164 | 'radiusInner' => 1, |
||
| 165 | /* Radius of the outer edge of the ring. */ |
||
| 166 | 'radiusOuter' => 1, |
||
| 167 | /* Number of segments. A higher number means the ring will be more round */ |
||
| 168 | 'segmentsTheta' => 32, |
||
| 169 | /* Number of triangles within each face defined by segmentsTheta. */ |
||
| 170 | 'segmentsPhi' => 8, |
||
| 171 | /* Starting angle in degrees. */ |
||
| 172 | 'thetaStart' => 0, |
||
| 173 | /* Central angle in degrees. */ |
||
| 174 | 'thetaLength' => 360 |
||
| 175 | ); |
||
| 176 | |||
| 177 | /** |
||
| 178 | * The sphere primitive can define spheres in the traditional sense like a basketball. |
||
| 179 | * |
||
| 180 | * But it can also define various polyhedrons and abstract shapes given that it can specify |
||
| 181 | * the number of horizontal and vertical angles and faces. |
||
| 182 | * |
||
| 183 | * Sticking with a basic sphere, the default number of segments is high enough to make the sphere appear round. |
||
| 184 | */ |
||
| 185 | const P_SPHERE = array( |
||
| 186 | /* Radius of the sphere. */ |
||
| 187 | 'radius' => 1, |
||
| 188 | /* Number of horizontal segments. */ |
||
| 189 | 'segmentsWidth' => 18, |
||
| 190 | /* Number of vertical segments. */ |
||
| 191 | 'segmentsHeight' => 36, |
||
| 192 | /* Horizontal starting angle. */ |
||
| 193 | 'phiStart' => 0, |
||
| 194 | /* Horizontal sweep angle size. */ |
||
| 195 | 'phiLength' => 360, |
||
| 196 | /* Vertical starting angle. */ |
||
| 197 | 'thetaStart' => 0, |
||
| 198 | /* Vertical sweep angle size. */ |
||
| 199 | 'thetaLength' => 360 |
||
| 200 | ); |
||
| 201 | |||
| 202 | /** |
||
| 203 | * The torus primitive defines a donut shape. |
||
| 204 | */ |
||
| 205 | const P_TORUS = array( |
||
| 206 | /* Radius of the outer edge of the torus. */ |
||
| 207 | 'radius' => 1, |
||
| 208 | /* Radius of the tube. */ |
||
| 209 | 'radiusTubular' => 0.2, |
||
| 210 | /* Number of segments along the circumference of the tube ends. A higher number means the tube will be more round. */ |
||
| 211 | 'segmentsRadial' => 36, |
||
| 212 | /* Number of segments along the circumference of the tube face. A higher number means the tube will be more round. */ |
||
| 213 | 'segmentsTubular' => 32, |
||
| 214 | /* Central angle. */ |
||
| 215 | 'arc' => 360 |
||
| 216 | ); |
||
| 217 | |||
| 218 | /** |
||
| 219 | * The torus knot primitive defines a pretzel shape, the particular shape of which is defined by a pair of coprime integers, |
||
| 220 | * p and q. |
||
| 221 | * If p and q are not coprime the result will be a torus link. |
||
| 222 | */ |
||
| 223 | const P_TORUS_KNOT = array( |
||
| 224 | /* Radius that contains the torus knot. */ |
||
| 225 | 'radius' => 1, |
||
| 226 | /* Radius of the tubes of the torus knot. */ |
||
| 227 | 'radiusTubular' => 0.2, |
||
| 228 | /* Number of segments along the circumference of the tube ends. A higher number means the tube will be more round. */ |
||
| 229 | 'segmentsRadial' => 36, |
||
| 230 | /* Number of segments along the circumference of the tube face. A higher number means the tube will be more round. */ |
||
| 231 | 'segmentsTubular' => 32, |
||
| 232 | /* Number that helps define the pretzel shape. */ |
||
| 233 | 'p' => 2, |
||
| 234 | /* Number that helps define the pretzel shape. */ |
||
| 235 | 'q' => 3 |
||
| 236 | ); |
||
| 237 | |||
| 238 | /* DOM atrributes */ |
||
| 239 | protected $primitive; |
||
| 240 | |||
| 241 | protected $width; |
||
| 242 | |||
| 243 | protected $height; |
||
| 244 | |||
| 245 | protected $depth; |
||
| 246 | |||
| 247 | protected $radius; |
||
| 248 | |||
| 249 | protected $radiusTubular; |
||
| 250 | |||
| 251 | protected $radiusBottom; |
||
| 252 | |||
| 253 | protected $radiusTop; |
||
| 254 | |||
| 255 | protected $segments; |
||
| 256 | |||
| 257 | protected $thetaStart; |
||
| 258 | |||
| 259 | protected $thetaLength; |
||
| 260 | |||
| 261 | protected $openEnded; |
||
| 262 | |||
| 263 | protected $segmentsRadial; |
||
| 264 | |||
| 265 | protected $segmentsHeight; |
||
| 266 | |||
| 267 | protected $segmentsWidth; |
||
| 268 | |||
| 269 | protected $segmentsTheta; |
||
| 270 | |||
| 271 | protected $segmentsPhi; |
||
| 272 | |||
| 273 | protected $segmentsTubular; |
||
| 274 | |||
| 275 | protected $phiStart; |
||
| 276 | |||
| 277 | protected $phiLength; |
||
| 278 | |||
| 279 | protected $radiusInner; |
||
| 280 | |||
| 281 | protected $radiusOuter; |
||
| 282 | |||
| 283 | protected $arc; |
||
| 284 | |||
| 285 | protected $p; |
||
| 286 | |||
| 287 | protected $q; |
||
| 288 | |||
| 289 | protected $translate; |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get Component scripts |
||
| 293 | * |
||
| 294 | * {@inheritdoc} |
||
| 295 | * |
||
| 296 | * @return array |
||
| 297 | */ |
||
| 298 | public function getScripts(): array |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Does component have DOM Atributes |
||
| 305 | * |
||
| 306 | * {@inheritdoc} |
||
| 307 | * |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | public function hasDOMAttributes(): bool |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Remove default DOMElement Attributes which are not required |
||
| 317 | * |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | public function removeDefaultDOMAttributes() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get DOMAttr for the entity |
||
| 331 | * |
||
| 332 | * @return DOMAttr |
||
| 333 | */ |
||
| 334 | public function getDOMAttributes(): DOMAttr |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Set primitive |
||
| 342 | * |
||
| 343 | * One of box, circle, cone, cylinder, plane, ring, sphere, torus, torusKnot. |
||
| 344 | * |
||
| 345 | * @param unknown $primitive |
||
|
|
|||
| 346 | * @throws InvalidComponentArgumentException |
||
| 347 | */ |
||
| 348 | 8 | public function primitive($primitive = null) |
|
| 349 | { |
||
| 350 | try { |
||
| 351 | 8 | if (in_array($primitive, self::ALLOWED_PRIMITIVES)) { |
|
| 352 | /* If primitive is changed we reset the object and releoad allowed attributes */ |
||
| 353 | 8 | $this->primitive = $primitive; |
|
| 354 | 8 | $defaults = $this->primitiveDefaults(); |
|
| 355 | 8 | foreach (get_class_vars(get_class($this)) as $name => $default) { |
|
| 356 | 8 | if ($name === 'primitive') |
|
| 357 | 8 | continue; |
|
| 358 | |||
| 359 | 8 | if (array_key_exists($name, $defaults)) |
|
| 360 | 8 | $this->$name = $defaults[$name]; |
|
| 361 | 8 | elseif (array_key_exists($name, self::P_COMMON_PROPS)) |
|
| 362 | 8 | $this->$name = self::P_COMMON_PROPS[$name]; |
|
| 363 | else |
||
| 364 | 8 | unset($this->$name); |
|
| 365 | } |
||
| 366 | } else { |
||
| 367 | 8 | throw new InvalidComponentArgumentException($primitive, 'Geometry::primitive'); |
|
| 368 | } |
||
| 369 | } catch (InvalidComponentArgumentException $e) { |
||
| 370 | die($e->getMessage()); |
||
| 371 | } |
||
| 372 | 8 | } |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Set Buffer |
||
| 376 | * |
||
| 377 | * Transform geometry into a BufferGeometry to reduce memory usage at the cost of being harder to manipulate. |
||
| 378 | * |
||
| 379 | * @param string $buffer |
||
| 380 | */ |
||
| 381 | public function buffer($buffer = 'true') |
||
| 385 | |||
| 386 | /** |
||
| 387 | * skipCache |
||
| 388 | * |
||
| 389 | * Disable retrieving the shared geometry object from the cache. |
||
| 390 | * |
||
| 391 | * @param string $skipCache |
||
| 392 | */ |
||
| 393 | public function skipCache($skipCache = 'false') |
||
| 397 | |||
| 398 | 4 | public function width($width) |
|
| 399 | { |
||
| 400 | 4 | $this->width = $width; |
|
| 401 | 4 | } |
|
| 402 | |||
| 403 | 8 | public function height($height) |
|
| 404 | { |
||
| 405 | 8 | $this->height = $height; |
|
| 406 | 8 | } |
|
| 407 | |||
| 408 | 4 | public function depth($depth) |
|
| 409 | { |
||
| 410 | 4 | $this->depth = $depth; |
|
| 411 | 4 | } |
|
| 412 | |||
| 413 | 4 | public function radius($radius) |
|
| 414 | { |
||
| 415 | 4 | $this->radius = $radius; |
|
| 416 | 4 | } |
|
| 417 | |||
| 418 | public function radiusTubular($radiusTubular) |
||
| 422 | |||
| 423 | public function radiusBottom($radiusBottom) |
||
| 427 | |||
| 428 | public function radiusTop($radiusTop) |
||
| 432 | |||
| 433 | public function segments($segments) |
||
| 437 | |||
| 438 | 4 | public function thetaStart($thetaStart) |
|
| 439 | { |
||
| 440 | 4 | $this->thetaStart = $thetaStart; |
|
| 441 | 4 | } |
|
| 442 | |||
| 443 | 4 | public function thetaLength($thetaLength) |
|
| 444 | { |
||
| 445 | 4 | $this->thetaLength = $thetaLength; |
|
| 446 | 4 | } |
|
| 447 | |||
| 448 | 4 | public function openEnded($openEnded) |
|
| 449 | { |
||
| 450 | 4 | $this->openEnded = $openEnded; |
|
| 451 | 4 | } |
|
| 452 | |||
| 453 | public function segmentsRadial($segmentsRadial) |
||
| 457 | |||
| 458 | 4 | public function segmentsHeight($segmentsHeight) |
|
| 459 | { |
||
| 460 | 4 | $this->segmentsHeight = $segmentsHeight; |
|
| 461 | 4 | } |
|
| 462 | |||
| 463 | public function segmentsWidth($segmentsWidth) |
||
| 467 | |||
| 468 | public function segmentsTheta($segmentsTheta) |
||
| 472 | |||
| 473 | public function segmentsPhi($segmentsPhi) |
||
| 477 | |||
| 478 | public function segmentsTubular($segmentsTubular) |
||
| 482 | |||
| 483 | public function phiStart($phiStart) |
||
| 487 | |||
| 488 | public function phiLength($phiLength) |
||
| 492 | |||
| 493 | public function radiusInner($radiusInner) |
||
| 497 | |||
| 498 | public function radiusOuter($radiusOuter) |
||
| 502 | |||
| 503 | public function arc($arc) |
||
| 507 | |||
| 508 | public function p($p) |
||
| 512 | |||
| 513 | public function q($q) |
||
| 517 | |||
| 518 | 3 | public function translate($translate) |
|
| 519 | { |
||
| 520 | 3 | $this->translate = $translate; |
|
| 521 | 3 | } |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Magic Call |
||
| 525 | * |
||
| 526 | * @param string $method |
||
| 527 | * @param array $args |
||
| 528 | * @throws InvalidComponentMethodException |
||
| 529 | */ |
||
| 530 | public function __call($method, $args) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Is called method allowed for current primitive |
||
| 554 | * |
||
| 555 | * @param unknown $method |
||
| 556 | */ |
||
| 557 | protected function isPrimitiveMethod($method) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Get defaults for current primitve type |
||
| 595 | * |
||
| 596 | * @return array; |
||
| 597 | */ |
||
| 598 | 8 | protected function primitiveDefaults(): array |
|
| 599 | { |
||
| 600 | 8 | switch ($this->primitive) { |
|
| 601 | 8 | case 'box': |
|
| 602 | 4 | return self::P_BOX; |
|
| 633 | } |
||
| 634 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.